Results 1 to 6 of 6
  1. #1
    tarkowc is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default 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?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Do not cobble together statements like this. See the JDBC tutorials for PreparedStatement.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    And why escape single quotes?

  4. #4
    tarkowc is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    This:
    Java Code:
    String somestring = "select * from mytable where name = 'somename'";
    compiles, so you don't escape single quotes. Something esle was causing that problem.

    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)

  6. #6
    tarkowc is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default The suggestion helped a little

    The suggestion helped a little. I had to do some reading up on SQL commands and try the queries in Access to finally get this to work. Now I understand and am well on my way to creating a pretty cool program. Thanks for the help.

Similar Threads

  1. Update table in database
    By CTheSky in forum JDBC
    Replies: 3
    Last Post: 01-30-2011, 04:18 AM
  2. Replies: 1
    Last Post: 01-10-2011, 01:44 AM
  3. how can i update my table with Jtextfield filtering?
    By sungirl in forum Advanced Java
    Replies: 4
    Last Post: 12-14-2010, 04:28 PM
  4. Replies: 13
    Last Post: 10-30-2010, 04:01 PM
  5. Replies: 2
    Last Post: 07-28-2010, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •