1

I tried to read a file and write the data into database. The file is read to store into the string array, as follows:

 String[] result = new String[numberofToken];

Then I tried to:

Statement stm = conn.createStatement(); 
stm.executeUpdate("insert into login (firstname,lastname,pass, users ) values ('result[0]','result[1]','result[2]','result[3]')");

This just stores the "result[0]"..."result[3]" into the database, not the value of result[0]..result[3].

How do I insert the values?

1 Answer 1

1

You can use prepared statement and loop the array to set values for your insert query:

    PreparedStatment ps = conn.prepareStatement("INSERT INTO login (firstname,lastname,pass, users ) VALUES (?,?,?,?)");
    for(int i = 0; i < result.length; i++)
        ps.setString(i, result[i]);
    ps.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.