Я пытаюсь вставить данные, но это дает мне сообщение об успехе, но не вставлено в базу данных. Я использую 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)
Почему код выдает эту ошибку?