0

I have a list of row object wherein each row has some fields something like this

myList= [ shridhar, 1234, 2015-10-30 3:44 , someMessage, 8;
          sahil,4534, 2015-10-29 13:44 , someMessage, 18;
          shubham, 3234, 2015-10-10 12:24 , someMessage, 9]

I want the list data to store it in MySql database with different fields, I'm stuck in how to set the multiple values while querying, as I'm making use of setString() of PreparedStatement class method which takes only two arguments. I have tried something like this:

String query= "INSERT INTO facebooktable(name, id, date, msg, score)" + "VALUES( ?,?,?,?,?)";
             PreparedStatement preStatement= connection.prepareStatement(query);
             for(int index=0; index<=myList.size();index++){
                 rowObj=myList.get(index);
                 preStatement.setString() // allows only two parameters

             }

How do I pass multiple values making use of the PreparedStatement class?

ShridharBavannawar
  • 164
  • 1
  • 3
  • 13
  • You'll need to do that manually for each field, because they are not all strings - some are dates, some are numbers. At least I assume that's how your table is declared in mySQL. It's not so hard to write 5 statements. – RealSkeptic Oct 30 '15 at 10:27
  • You mean to say something like **preStatement.setString(index, rowObj.getName());** **preStatement.setString(index, rowObj.getName());** within the loop? – ShridharBavannawar Oct 30 '15 at 10:43
  • I mean that you should use `setString` for the string parameter (1), `setInt` for the int parameter (2) and so on. But you should give the exact declaration of your list's type, and the definition of the table. I believe this was marked as the wrong duplicate, but I can't say for sure unless you provide more info by editing the question. – RealSkeptic Oct 30 '15 at 11:14

0 Answers0