【问题标题】:How to execute multiple SQL statement using Java?如何使用 Java 执行多条 SQL 语句?
【发布时间】:2014-04-04 22:29:33
【问题描述】:

我有一个批处理程序,我使用 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

我想为 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
}

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

【问题讨论】:

  • 为什么需要关闭每个 id 的连接?你不是已经有不关闭连接的代码吗?
  • @eis 删除了 'rs.close()' 语句,如果我不关闭连接并执行多个语句,相同的步骤是否有效?
  • @user3188390 关闭结果集与关闭数据库连接有何关系?

标签: java mysql sql sql-server batch-file


【解决方案1】:

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

//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.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);
    }
}

最好是批量查询。

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);
}

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

【讨论】:

  • 我会实施然后标记,感谢您的建议和帮助。希望如果我确实有一些问题,我会在需要时得到帮助。
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多