Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java执行多个SQL语句?_Java_Mysql_Sql_Sql Server_Batch File - Fatal编程技术网

如何使用Java执行多个SQL语句?

如何使用Java执行多个SQL语句?,java,mysql,sql,sql-server,batch-file,Java,Mysql,Sql,Sql Server,Batch File,我有一个批处理程序,使用JDBC调用数据库。 我正在以“ID”作为主键发送一批100个信息 我想对数据库进行一次调用,然后在关闭连接之前执行多个SQL语句 我张贴部分代码以供参考 // Database credentials String USER = username; String PASS = password; Connection connec = null; Statement stmt = nul

我有一个批处理程序,使用JDBC调用数据库。 我正在以“ID”作为主键发送一批100个信息

我想对数据库进行一次调用,然后在关闭连接之前执行多个SQL语句

我张贴部分代码以供参考

        //  Database credentials
        String USER = username;
        String PASS = password;

        Connection connec = null;
        Statement stmt = null;

        //STEP 2: Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //STEP 3: Open a connection
        System.out.println("Connecting to a selected database...");
        connec = DriverManager.getConnection(DB_URL, USER, PASS);
        System.out.println("Connected database successfully...");

        //STEP 4: Execute a query
        System.out.println("Creating statement...");
        stmt = connec.createStatement();

        // Step 2: To update the database with the new count and the netppe values.

        // Step to know whether the ID is present or not in the database
        for(int i=0;i<identities.length;i++){
        String booleanString = "SELECT 1 FROM cgm_counters WHERE id = "+identities[i];

stmt.execute(booleanString);  
        ResultSet resultSet = stmt.getResultSet(); //result set for records  
        boolean recordFound = resultSet.next(); 
        ResultSet rs = null;
// Retrieve the 'netppe' information.
        if(recordFound){

            String sql =  "SELECT * FROM cgm_counters WHERE id="+identities[i];
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                 //Retrieve by column name
                 double net_ppe  = rs.getDouble("spend");
                 System.out.println("The value of the netppe :"+net_ppe);
            }

}// end of 'If' statement
如何执行所有语句而不必关闭每个ID的连接?
JDBC和SQL的新特性。非常感谢您的帮助。

好的,您的代码有很多问题。首先

//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
您必须传递驱动程序实现,而不是接口本身;但好消息是,从JDBC4.0开始,这段代码就不再需要了。它已经扫描了你的类路径来为你定位驱动程序

其次,并不是每次查询都会关闭连接。代码中没有任何地方可以调用
connec.close()
。JDBC也不会为您关闭连接

第三,您不需要对嵌套for循环执行此操作!那是个糟糕的主意。您对SQL查询和JDBC的概念需要进一步完善。您可以简单地执行以下操作:

for(int i=0;i<identities.length;i++) {
    String sql =  "SELECT * FROM cgm_counters WHERE id="+identities[i];
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()){
        //Retrieve by column name
        double net_ppe  = rs.getDouble("spend");
        System.out.println("The value of the netppe :"+net_ppe);
    }
}

for(int i=0;iwhy您需要关闭每个id的连接?您是否已经拥有不关闭连接的代码?@eis删除了'rs.close()'语句,如果我不关闭连接并执行多个语句,相同的步骤是否有效?@user3188390关闭结果集与关闭数据库连接有什么关系?我将执行并标记,感谢您的建议和帮助。希望如果我确实有一些问题,我会在需要时获得帮助。
for(int i=0;i<identities.length;i++) {
    String sql =  "SELECT * FROM cgm_counters WHERE id="+identities[i];
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()){
        //Retrieve by column name
        double net_ppe  = rs.getDouble("spend");
        System.out.println("The value of the netppe :"+net_ppe);
    }
}
String batch = "(";
for (int i = 0; i < identities.length;i++) {
    if (i < identities.length() - 1) 
        batch += "?, ";
    else 
        batch += "?)"
}

String sql =  "SELECT * FROM cgm_counters WHERE id in " + batch;
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
    double net_ppe  = rs.getDouble("spend");
    System.out.println("The value of the netppe :"+net_ppe);
}