使用netbeans更新mysql数据库中的数据库数据

时间:2013-12-30 08:33:33

标签: java mysql sql

我创建了计算机实验室库存系统,我将更新监视器列表中的数据。我在JtabbedPane中有监控列表,它有子标签实验室A,实验室B,实验室C,实验室D,实验室E,图书馆。当我在SubtabbedPane实验室A中选择项目数据时,所选项目将不会放置在监视器条目中的JtextFields中,当我单击“更新”按钮时出现错误并弹出消息“java.sql.SQLException:No specified for参数5“;这是什么意思......你能帮我解决错误。

我有组合框,其名称是监测位置,包含实验室A,实验室B,实验室C,实验室D,实验室E,图书馆的值。监视位置用于保存我在组合框中选择的数据,以便将其保存在不同的位置,因为显示器在几个实验室中分开。

这是我在数据库中连接的代码:

import java.sql.*;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

public class Monitors extends javax.swing.JFrame {

 public Connection con;
PreparedStatement pst=null;

public Monitors() {
    initComponents();
    try {
                      Class.forName("com.mysql.jdbc.Driver");
                    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/computerinventorysystem","root","");
   Update_tableA();
               Update_tableB();//*Update the data of table in xampp to the java jtable*//
                Update_tableC();//*Update the data of table in xampp to the java jtable*//
                Update_tableD();//*Update the data of table in xampp to the java jtable*//
                Update_tableE();//*Update the data of table in xampp to the java jtable*//
                Update_tableLib();//*Update the data of table in xampp to the java jtable*//


    } catch (Exception e) {
    }
    }

代码更新xampp表或java jtable:

 private void Update_tableA(){
try{
String sql = "select * from monitor_la";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
monitor_A.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){
}

}
private void Update_tableB(){
try{
String sql = "select * from monitor_lb";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
monitor_B.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){
}

}
private void Update_tableC(){
try{
String sql = "select * from monitor_lc";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
monitor_C.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){
}

}
private void Update_tableD(){
try{
String sql = "select * from monitor_ld";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
monitor_D.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){
}

}
private void Update_tableE(){
try{
String sql = "select * from monitor_le";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
monitor_E.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){
}

}
private void Update_tableLib(){
try{
String sql = "select * from monitor_library";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
monitor_library.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){
}

}

用于保存数据库中的数据:

   private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
   if (monitorSN.getText().equals("") || monitorM.getText().equals("") ||        monitorB.getText().equals("") || monitorSS.getText().equals("")){
    JOptionPane.showMessageDialog(null,"Required to fill all the boxes!!!");
 } else if (monitorlocation.getSelectedItem().equals("Laboratory A")){
      String sql= "Insert Into monitor_la (SerialNumber,model,brand,ScreenSize) values  (?,?,?,?)";

      try {
                      pst=con.prepareStatement(sql);

       pst.setString(1, monitorSN.getText());
        pst.setString(2, monitorM.getText());
         pst.setString(3, monitorB.getText());
          pst.setString(4, monitorSS.getText());
            pst.execute();
      JOptionPane.showMessageDialog(null,"Data Saved!");
      } catch (Exception e) {
      }Update_tableA();

  }else if (monitorlocation.getSelectedItem().equals("Laboratory B")){
      String sql= "Insert Into monitor_lb (SerialNumber,model,brand,ScreenSize) values (?,?,?,?)";
     try {
                pst=con.prepareStatement(sql);

       pst.setString(1, monitorSN.getText());
        pst.setString(2, monitorM.getText());
         pst.setString(3, monitorB.getText());
          pst.setString(4, monitorSS.getText());

             pst.execute();
             JOptionPane.showMessageDialog(null,"Data Saved!");


     } catch (Exception e) {
     }Update_tableB();
  }else if (monitorlocation.getSelectedItem().equals("Laboratory C")){
      String sql= "Insert Into monitor_lc (SerialNumber,model,brand,ScreenSize) values (?,?,?,?)";
      try {
         pst=con.prepareStatement(sql);

       pst.setString(1, monitorSN.getText());
        pst.setString(2, monitorM.getText());
         pst.setString(3, monitorB.getText());
          pst.setString(4, monitorSS.getText());

             pst.execute();
             JOptionPane.showMessageDialog(null,"Data Saved!");

     } catch (Exception e) {
     }Update_tableC();
 }                                       
 else if (monitorlocation.getSelectedItem().equals("Laboratory D")){
      String sql= "Insert Into monitor_ld (SerialNumber,model,brand,ScreenSize) values  (?,?,?,?)";
      try {
           pst=con.prepareStatement(sql);

       pst.setString(1, monitorSN.getText());
        pst.setString(2, monitorM.getText());
         pst.setString(3, monitorB.getText());
          pst.setString(4, monitorSS.getText());

             pst.execute();
             JOptionPane.showMessageDialog(null,"Data Saved!");
     } catch (Exception e) {
     }Update_tableD();
  }else if (monitorlocation.getSelectedItem().equals("Laboratory E")){
      String sql= "Insert Into monitor_le (SerialNumber,model,brand,ScreenSize) values   (?,?,?,?)";
      try {
            pst=con.prepareStatement(sql);

       pst.setString(1, monitorSN.getText());
        pst.setString(2, monitorM.getText());
         pst.setString(3, monitorB.getText());
          pst.setString(4, monitorSS.getText());

             pst.execute();
             JOptionPane.showMessageDialog(null,"Data Saved!");
     } catch (Exception e) {
     }Update_tableE();
    }else if (monitorlocation.getSelectedItem().equals("Library")){
      String sql= "Insert Into monitor_library (SerialNumber,model,brand,ScreenSize)    values (?,?,?,?)";
      try {
               pst=con.prepareStatement(sql);

       pst.setString(1, monitorSN.getText());
        pst.setString(2, monitorM.getText());
         pst.setString(3, monitorB.getText());
          pst.setString(4, monitorSS.getText());

             pst.executeUpdate();
             JOptionPane.showMessageDialog(null,"Data Saved!");
     } catch (Exception e) {
         JOptionPane.showMessageDialog(null, e);
     }Update_tableLib();
}
}

为了更新,我只尝试更新SubJtabbedPane实验室A中的值,这是它的代码:

private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          

    try {
        String sql = "update monitor_la set SerialNumber=? ,model=?,brand= ? ,ScreenSize= ? where SerialNumber=? where SerialNumber=?";
   pst=con.prepareStatement(sql);
   pst.setString(1, monitorSN.getText());
    pst.setString(2, monitorM.getText());
     pst.setString(3, monitorB.getText());
      pst.setString(4, monitorSS.getText());


   pst.execute();

JOptionPane.showMessageDialog(null,"Updated");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
  Update_tableA();

}                 

那么它会给我一个错误,那就是“java.sql.SQLException:No for parameter for parameter 5”

1 个答案:

答案 0 :(得分:1)

查询方法cmdUpdateActionPerformed()时出错,您没有为where子句SerialNumber ?设置参数

异常"java.sql.SQLException: No specified for parameter 5"

的原因
SerialNumber=? ,model=?,brand= ? ,ScreenSize= ?  where SerialNumber=? <-- missing param for this last binder

您为这四个参数设置了4个值。但不提供where子句。

并重复where SerialNumber=?

....ScreenSize= ? where SerialNumber=? where SerialNumber=?"

将其更改为

.... ScreenSize= ? where SerialNumber=?"

摆脱额外的地方。