簡體   English   中英

如何使用Java執行多個SQL語句?

[英]How to execute multiple SQL statement using Java?

我有一個批處理程序,我正在使用JDBC來調用數據庫。 我將以100個批次發送信息,並以“ID”作為主鍵。

我想對數據庫進行一次調用,然后在關閉連接之前執行多個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新手。 任何幫助表示贊賞。

好吧,你的代碼有很多問題。 首先,

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

你必須傳遞一個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()來檢查它是否為空。 這種方式不會移動光標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM