Results 1 to 6 of 6
Thread: Update field in table
- 02-03-2011, 08:52 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Update field in table
com.microsoft.sqlserver.jdbc.SQLServerDataSource ds = new com.microsoft.sqlserver.jdbc.SQLServerDataSource() ;
ds.setServerName("ServerName");
ds.setDatabaseName("DBName");
ds.setInstanceName("InstanceName");
ds.setUser("user");
ds.setPassword("password");
Connection con = ds.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
while(selectedRS.next()){}
String tempString7 = "UPDATE TOP " + airCardsRequested + " dbo.Assets SET StatusID=\'Reserved\' WHERE Description Like \'%Air Card%\' AND StatusID=\'extra\'";
int tempInt = stmt.executeUpdate(tempString7);
Getting the error: Incorrect Syntax near '1'. Any idea what might be causing it?
- 02-04-2011, 07:42 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Do not cobble together statements like this. See the JDBC tutorials for PreparedStatement.
- 02-04-2011, 08:44 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
And why escape single quotes?
- 02-04-2011, 02:03 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
How do I combine a select with an update? I am doing a select top statement and trying to update data in that selection. What is the syntax?
I thought I had to escape single quotes. I tried it without escaping the single quotes and it won't compile. With the single quotes escaped, it compiles but I get the error in the original post.
- 02-04-2011, 02:23 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
This:
compiles, so you don't escape single quotes. Something esle was causing that problem.Java Code:String somestring = "select * from mytable where name = 'somename'";
Are you using a PreparedStatement yet?
As for the UPDATE:
Java Code:UPDATE my_table SET some_column = 1 WHERE id IN (your select top statement returning only ids)
- 02-10-2011, 07:25 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Update table in database
By CTheSky in forum JDBCReplies: 3Last Post: 01-30-2011, 04:18 AM -
issue with update on table and prepared statement
By hacktorious in forum JDBCReplies: 1Last Post: 01-10-2011, 01:44 AM -
how can i update my table with Jtextfield filtering?
By sungirl in forum Advanced JavaReplies: 4Last Post: 12-14-2010, 04:28 PM -
GUI does not update table when variables are set to datamodel using JButton
By Kenjitsuka in forum New To JavaReplies: 13Last Post: 10-30-2010, 04:01 PM -
How to insert/update Date field in Oracle with java code
By sasi.tati in forum AWT / SwingReplies: 2Last Post: 07-28-2010, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks