在以下情况下如何使用准备好的语句?
我有以下 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)
这是我的 java 代码;
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();
}
}
}
但是当我尝试运行代码时,它会抛出以下异常
java.sql.SQLException: 参数索引超出范围(3 > 参数数量,即 2)。 在com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2474) 在 com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3191) 在 PSMTTest.main(PSMTTest.java:45)
我的用户 ID 是主键和自动增量字段,因此它的值是由数据库自动生成的。 对于名字和姓氏字段,我有值。 但对于日期,我想添加数据库的当前数据和时间。
如果地址尚未提供,因此应保留原样
请告诉我的朋友,我的代码中的查询有什么问题? 为什么要例外? 请各位朋友指导一下! 谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息是不言自明的 - 您只指定了两个参数占位符(问号),但您正在尝试使用索引 2 和 3。因此:
应该是:
事实上,它们是值列表无关紧要 - 它们是第一个和第二个参数。
不过,您可以通过重新排序 SQL 来使对应关系更加清晰:
现在,第一个和第二个准备好的语句参数与 SQL 中指定的第一个和第二个字段相对应。
我不清楚您是否需要指定
userid
字段,请注意 - 鉴于默认情况下会提供该字段。老实说,我会把它从 SQL 中完全删除。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:
should be:
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:
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.