2013-05-03 39 views
-1

以下代碼工作 - 更新表(通過查看代碼執行前後的表內容,在SQL服務器上進行確認)。更新表時生成異常 - java

但是,當我在NetBeans中並從命令行運行程序時,只要執行「rs = stmt.executeQuery(SQLquery);」,CATCH塊就會捕獲異常。

我知道這是因爲在CATCH塊的執行從未執行之前,系統「System.out.println(」已成功更新!「);」。如果我將它移動到「rs = stmt.executeQuery(SQLquery);」之後,它也不會執行。

異常錯誤是:

run: 
The statement did not return a result set. 
BUILD SUCCESSFUL (total time: 0 seconds) 

我不明白我做了什麼錯。

我也嘗試使用 「RS = stmt.executeUpdate(SqlQuery類);」 而不是 「RS = stmt.executeQuery(SqlQuery類);」,但NetBeans的有紅色感嘆號本筆記,當我徘徊在它:

incompatible types 
    required: ResultSet 
    found: int 

如果我編譯它,NetBeans的吐出了這一點:

C:\Java\Example\src\example\Example.java:56: error: incompatible types 
      rs = stmt.executeUpdate(SQLquery); 
           ^
    required: ResultSet 
    found: int 
1 error 

哎呀。我瘋了! 請幫忙!

package example; 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.PrintWriter; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

class Example 
{ 
    public static void main(String args[]) 
    { 
     String host = "jdbc:sqlserver://SERVER1\\Primrose;databaseName=Primrose"; 
     String uName = "sa"; 
     String uPwd = "[email protected]@ct"; 

     String SQLquery = ""; 

     int totalFolders = 4; 

     int getRecId = 0; 
     String getUser = ""; 
     String getSection = ""; 
     String getKey = ""; 
     String getValue = ""; 
     String getExtraInfo = ""; 

     try 
     { 
      Connection con = DriverManager.getConnection(host, uName, uPwd); 
      Statement stmt = con.createStatement(); 

      SQLquery = "SELECT * FROM _Registry WHERE (Section = 'RecIds' AND Key_ = '_Folders' AND User_ = 'sc_general')"; 

      ResultSet rs = stmt.executeQuery(SQLquery); 

      while (rs.next()) 
      { 
       getRecId = rs.getInt("RecId"); 
       getUser = rs.getString("User_"); 
       getSection = rs.getString("Section"); 
       getKey = rs.getString("Key_"); 
       getValue = rs.getString("Value"); 
       getExtraInfo = rs.getString("ExtraInfo"); 

       getValue = getValue.trim();       // Strip trailing spaces from string 
       int newValue = Integer.parseInt(getValue) + 1;  // Convert string to number so I can add it to total folders 
       newValue = newValue + totalFolders;     // Change to total + existing value to write back to dB 
       getValue = Integer.toString(newValue);    // Convert to string as required by the table 
      } 

      SQLquery = "UPDATE _Registry SET Value=" + getValue + " WHERE (RecId = 5 AND User_ = 'sc_general' AND Section = 'RecIds' AND Key_ = '_FOLDERS')"; 

      rs = stmt.executeQuery(SQLquery); 

      rs.updateInt("RecId", getRecId); 
      rs.updateString("User_", getUser); 
      rs.updateString("Section", getSection); 
      rs.updateString("Key_", getKey); 
      rs.updateString("Value", getValue); 
      rs.updateString("ExtraInfo", getExtraInfo); 
      rs.updateRow(); 

      System.out.println("Updated successfully!"); 
     } 

     catch (SQLException err) 
     { 
      System.out.println(err.getMessage()); 
     } 
    } 
} 

回答

0

executeUpdate方法返回受影響的行數,這就是爲什麼你有這樣的錯誤。

所以這是很好的使用這種方法。但是你不會檢索編輯的值。

1

查看您正在使用的JRE的PreparedStatement的javadocs。 PreparedStatement.executeUpdate()返回已更新的行數的整數。 Here是一個鏈接到PreparedStatement文檔。

2

函數 「stmt.executeUpdate(的SQLQuery);」返回一個Integer,因爲你沒有從數據庫中檢索任何數據。試試這個:

Integer c = stmt.executeUpdate(SQLquery); 

Integer值表示有多少行已被改變。