I have a Java program that reads lines from text file. I want to insert read lines into a columns for already available records in a table created in MySQL database.
The table that is already available consists of 4 columns.
col1: pk, and auto increment number
col2: text
col3: int
col4: text
Col4 is the value to be added after reading each line from the text file. I tried to use the following :
Query= "insert into db.table values (?)";
preparedStmt3 = DBConnection.con.prepareStatement(Query);
Then after I read the line from the text file, I do the following;
preparedStmt3.setString (3, line);
The column that I need to insert in is the 4th column. However, I get the following error:
DB_Error:_java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 1).
My Question: How can I insert values in a specific column in MySQL database using Java wihtout affecting any value in the records (all other columns including the pk auto increment values are already inserted and need not to be changed at all).
I do not understand why have you used
3as an argument tosetString()The first argument of that function is the placeholder of parameter – the question mark in the query – this place holder starts from1and you only have a single placeholder.Try,
because you only have a single place holder in your query. If this is not what you seek then rewrite your query. In fact, if you read the exception message, you will realise it:
index out of range (3 > number of parameters, which is 1).
If you wish to insert multiple values, construct your query as:
and then