how to use prepared statement in following case?
I have following database schema for tabel tblpers
Field Type Null Key Default Extra Privileges
userid int(11) NO PRI (NULL) auto_increment
firstname char(10) NO
lastname char(15) NO 开发者_运维技巧 192.168.1.15
sdate datetime NO 0000-00-00 00:00:00
address varchar(100) YES (NULL)
Here is my java code;
public class PSMTTest {
private static String jdbcDriver = "org.gjt.mm.mysql.Driver";
private static String host="localhost";
private static String userName = "root";
private static String password = "rootpass";
public static void main(String args[]){
Connection con=null;
try{
if(jdbcDriver!=null){
Class.forName(jdbcDriver);
String url = "jdbc:mysql://"+host+"/mysql";
con = DriverManager.getConnection(url,userName,password);
String insertQuery="insert into `querytest`.`tblpers` " +
"(`userid`,`firstname`,`lastname`,`sdate`,`address`)values ( NULL,?,?,Now(),NULL);";
System.out.println("insertQuery : "+insertQuery);
PreparedStatement psmt=con.prepareStatement(insertQuery);
psmt.setString(2,"param");
psmt.setString(3,"ganak"); //as per the sql exception error comes here
psmt.executeUpdate();
psmt.close();
}
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}catch(SQLException sqle){
sqle.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
But when I am tries to run the code It throws following exception
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2474) at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3191) at PSMTTest.main(PSMTTest.java:45)
My userid is a primary key and auto increment field so value for it is automatically generated by database. for the fields firstname and lastname I have the values. but in case of date I want to add current data and time of the db.
In case of address the value is not provided yet so it should be kept as it is
Please tell me friends whats wrong with the query in my code? why its giving an exception? please guide me friends! Thank You!
The error message is self-explanatory - you've only specified two parameter placeholders (question marks) but you're trying to use indexes 2 and 3. So this:
psmt.setString(2, "param");
psmt.setString(3, "ganak");
should be:
psmt.setString(1, "param");
psmt.setString(2, "ganak");
The fact that they are the second and third fields within the value list is irrelevant - they're the first and second parameters.
You could make the correspondence clearer by reordering your SQL though:
String insertQuery = "insert into `querytest`.`tblpers` " +
"(`firstname`, `lastname`, `sdate`, `userid`, `address`) values " +
"(?, ?, Now(), NULL, NULL);";
Now the first and second prepared statement parameters correspond with the first and second fields specified in the SQL.
It's not clear to me that you need to specify the userid
field anyway, mind you - given that it's going to be provided by default. I would remove it from the SQL entirely, to be honest.
精彩评论