大家。
我相对较新的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)
答案 0 :(得分:3)
如错误所示,您正在尝试使用可能尚未初始化的变量。尝试将它们初始化为null,或者在尝试从它们读取之前确保将它们设置为某个有用的值。
此外,您的SQL更新查询无效:您不能只SET Value
,必须SET Value=
并为其提供一些价值。
答案 1 :(得分:1)
'变量可能尚未初始化'错误意味着它所说的内容。由于这些变量的声明在while循环中,如果没有从数据库返回结果会发生什么?答案:变量不会被初始化,因为while循环的主体从未执行过。尝试将这些变量声明为循环外的默认值,代码应该编译。