每个人。
我对 Java 比较陌生(一个完整的菜鸟),但有使用 VB.Net 的经验。
我正在尝试更新 SQL2008 表中的一行。
表名是_Registry。
当我编译下面的代码时,它会失败并出现几个错误(在代码之后单独列出)。如果我注释掉从“ //这里是我遇到问题的地方”到 catch 块的部分,它运行良好(除了表没有更新)。
任何帮助将不胜感激!
public static void main(String[] args)
{
String host = "jdbc:sqlserver://SERVER1\\Primrose;databaseName=Primrose";
String uName = "sa";
String uPwd = "P@ssw0rd";
// The two SQL statements I am using:
String queryDB = "SELECT * FROM _Registry WHERE Section = 'RecIds' AND Key_ = '_Folders' AND User_ = 'sc_general'";
String updateDB = "UPDATE _Registry SET Value WHERE RecID = 5";
// These are the 6 columns in the table named _Registry:
int getRecId; // RecId [int] IDENTITY(1,4)
String getUser; // User_ [char](64)
String getSection; // Section [char](64)
String getKey; // Key_ [char](64)
String getValue; // Value [char](255)
String getExtraInfo; // ExtraInfo [text]
int totalFolders = 10; // This is a static test value that will be used to ensure I can update the table:
// Everything works from here until I exit the WHILE loop:
try
{
Connection con = DriverManager.getConnection(host, uName, uPwd);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(queryDB);
// Keep reading through the table until I get my desired result:
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 in template
newValue = newValue + totalFolders; // Change to total + existing value to write back to dB
getValue = Integer.toString(newValue);
}
// And here is where I run into issues - I even tried
// this "rs = stmt.executeUpdate(updateDB)" but that had no effect:
rs = stmt.executeQuery(updateDB);
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( ) );
}
}
这是编译器输出:
Compiling 1 source file to C:\Java\DBupdate\build\classes
C:\Java\DBupdate\src\dbupdate\DBupdate.java:73: error: variable getRecId might not have been initialized
rs.updateInt( "RecId", getRecId );
^
C:\Java\DBupdate\src\dbupdate\DBupdate.java:74: error: variable getUser might not have been initialized
rs.updateString( "User_", getUser );
^
C:\Java\DBupdate\src\dbupdate\DBupdate.java:75: error: variable getSection might not have been initialized
rs.updateString( "Section", getSection );
^
C:\Java\DBupdate\src\dbupdate\DBupdate.java:76: error: variable getKey might not have been initialized
rs.updateString( "Key_", getKey );
^
C:\Java\DBupdate\src\dbupdate\DBupdate.java:77: error: variable getValue might not have been initialized
rs.updateString( "Value", getValue );
^
C:\Java\DBupdate\src\dbupdate\DBupdate.java:78: error: variable getExtraInfo might not have been initialized
rs.updateString( "ExtraInfo", getExtraInfo );
^
6 errors
C:\Java\DBupdate\nbproject\build-impl.xml:926: The following error occurred while executing this line:
C:\Java\DBupdate\nbproject\build-impl.xml:268: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)