Results 1 to 12 of 12
  1. #1
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default update arraylist to mysql database

    Hi, below is my code on update data.i use JDBC

    Java Code:
    private void update(ArrayList<String> list, int uid)
     {
        try
           {
    	   Connection con=null;
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    	          			con= DriverManager.getConnection("jdbc:mysql://localhost/t-way","root","");
    
           	   PreparedStatement ps = con.prepareStatement("UPDATE `input` SET `check`=1, `output`=?  WHERE `check`=0 AND `userid`=?");
    		ps.setArray(1,list);
    		ps.setInt(2,uid);
    		ps.executeUpdate();
                    ps.close();
    	        con.close();
    
    	}
    
       catch (Exception e)
    	{
    	    System.err.println ("Cannot connect to database server "+ e.getMessage());
    	 }
    }
    error is about setArray cannot be applied to ArrayList. ps.setArray(1,list);
    i want to send the whole set in arraylist,seems preparedstatement doesnt has arraylist.
    FYI,the variable for output in database is Blob.
    can u please help me to solve this matter.im stuck here..

    thanks in advance.....

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    I presume this is compile time error? See the API...PreparedStatement

    ...where it defines this method to take as a second parameter an Array (Java Platform SE 6) , and not a List

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    What are you trying to do?
    Arrays are normally sent into stored procedures, and not insert statements.

    What do you expect to see in the "output" column for the given user id?

  4. #4
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default

    @Toll
    the example of data in arraylist<string> list= [apple, grape, banana, peach, mango, honey, sugar, salt]

    and i want to move all the data to output field.is this make u clear?

  5. #5
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default

    @doWhile.... yes error when compile

  6. #6
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    I presume this is compile time error? See the API...PreparedStatement

    ...where it defines this method to take as a second parameter an Array (Java Platform SE 6) , and not a List
    i need to double array? list[][]?
    is it possible to convert arraylist to double array?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Quote Originally Posted by sks View Post
    @Toll
    the example of data in arraylist<string> list= [apple, grape, banana, peach, mango, honey, sugar, salt]

    and i want to move all the data to output field.is this make u clear?
    Then convert the array list to a string in the format you expect it to appear in the database, and then use setString().

    I really wouldn't expect setArray() to work the way you seem to want it to.

  8. #8
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default

    if i change to string, can "apple, grape, banana, peach, mango, honey, sugar, salt" enter in the same row of `output` field?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    It's just a String, so no reason (apart from size of your "output" column) for them not to fit.

    This will give you a row in your table like:
    Java Code:
    userid    check    output
    1         1        apple, grape, banana, peach, mango, honey, sugar, salt
    If that's what you need then OK.

  10. #10
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    It's just a String, so no reason (apart from size of your "output" column) for them not to fit.

    This will give you a row in your table like:
    Java Code:
    userid    check    output
    1         1        apple, grape, banana, peach, mango, honey, sugar, salt
    If that's what you need then OK.
    this is totally what i need.ok,'ill try to convert array to string.im a newbie and need to struggle in everything.im not like u guys, everything is new to me and i could take 2 or 3 hours just to display a character in prompt. :(

  11. #11
    sks
    sks is offline Member
    Join Date
    Mar 2011
    Location
    malaysia
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    It's just a String, so no reason (apart from size of your "output" column) for them not to fit.

    This will give you a row in your table like:
    Java Code:
    userid    check    output
    1         1        apple, grape, banana, peach, mango, honey, sugar, salt
    If that's what you need then OK.
    Java Code:
    String listString = "";
         for (String s : list)
                {
    		listString += s + "\t";
    	    }
    theres no error and database updated.seems like solved.but i dont know incoming problem...
    thanks Tolls.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

Similar Threads

  1. Update table in database
    By CTheSky in forum JDBC
    Replies: 3
    Last Post: 01-30-2011, 04:18 AM
  2. Replies: 0
    Last Post: 02-21-2010, 11:51 AM
  3. Mysql/JDBC update query problem
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 02-11-2009, 09:56 PM
  4. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  5. unable to update MYSQL with values from jsp page
    By koushika in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2008, 01:35 AM

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
  •