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

use*_*390 3 java mysql sql sql-server batch-file

我有一个批处理程序,使用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
Run Code Online (Sandbox Code Playgroud)

我想一遍一遍地为for循环中的每个ID做三件事。

1 > I want to see whether the ID is present or not in the database
2 > If ID - present
{ 
2 a > retrieve the 'netppe' information for that particular ID
2 b > retrieve the 'count' information for that same ID
2 c> Update the database with the new 'netppe' and 'count' value for the same ID
} else {
Insert the information for the new ID
}
Run Code Online (Sandbox Code Playgroud)

如何执行所有语句而不必关闭每个ID的连接?JDBC和SQL的新手。任何帮助表示赞赏。

ktm*_*124 5

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

//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
Run Code Online (Sandbox Code Playgroud)

您将必须传递一个Driver实现,而不是接口本身。但是好消息是,从JDBC 4.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

更好的方法是执行批查询。

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);
}
Run Code Online (Sandbox Code Playgroud)

看来您正在进行一轮查询,以“检查”表中是否包含每个ID,但这不是必需的。如果该ID不在表格中,那么您将得到一个空结果集。空结果集没有任何问题。在这种情况下,您的while循环将永远不会运行,因为rs.next()它将返回false。您还可以通过调用来检查其是否为空rs.first()。这种方式不会移动光标。