Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question saving Array values in to Table column

    Hi,

    I am trying to save array values to database columns. The array will have 6 values and DB have columns with ID1, ID2, upto ID6. Each array value has to be saved in different column. I wrote below code and getting errors. Any help

    error : Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ')'.

    PHP Code:
        String val[]={"1","2","4","5","6","5"};
              
             for (
    int i=1i<=val.lengthi++) {
                
    String sql_fields "ID"+i;
                
    String sql_values val[i];
                
            
             
    String sql =  "INSERT INTO TEST"sql_fields +"VALUES (?)";
                     
            
    PreparedStatement pst conn.prepareStatement(sql);
            
    pst.setString(1sql_values);
             
            
    pst.execute();
            } 

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I don't know how that conn.prepareStatement works, but the exception is happening with the SQL statement. Throw a break point on that pst.execute(); and see what the value of the SQL statement is- post that. I am assuming it is messing that up

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Edit: You need the parenthesis for the column too

    my guess is that your statement looks like this
    Code:
    INSERT INTO TESTID1VALUES (1)
    there is no space in your string that you build, so that would error out. Also you have the 1 as a string... Assuming that your SQL filed is varchar you need to have the tiks
    Code:
    INSERT INTO TEST ("+ sql_fields +") VALUES ('?')
    would result in
    Code:
    INSERT INTO TEST (ID1) VALUES ('1')
    Last edited by alykins; 12-30-2013 at 05:00 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

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