Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库获取信息,然后在同一代码块中更新(正确语法)_Java_Mysql_Swing_Netbeans - Fatal编程技术网

Java 从数据库获取信息,然后在同一代码块中更新(正确语法)

Java 从数据库获取信息,然后在同一代码块中更新(正确语法),java,mysql,swing,netbeans,Java,Mysql,Swing,Netbeans,我正在尝试从数据库中获取“费用”,并使用它根据用户输入的存款计算新的“费用”,并使用此新的“费用”更新数据库 我收到的错误消息。我不知道为什么如果你能告诉我我做错了什么,那就太好了,谢谢。我不知道是语法还是这段代码完全错了。我是初学者,所以你呢 java.sql.SQLException: No value specified for parameter 1 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError

我正在尝试从数据库中获取“费用”,并使用它根据用户输入的存款计算新的“费用”,并使用此新的“费用”更新数据库

我收到的错误消息。我不知道为什么如果你能告诉我我做错了什么,那就太好了,谢谢。我不知道是语法还是这段代码完全错了。我是初学者,所以你呢

 java.sql.SQLException: No value specified for parameter 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1115)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
at wecare.Fee.PfeeBtnActionPerformed(Fee.java:191)
at wecare.Fee.access$100(Fee.java:20)
at wecare.Fee$2.actionPerformed(Fee.java:67)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
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:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
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:76)
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)
首先:

String sql2 =("update studentinfo set fees =? where studentId ='"+id+"'");
使用PreparedStatement的原因之一是使代码易于阅读和维护,并通过使用参数防止SQL语法错误。您已经在第一条SQL语句中使用了一个参数,那么为什么要在该语句中进行更改呢

您的SQL应该是:

String sql2 =("update studentinfo set fees = ? where studentId = ?);
更易于阅读、维护和理解,无需所有“+…+”等

然后,按照之前的方法设置语句的参数

pst.setDouble(1, nfee );
pst.setInt(2, id);
不会解决您的问题,但要知道您有更一致的可读代码

java.sql.SQLException:没有为参数1指定值

这说明SQL语句中的一个参数无效。那么是哪条语句导致了问题?堆栈跟踪会告诉您。您查找的语句是代码的一部分:

at wecare.Fee.PfeeBtnActionPerformed(Fee.java:191)
现在您知道了该语句是正确的,问题是您可以显示您使用的变量的值。例如:

System.out.println(id);
System.out.println(…);
我猜变量不包含您期望的内容,所以请找出原因,然后解决问题。
这是您在提问之前进行的基本调试。如果您不知道如何解决问题,请告诉我们使用导致问题的语句,也许我们可以建议解决方案。

我找到了一个解决方案,它可能不符合标准,但它可以正常工作,我只剩下几个小时来提交。谢谢您的尝试g来帮我

private void PfeeBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:



    double fee;
    double nfee;
    double inp;
    Connection conn=null;

    try{
      Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
     conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root","Splash7727#" );
     String sql= ("select  fees  from students.studentinfo where studentId = ?");
      PreparedStatement  pst=conn.prepareStatement(sql);
      Integer id=Integer.parseInt(stuIdTxt.getText());
      pst.setInt(1,id);

      ResultSet rs=pst.executeQuery();
      if (rs.next())
      {
          fee=rs.getInt(1);
          inp=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Deposit amount"));
          nfee=fee-inp;



          JOptionPane.showMessageDialog(this, "New balance is:$"+nfee);



      String sql2 =("update studentinfo set fees =? where studentId ='"+id+"'");
      PreparedStatement  pst2=conn.prepareStatement(sql2);







          pst2.setDouble(1,nfee );
          pst2.executeUpdate();
           JOptionPane.showMessageDialog(this, "Record updated");


      }

          else
      {
          JOptionPane.showMessageDialog(this, "Record not found");
      }

            conn.close();
      }catch (Exception e)
              {
                  e.printStackTrace();
              }



}                                       

这是一个怎样的解决方案?我们不知道这里与您发布的原始代码相比发生了什么变化。发布和回答的目的是解释错误并提供解决方案的详细信息。您仍然没有修复第二条SQL语句。
private void PfeeBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:



    double fee;
    double nfee;
    double inp;
    Connection conn=null;

    try{
      Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
     conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root","Splash7727#" );
     String sql= ("select  fees  from students.studentinfo where studentId = ?");
      PreparedStatement  pst=conn.prepareStatement(sql);
      Integer id=Integer.parseInt(stuIdTxt.getText());
      pst.setInt(1,id);

      ResultSet rs=pst.executeQuery();
      if (rs.next())
      {
          fee=rs.getInt(1);
          inp=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Deposit amount"));
          nfee=fee-inp;



          JOptionPane.showMessageDialog(this, "New balance is:$"+nfee);



      String sql2 =("update studentinfo set fees =? where studentId ='"+id+"'");
      PreparedStatement  pst2=conn.prepareStatement(sql2);







          pst2.setDouble(1,nfee );
          pst2.executeUpdate();
           JOptionPane.showMessageDialog(this, "Record updated");


      }

          else
      {
          JOptionPane.showMessageDialog(this, "Record not found");
      }

            conn.close();
      }catch (Exception e)
              {
                  e.printStackTrace();
              }



}