Results 1 to 10 of 10
- 08-08-2010, 03:33 PM #1
Member
- Join Date
- May 2010
- Location
- Florida
- Posts
- 68
- Rep Power
- 0
How to update with dynamic variable?
Hi
We all know that the statement used to update the database using (JDBC with mysql) looks like this:
"assume the table name is NODES and the fields to be updated are value1 and value2"
now assume that I have this variable defined in my java program:PHP Code:statement.executeUpdate("update NODES set value1='101', value2='110' where index='30'");
String var="999";
how can I put it in value1 or value2, I tried to put it using this statement but I get an error:
java.sql.SQLException: Unknown column 'var' in 'field list'PHP Code:statement.executeUpdate("update NODES set value1=var, value2='110' where index='30'");
- 08-08-2010, 03:56 PM #2
When you build the String, you need to exclude the variable name from the String to have its value put into the String:
"....." + var + "...."
- 08-08-2010, 04:23 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
That can be extremely dangerous (sql injection attacks); better use a PreparedStatement. See this.
kind regards,
Jos
- 08-08-2010, 04:52 PM #4
Member
- Join Date
- May 2010
- Location
- Florida
- Posts
- 68
- Rep Power
- 0
Thank you guys very very much
Norm do you mean to put it like this?
JosAH can you please check the link again.PHP Code:statement.executeUpdate("update NODES set value1="+var+", value2='110' where index='30'");
By the way the value of the variable var is taken from a JTextField:
Thank you gain guysPHP Code:JTextField textIn=new JTextField(10); String var=textIn.getText();
Regards.
Anderson.Last edited by mr_anderson; 08-08-2010 at 05:07 PM.
- 08-08-2010, 05:14 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-08-2010, 05:19 PM #6
Not knowing anything about databases, I understood the OPs problem to be how to build a String with the variable contents vs the name of the variable.
I can understand that an unedited String inserted into a command String could have undesired consequences. Its up to the OP to verify the contents of var.
- 08-08-2010, 05:49 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-08-2010, 05:52 PM #8
Sorry, you're not going to educate me on databases. It just runs off like water off a duck's back.
- 08-09-2010, 04:08 AM #9
It has been so long since I have used Statement; I always use PreparedStatement when I do JDBC'ing.
To help out the OP in the quest for building better data accessors, Below is a complete example of using prepared statements for this update query, as well as the 'proper' handling of closing of the connection and statement objects after use.
Java Code:Connection con = null; PreparedStatement stmt = null; try { con = dataSource.getConnection(); // or how ever else you get a connection object. // the first difference when using prepared statements is to specify the query string with con.prepareStatement(). // note, one string, we use ? as place holders where variables will go. stmt = con.prepareStatement("update NODES set value1=?, value2=? where index=?"); // now its important to bind the variables to the ? place holders in the query string above. // for some reason, jdbc uses 1-based indexes. stmt.setInt(1, 101); // see API docs, setString, setDate, set(etc) stmt.setInt(2, 110); stmt.setInt(3, 30); // now we may execute the statement, what we are used to do, note no query string here, as we did this above stmt.executeUpdate(); } catch (SQLException ex) { // error handling here } finally { // close the statement handle now, faster than waiting for garbage collection to get around to it, // and better if working in connection pooled environments if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { // ignore } } // close the connection, required for connection pool environments if (con != null) { try { con.close(); } catch (SQLException ex) { // ignore } } }
- 08-09-2010, 08:40 AM #10
Member
- Join Date
- May 2010
- Location
- Florida
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
Dynamic types for a variable.
By Somelauw in forum New To JavaReplies: 5Last Post: 11-27-2009, 10:38 AM -
Dynamic GUI
By ike2u in forum New To JavaReplies: 4Last Post: 08-08-2009, 02:50 AM -
How do I update a WINDOWS user env variable from my java code ?
By gavman99 in forum Advanced JavaReplies: 0Last Post: 02-06-2008, 02:07 PM -
dynamic update in swt
By sandor in forum SWT / JFaceReplies: 0Last Post: 05-14-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks