Can't update SQL Server DB -
everyone.
i relatively new java (a complete noob), have had experience vb.net.
i trying update row in sql2008 table.
the table name _registry.
when compile code below, fails several errors (listed separately after code). if comment out section says "// , here run issues" catch block, runs fine (except table wasn't updated).
any assistance appreciated!
public static void main(string[] args) { string host = "jdbc:sqlserver://server1\\primrose;databasename=primrose"; string uname = "sa"; string upwd = "p@ssw0rd"; // 2 sql statements using: string querydb = "select * _registry section = 'recids' , key_ = '_folders' , user_ = 'sc_general'"; string updatedb = "update _registry set value recid = 5"; // these 6 columns in 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; // static test value used ensure can update table: // works here until exit while loop: try { connection con = drivermanager.getconnection(host, uname, upwd); statement stmt = con.createstatement(); resultset rs = stmt.executequery(querydb); // keep reading through table until 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 string int newvalue = integer.parseint(getvalue) + 1; // convert string number can add total folders in template newvalue = newvalue + totalfolders; // change total + existing value write db getvalue = integer.tostring(newvalue); } // , here run issues - tried // "rs = stmt.executeupdate(updatedb)" 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( ) ); } }
and here compiler output:
compiling 1 source file 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: following error occurred while executing line: c:\java\dbupdate\nbproject\build-impl.xml:268: compile failed; see compiler error output details. build failed (total time: 0 seconds)
as errors indicate, you're trying use variables may not have been initialized. try initializing them null, or ensuring set them useful value before try read them.
also, sql update query invalid: can't set value
, have set value=
, give value.
Comments
Post a Comment