📜  JDBC中如何在一个数据库上同时执行多个SQL命令?

📅  最后修改于: 2022-05-13 01:54:33.039000             🧑  作者: Mango

JDBC中如何在一个数据库上同时执行多个SQL命令?

Java数据库连接也称为 JDBC,是Java中的应用程序编程接口,用于在Java应用程序和数据库之间建立连接。 JDBC 命令可用于从Java应用程序执行 SQL 操作。演示使用 JDBC 的 addBatch() 和 executeBatch() 命令在数据库上同时执行多个 SQL 命令。

目标:演示两个示例,其中一个使用Statement 接口,另一个使用PreparedStatement接口。 PreparedStatement 的性能优于 Statement 接口。 Statement 接口可用于执行静态 SQL 查询,而 PreparedStatement 接口可用于多次执行动态 SQL 查询。

示例 1:使用语句接口

在此示例中,导入了Java.sql 包类和接口。 Statement 接口用于执行 sql 语句。该表是创建 sql 语句以及记录插入 sql 语句,使用 addBatch() 命令添加到批处理中。当所有语句都被批处理时,executeBatch() 命令被执行,它同时运行所有批处理查询。 sql 语句可能会抛出 SQL 异常,必须在 try catch 块中处理这些异常,以避免程序突然终止。创建表并插入记录后,要查看表中的数据,执行选择查询。通过执行选择查询获得的结果存储在 ResultSet 游标中。使用 next() 方法迭代光标,并在屏幕上显示记录。

实现:使用标准接口

Java
// Step 1: Create a database
// SQL database imported
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class BatchCommand {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Try block to check if exception occurs
        try {
 
            // Step 2: Loading driver class
            // Using forName()
            Class.forName("oracle.jdbc.OracleDriver");
 
            // Step 3: Create connection object
            Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "username", "password");
            Statement s = con.createStatement();
           
            // Step 4: Create a statement / create table
            String sql1
                = "CREATE TABLE STUDENT(STUDENTID VARCHAR2(10) PRIMARY KEY,NAME VARCHAR2(20),DEPARTMENT VARCHAR2(10))";
 
            // Step 5: Process a query
            // Insert records in the table
            String sql2
                = "INSERT INTO STUDENT VALUES('S101','JEAN','CSE')";
            String sql3
                = "INSERT INTO STUDENT VALUES('S102','ANA','CSE')";
            String sql4
                = "INSERT INTO STUDENT VALUES('S103','ROBERT','ECE')";
            String sql5
                = "INSERT INTO STUDENT VALUES('S104','ALEX','IT')";
            String sql6
                = "INSERT INTO STUDENT VALUES('S105','DIANA','IT')";
            s.addBatch(sql1);
            s.addBatch(sql2);
            s.addBatch(sql3);
            s.addBatch(sql4);
            s.addBatch(sql5);
            s.addBatch(sql6);
 
            // Step 6: Process the results
            // execute the sql statements
            s.executeBatch();
            ResultSet rs
                = s.executeQuery("Select * from Student");
 
            // Print commands
            System.out.println(
                "StudentID\tName\t\tDepartment");
            System.out.println(
                "-------------------------------------------------------");
 
            // Condition to check pointer pointing
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t\t"
                                   + rs.getString(2)
                                   + "\t\t"
                                   + rs.getString(3));
            }
 
            // Step 7: Close the connection
            con.commit();
            con.close();
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Print line number if exception occured
            System.out.println(e);
        }
    }
}


Java
// Step 1: Importing database
// SQL database imported
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class BatchCommand {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Try block to handle if exception occurs
        try {
 
            // Step 2: loading driver class
            Class.forName("oracle.jdbc.OracleDriver");
 
            // Step 3: create connection object
            Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "username", "password");
            Statement s = con.createStatement();
 
            // Step 4: Create a statement
            // Create table
            String sql1
                = "CREATE TABLE STUDENT(STUDENTID VARCHAR2(10) PRIMARY KEY,NAME VARCHAR2(20),DEPARTMENT VARCHAR2(10))";
 
            // Step 5: Execute a query
            // Insert records in the table
            String sql2
                = "INSERT INTO STUDENT VALUES('S101','JEAN','CSE')";
            String sql3
                = "INSERT INTO STUDENT VALUES('S102','ANA','CSE')";
            String sql4
                = "INSERT INTO STUDENT VALUES('S103','ROBERT','ECE')";
            String sql5
                = "INSERT INTO STUDENT VALUES('S104','ALEX','IT')";
            String sql6
                = "INSERT INTO STUDENT VALUES('S105','DIANA','IT')";
            s.addBatch(sql1);
            s.addBatch(sql2);
            s.addBatch(sql3);
            s.addBatch(sql4);
            s.addBatch(sql5);
            s.addBatch(sql6);
 
            // Step 6: Process the statements
            // Create an int[] to hold returned values
            s.executeBatch();
            ResultSet rs
                = s.executeQuery("Select * from Student");
 
            // Print statements
            System.out.println(
                "StudentID\tName\t\tDepartment");
            System.out.println(
                "-------------------------------------------------------");
 
            // Condition check for pointer pointing which
            // record
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t\t"
                                   + rs.getString(2)
                                   + "\t\t"
                                   + rs.getString(3));
            }
 
            // Step 7: Close the connection
            con.commit();
            con.close();
        }
 
        // Catch block to handle exception
        catch (Exception e) {
 
            // Print line number where exception occured
            System.out.println(e);
        }
    }
}


Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
public class AddBatchCommand {
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        try {
 
            // loading driver class
            Class.forName("oracle.jdbc.OracleDriver");
 
            // create connection object
            Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "username", "password");
 
            // create the table
            String sql1
                = "CREATE TABLE STUDENTS(STUDENTID VARCHAR2(10) PRIMARY KEY,NAME VARCHAR2(20),DEPARTMENT VARCHAR2(10))";
            PreparedStatement ps
                = con.prepareStatement(sql1);
            ps.execute(sql1);
 
            // inserting records
            String sql
                = "Insert into Students values(?,?,?)";
            PreparedStatement ps1
                = con.prepareStatement(sql);
            for (int i = 0; i < 3; i++) {
                System.out.println("Enter Student ID");
                String id = scan.nextLine();
                System.out.println("Enter Student Name");
                String name = scan.nextLine();
                System.out.println("Enter the Department");
                String dept = scan.nextLine();
                ps1.setString(1, id);
                ps1.setString(2, name);
                ps1.setString(3, dept);
                // adding to batch
                ps1.addBatch();
            }
            // executing the batch
            ps1.executeBatch();
           
            // viewing the table
            ResultSet rs
                = ps.executeQuery("Select * from Students");
            System.out.println(
                "StudentID\tName\t\tDepartment");
            System.out.println(
                "-------------------------------------------------------");
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t\t"
                                   + rs.getString(2)
                                   + "\t\t"
                                   + rs.getString(3));
            }
            con.commit();
            con.close();
        }
 
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


使用 addBatch() 方法在 executeBatch() 的参与下对数据库的 SQL 命令

Java

// Step 1: Importing database
// SQL database imported
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class BatchCommand {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Try block to handle if exception occurs
        try {
 
            // Step 2: loading driver class
            Class.forName("oracle.jdbc.OracleDriver");
 
            // Step 3: create connection object
            Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "username", "password");
            Statement s = con.createStatement();
 
            // Step 4: Create a statement
            // Create table
            String sql1
                = "CREATE TABLE STUDENT(STUDENTID VARCHAR2(10) PRIMARY KEY,NAME VARCHAR2(20),DEPARTMENT VARCHAR2(10))";
 
            // Step 5: Execute a query
            // Insert records in the table
            String sql2
                = "INSERT INTO STUDENT VALUES('S101','JEAN','CSE')";
            String sql3
                = "INSERT INTO STUDENT VALUES('S102','ANA','CSE')";
            String sql4
                = "INSERT INTO STUDENT VALUES('S103','ROBERT','ECE')";
            String sql5
                = "INSERT INTO STUDENT VALUES('S104','ALEX','IT')";
            String sql6
                = "INSERT INTO STUDENT VALUES('S105','DIANA','IT')";
            s.addBatch(sql1);
            s.addBatch(sql2);
            s.addBatch(sql3);
            s.addBatch(sql4);
            s.addBatch(sql5);
            s.addBatch(sql6);
 
            // Step 6: Process the statements
            // Create an int[] to hold returned values
            s.executeBatch();
            ResultSet rs
                = s.executeQuery("Select * from Student");
 
            // Print statements
            System.out.println(
                "StudentID\tName\t\tDepartment");
            System.out.println(
                "-------------------------------------------------------");
 
            // Condition check for pointer pointing which
            // record
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t\t"
                                   + rs.getString(2)
                                   + "\t\t"
                                   + rs.getString(3));
            }
 
            // Step 7: Close the connection
            con.commit();
            con.close();
        }
 
        // Catch block to handle exception
        catch (Exception e) {
 
            // Print line number where exception occured
            System.out.println(e);
        }
    }
}

输出

示例2:在本示例中,导入了Java.sql 包类和接口。 PreparedStatement 接口用于执行 SQL 语句。该表是使用 addBatch() 命令将创建 SQL 语句与记录插入 SQL 语句一起添加到批处理中。当所有语句都被批处理时,executeBatch() 命令被执行,它同时运行所有批处理查询。 sql 语句可能会抛出 SQL 异常,必须在 try-catch 块中处理这些异常,以避免程序突然终止。创建表并插入记录后,要查看表中的数据,执行选择查询。通过执行选择查询获得的结果存储在 ResultSet 游标中。使用 next() 方法迭代光标,并在屏幕上显示记录。与前面的示例不同,它需要来自用户的动态输入。因此,使用 PreparedStatement 具有性能优势。

代码实现

Java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
public class AddBatchCommand {
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        try {
 
            // loading driver class
            Class.forName("oracle.jdbc.OracleDriver");
 
            // create connection object
            Connection con = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe",
                "username", "password");
 
            // create the table
            String sql1
                = "CREATE TABLE STUDENTS(STUDENTID VARCHAR2(10) PRIMARY KEY,NAME VARCHAR2(20),DEPARTMENT VARCHAR2(10))";
            PreparedStatement ps
                = con.prepareStatement(sql1);
            ps.execute(sql1);
 
            // inserting records
            String sql
                = "Insert into Students values(?,?,?)";
            PreparedStatement ps1
                = con.prepareStatement(sql);
            for (int i = 0; i < 3; i++) {
                System.out.println("Enter Student ID");
                String id = scan.nextLine();
                System.out.println("Enter Student Name");
                String name = scan.nextLine();
                System.out.println("Enter the Department");
                String dept = scan.nextLine();
                ps1.setString(1, id);
                ps1.setString(2, name);
                ps1.setString(3, dept);
                // adding to batch
                ps1.addBatch();
            }
            // executing the batch
            ps1.executeBatch();
           
            // viewing the table
            ResultSet rs
                = ps.executeQuery("Select * from Students");
            System.out.println(
                "StudentID\tName\t\tDepartment");
            System.out.println(
                "-------------------------------------------------------");
            while (rs.next()) {
                System.out.println(rs.getString(1) + "\t\t"
                                   + rs.getString(2)
                                   + "\t\t"
                                   + rs.getString(3));
            }
            con.commit();
            con.close();
        }
 
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

输出:同时说明数据库上的多个 SQL 命令: