尝试使用NetBeans和mysql数据库在数据库中插入数据

用户2921378

我有一种方法,我试图将文本框中的值保存到数据库中,但有时它给我错误

 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)
B队

您实际上从未在准备好的语句中设置任何值。pst=conn.prepareStatement(sql);之间pst.execute();您需要pst.setXXX设置与?您的问题相对应的所有值

请记住,?索引从1开始,而不是从0开始。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用mysqli和oops创建数据库并在数据库中插入值

来自分类Dev

无法使用json在数据库中插入数据

来自分类Dev

使用Faker库在数据库中插入图像

来自分类Dev

在数据库中插入值

来自分类Dev

在Mysql数据库中插入数据

来自分类Dev

在数据库MySQL中插入格式编号

来自分类Dev

MySQL在数据库中插入日期

来自分类Dev

如何使用nodejs在数据库中插入和更新现有数据?

来自分类Dev

无法使用php在数据库中插入值

来自分类Dev

使用jdbctemplate在数据库中插入地图

来自分类Dev

无法使用php在数据库中插入值

来自分类Dev

使用codeigniter在数据库中插入多行

来自分类Dev

使用servlet在mysql数据库中插入数据

来自分类Dev

无法使用PHP在MySQL数据库中插入数据

来自分类Dev

在数据库中插入外键数据

来自分类Dev

根据关系在数据库中插入数据

来自分类Dev

Yii Rest API在数据库中插入数据

来自分类Dev

问题在数据库中插入数据

来自分类Dev

在数据库-Moodle中逐列插入数据

来自分类Dev

POST未在数据库中插入数据

来自分类Dev

在mysql数据库中插入的重复

来自分类Dev

使用Servlet和JSP在数据库中插入之前如何获得用户确认?

来自分类Dev

使用 Flask 和 SQLAlchemy 在数据库中插入用户提供的值

来自分类Dev

在mysql数据库中插入和获取不同的语言

来自分类Dev

大数据库中的MYSQL慢速插入和更新

来自分类Dev

使用Ajax和PHP插入数据库(mysql)

来自分类Dev

尝试使用PHP将数据插入数据库

来自分类Dev

使用Python在Mysql数据库中插入值

来自分类Dev

在数据库中插入和删除关联数据