以下代码有效 - 表已更新(通过在执行代码之前和之后查看表内容在SQL服务器上确认)。
但是,当我在NetBeans和命令行中运行程序时,一旦" rs = stmt.executeQuery(SQLquery); ,CATCH块就会捕获异常。 "被执行。
我知道这一点因为" System.out.println("已成功更新!"); "就在从不执行CATCH块之前。如果我将其移动到" 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 = "Pr1m@f@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 :(得分:2)
函数“stmt.executeUpdate(SQLquery);”返回一个Integer,因为您没有从数据库中检索任何数据。试试这个:
Integer c = stmt.executeUpdate(SQLquery);
Integer值表示已更改的行数。
答案 1 :(得分:1)
查看您正在使用的JRE的PreparedStatement的javadoc。 PreparedStatement.executeUpdate()
返回已更新的行数的int。 Here是PreparedStatement文档的链接。
答案 2 :(得分:1)
executeUpdate返回一个int,而不是结果集。 http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)
int nbUpdatedResult = stmt.executeUpdate(SQLquery);
答案 3 :(得分:0)
executeUpdate方法返回受影响的行数,这就是您遇到此错误的原因。
所以使用这种方法很好。但您不会检索已编辑的值。