我在铁路管理系统上做了一个项目,想要将文本字段和Java表中的值插入到MySQL数据库中。我怎样才能摆脱这个错误?请帮忙!!
int rows = jTable1.getRowCount();
for(int row = 0; row<rows ; row++){
int index=1;
String S_No = (String) jTable1.getValueAt(row, 1);
String Name = (String) jTable1.getValueAt(row, 2);
String Age = (String) jTable1.getValueAt(row, 3);
String Gender = (String) jTable1.getValueAt(row, 4);
String berth_preference = (String) jTable1.getValueAt(row, 5);
String Quota = (String) jTable1.getValueAt(row, 6);
String tno =(String) cb1.getSelectedItem();
String tname =(String) cb2.getSelectedItem();
String clas =(String) cb3.getSelectedItem();
String tic= tick.getText();
String frms=frm.getText();
String tos=to.getText();
String boarding=bp.getText();
String reser=ru.getText();
String jdate=date.getText();
String pnr=l1.getText();
try{
Class.forName("java.sql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/railway","root","ridhi");
st =con.prepareStatement("INSERT INTO pnr(pnr_no,Train_Number,Train_Name,From_m,T_o,Date,Class,Reserved_Upto,Boarding_Pt,no_tickets,S_No,Name,Age,Gender,berth_preference,Quota) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
st.setString(1, S_No);
st.setString(2, Name);
st.setString(3, Age);
st.setString(4, Gender);
st.setString(5, berth_preference);
st.setString(6, Quota);
st.executeUpdate();
index++;
this.setVisible(true);
new payment().setVisible(true);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 6
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
at javax.swing.JTable.convertColumnIndexToModel(JTable.java:2553)
at javax.swing.JTable.getValueAt(JTable.java:2695)
at reservation.reservationActionPerformed(reservation.java:341)
at reservation.access$100(reservation.java:21)
at reservation$2.actionPerformed(reservation.java:245)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
现在我收到了一个新错误:
java.sql.SQLException: Parameter index out of range (0 < 1 ).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3283)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3272)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4108)
at reservation.reservationActionPerformed(reservation.java:358)
at reservation.access$100(reservation.java:21)
at reservation$2.actionPerformed(reservation.java:245)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 0 :(得分:3)
将此更改为此jtable列是基于零索引的。如果您有6列jtable,那么最大列索引是5而不是6。错误是因为这个但是关心行索引2如果你想获得第一行然后{ {1}}不是getValueAt(0, n);
getValueAt(1, n);
您在准备好的陈述中也应该有相同的列数。
String S_No = (String) jTable1.getValueAt(row, 0);
String Name = (String) jTable1.getValueAt(row, 1);
String Age = (String) jTable1.getValueAt(row, 2);
String Gender = (String) jTable1.getValueAt(row, 3);
String berth_preference = (String) jTable1.getValueAt(row, 4);
String Quota = (String) jTable1.getValueAt(row, 5);// 5 not 6
............................................... ............................................
st =con.prepareStatement("INSERT INTO pnr(pnr_no,Train_Number,Train_Name,From_m,T_o,Date,Class,Reserved_Upto,Boarding_Pt,no_tickets,S_No,Name,Age,Gender,berth_preference,Quota) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
你应该有,你错过了列电话号码列车编号
st.setString(1, S_No);
st.setString(2, Name);
st.setString(3, Age);
st.setString(4, Gender);
st.setString(5, berth_preference);
st.setString(6, Quota);
答案 1 :(得分:0)
当显示 ArrayIndexOutOfBoundsException
时:如果我们请求索引为负数,或者大于或等于数组的大小,则 {{1}抛出。
在您的情况下,ArrayIndexOutOfBoundsException
。