回答

收藏

在Java中执行多个SQL语句

技术问答 技术问答 353 人阅读 | 0 人回复 | 2023-09-14

我想在 Java中    执行查询。4 B& U8 X: K$ L/ b) D$ y$ r, \
我创建了一个连接。然后,我想执行一个INSERT句子,完成后关闭连接,但我想通过连接执行一些插入句子,并在循环完成后关闭连接。
0 g6 \; |) w/ U; k7 f0 \我能做什么?3 R# M4 y, a& P# g1 T' O( H5 I
我的示例代码是:+ j6 v1 D1 W- n% Y  L
public NewClass() throws SQLException    try        Class.forName("oracle.jdbc.driver.OracleDriver");   catch (ClassNotFoundException e)        System.out.println("Where is your Oracle JDBC Driver?");        return;      System.out.println("Oracle JDBC Driver Registered!");    Connection connection = null;    try        connection = DriverManager.getConnection(               "jdbcracle:thinlocalhost:1521rcl1","test",               "oracle");   catch (SQLException e)              System.out.println("Connection Failed! Check output console");        return;      if (connection != null)              Statement stmt = connection.createStatement();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ResultSet rs = stmt.executeQuery("SELECT * from test.special_columns");        while (rs.next())                  this.ColName = rs.getNString("column_name");            this.script = "insert into test.alldata (colname) ( select "    ColName   "   from test.alldata2        " stmt.executeUpdate(""   script);                    else              System.out.println("Failed to make connection!");          }当执行select语句("SELECT * fromtest.special_columns")循环必须是两次,但当(stmt.executeUpdate(""  script))执行和完成时,关闭连接并从类中返回。) `; b& ^( B) J+ p
                                                                8 S# f- m( j9 b* R* k, W% n
    解决方案:                                                                0 w; m; B4 \, D, m" D* a
                                                                使用以下示例addBatch&executeBatch同时执行多个命令SQL命令。
2 a2 O1 f, w( y& U; }import java.sql.*;public class jdbcConn {   public static void main(String[] args) throws Exception{       Class.forName("org.apache.derby.jdbc.ClientDriver");      Connection con = DriverManager.getConnection      ("jdbc:derby://localhost:1527/testDb","name","pass");      Statement stmt = con.createStatement      (ResultSet.TYPE_SCROLL_SENSITIVE,     ResultSet.CONCUR_UPDATABLE);      String insertEmp1 = "insert into emp values  'jay','trainee')";      String insertEmp2 = "insert into emp values     jayes','trainee')";      String insertEmp3 = "insert into emp values     shail','trainee')";      con.setAutoCommit(false);      stmt.addBatch(insertEmp1);       stmt.addBatch(insertEmp二、       stmt.addBatch(insertEmp3);      ResultSet rs = stmt.executeQuery("select * from emp");      rs.last();      System.out.println("rows before batch execution= "        rs.getRow();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;stmt.executeBatch();      con.commit();      System.out.println("Batch executed");      rs = stmt.executeQuery("select * from emp");      rs.last();      System.out.println("rows after batch execution= "        rs.getRow();结果:    以上代码示例将产生以下结果。结果可能会有所不同。
$ l6 }( p8 p. \# R9 P. jrows before batch execution= 6Batch executedrows after batch execution= = 9来源:执行多个SQL语句
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则