【问题标题】:Can I execute two different queries in a single method using jdbc code?我可以使用 jdbc 代码在一个方法中执行两个不同的查询吗?
【发布时间】:2011-08-13 17:46:16
【问题描述】:

我可以使用jdbc 代码在一个方法中执行两个不同的查询吗?

【问题讨论】:

    标签: jdbc


    【解决方案1】:

    试试executeBatch() JDBC Statement 接口的方法。

    import java.sql.*;  
    class Demo{  
       public static void main(String args[])throws Exception 
      {
    
         Class.forName("com.mysql.jdbc.Driver");  
         Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","password");  
         con.setAutoCommit(false);  
    
         Statement stmt=con.createStatement();  
         stmt.addBatch("insert into emp values(001,'abc',40000)");  
         stmt.addBatch("insert into emp values(002,'mni',50000)");  
    
         stmt.executeBatch();//executing the batch  
    
         con.commit();  
         con.close();  
    
    
    }
    }  
    

    【讨论】:

      【解决方案2】:

      是的,2 次更新作为单个语句

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      Connection conn = DriverManager.getConnection("jdbc.url","jdbc.username","jdbc.password");
      
      java.sql.Statement statement = conn.createStatement();
      
      String sqlStr = "update tab set col1= 'X' \n update tab_not_exist set col1='X2'"; 
      
      statemet.execute(sqlStr);
      

      你能详细说明一下,你希望达到什么目标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-09
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 2013-05-16
        • 2022-06-15
        • 1970-01-01
        • 2021-01-04
        相关资源
        最近更新 更多