【发布时间】:2020-11-01 14:51:24
【问题描述】:
我正在尝试插入数据,但它给了我成功消息但没有插入到数据库中。我正在使用 netbeans 和 Xampp。我尝试了很多方法,但总是一样。
这是我的代码:
public AddSeat() {
initComponents();
connect();
}
Connection con;
PreparedStatement pst;
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/busticket", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(AddSeat.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(AddSeat.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是插入方法:
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
String busno = txtbus.getSelectedItem().toString();
SimpleDateFormat Date_Format = new SimpleDateFormat("yyyy/mm/dd");
String date = Date_Format.format(txtdate.getDate());
for(int i = 0; i<=30; i++)
{
try {
int seats = i;
String status = "Unbooked";
pst = con.prepareStatement("insert into sat(busno, seats, date, status,) values(?, ? , '?' , '?')");
pst.setString(1, busno);
pst.setInt(2, seats);
pst.setString(3, date);
pst.setString(4, status);
pst.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(AddSeat.class.getName()).log(Level.SEVERE, null, ex);
}
}
JOptionPane.showMessageDialog(this, "Seat Added!");
}
代码抛出了这个异常:
Jul 12, 2020 1:30:37 PM PracticePackage.AddSeat button2ActionPerformed
SEVERE: null
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3385)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3370)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4095)
at PracticePackage.AddSeat.button2ActionPerformed(AddSeat.java:206)
at PracticePackage.AddSeat.access$200(AddSeat.java:20)
at PracticePackage.AddSeat$3.actionPerformed(AddSeat.java:120)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
为什么代码会出现这个错误?
【问题讨论】:
-
不要把问号放在引号里。它不认为它们是那样的参数。
-
使用相关标签。在命令行应用程序中调试它的数据库端。并且不要添加swing 标签。
-
我建议你不要使用
SimpleDateFormat和Date。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time API 的LocalDate和DateTimeFormatter。也不要将日期作为字符串传输到数据库,使用pst.setObject(3, yourLocalDate);传输LocalDate。 -
无论是使用现代的
DateTimeFormatter还是旧的麻烦的SimpleDateFormat,都要注意格式模式字母的大小写。请检查格式模式字符串中小写mm和大写MM的区别。 -
正如所指出的,问题在于您已将问号放在引号中,因此您要插入文字问号,但是由于代码将它们视为文字问号,因此不是更长的期望四个参数,因此错误。通常,在发布到 Stack Overflow 时,您应该在标题中包含实际异常的详细信息(即“参数索引超出范围(3 > 参数数量,即 2)。”)以帮助人们识别它。或者甚至可以先搜索它,这可能会更快地找到答案。
标签: java database parameter-passing prepared-statement sqlexception