我有一种方法,我试图将值从文本框保存到数据库中,但有时它会给我错误
Parameter index out of range (5>number of parameter, which is 1)
有些时间java.lang.nullpointerexception
我不明白该怎么办,请帮忙 以下是我尝试插入的方法的代码。
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
try{
if (y1.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel code","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (y2.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel location","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (y3.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel name","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (y4.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter no of rooms","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (img.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel image","Error", JOptionPane.ERROR_MESSAGE);
return;}
String room =gender.getSelectedItem().toString();
pst.setString(5, room);
Statement stmt;
stmt= conn.createStatement();
String sql1="Select HostelName from hostels where HostelName= '" + y3.getText() + "'";
rs=stmt.executeQuery(sql1);
if(rs.next()){
JOptionPane.showMessageDialog( this, "hostel name already exists","Error", JOptionPane.ERROR_MESSAGE);
y3.setText("");
y3.requestDefaultFocus();
return;
}
String sql = "INSERT INTO hostels (`id`, `hloc`, `HostelName`, `RoomNo`, `Capacity`,`image`) VALUES (?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
//for setting values corresponding ?
pst.setInt(1,Integer.parseInt(y1.getText()));
pst.setString(2,y2.getText());
pst.setString(3,y3.getText());
pst.setInt(4,Integer.parseInt(y4.getText()));
pst.setString(6,y5.getText());
pst.setString(7,y6.getText());
pst.executeUpdate();
pst.execute();
refresh();//Update jTable After adding a new record
JOptionPane.showMessageDialog(null, "Record Successfully Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
任何人都可以帮我摆脱这个错误吗?以下是堆栈跟踪现在的任何帮助吗?
java.lang.NullPointerException
at Hostels.saveActionPerformed(Hostels.java:472)
at Hostels.access$700(Hostels.java:28)
at Hostels$8.actionPerformed(Hostels.java:228)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:1)
您实际上从未在准备好的声明中设置任何值。在pst=conn.prepareStatement(sql);
和pst.execute();
之间,您需要pst.setXXX
设置与您问题中?
对应的所有值。
请注意,?
的索引是从1开始,而不是从0开始。