Java 使用netbeans更新mysql数据库中的数据库数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20835992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating database data in mysql database using netbeans
提问by user3145116
I create Computer laboratory Inventory System and i'm going to update the data in the monitor list. I have monitor list in JtabbedPane and it has subtabbedPane name Laboratory A, Laboratory B,Laboratory C, Laboratory D, Laboratory E, Library. When I select item data in SubtabbedPane Laboratory A, the selected item will not place to the JtextFields in the monitor entry and when i'm click the Update button i got error and it pop out message "java.sql.SQLException: No specified for parameter 5"; what this mean...can you help me sir to fix the error.
我创建了计算机实验室库存系统,我将更新监视器列表中的数据。我在 JtabbedPane 中有监视器列表,它有 subtabbedPane 名称实验室 A、实验室 B、实验室 C、实验室 D、实验室 E、图书馆。当我在 SubtabbedPane Laboratory A 中选择项目数据时,所选项目不会放置到监视器条目中的 JtextFields 中,当我单击“更新”按钮时出现错误并弹出消息“java.sql.SQLException:未指定参数 5"; 这是什么意思……先生,您能帮我修复错误吗?
I have combobox which name is monitorlocation that contains a value of Laboratory A,Laboratory B, Laboratory C, Laboratory D, Laboratory E, Library. The monitorlocation is for when i save a data i chose in value of combobox for me to save it in different place because the monitor are separated in several laboratory.
我有一个名为 monitorlocation 的组合框,其中包含实验室 A、实验室 B、实验室 C、实验室 D、实验室 E、图书馆的值。监视器位置用于当我保存我在组合框的值中选择的数据时,将其保存在不同的位置,因为监视器在几个实验室中是分开的。
this is my code for connecting in database:
这是我在数据库中连接的代码:
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) {
}
}
code Updating the xampp table or to the java jtable:
更新 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){
}
}
And for saving the data in database:
并将数据保存在数据库中:
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();
}
}
And for update, I only try to update the values in SubJtabbedPane Laboratory A and this is the code for it:
对于更新,我只尝试更新 SubJtabbedPane Laboratory 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();
}
then it will gave me an error out and that is "java.sql.SQLException: No specified for parameter 5"
然后它会给我一个错误,那就是“java.sql.SQLException:没有为参数5指定”
回答by Suresh Atta
You have a mistake in query of method cmdUpdateActionPerformed()
, you are not setting the parameter for where clause SerialNumber ?
您在查询方法时出错cmdUpdateActionPerformed()
,您没有为 where 子句设置参数 SerialNumber ?
And the reason for the exception "java.sql.SQLException: No specified for parameter 5"
以及异常的原因 "java.sql.SQLException: No specified for parameter 5"
SerialNumber=? ,model=?,brand= ? ,ScreenSize= ? where SerialNumber=? <-- missing param for this last binder
You are setting 4 values for those four params. But not supplying for where clause.
您正在为这四个参数设置 4 个值。但不提供 where 子句。
and also repeated where SerialNumber=?
并且还重复了 where SerialNumber=?
....ScreenSize= ? where SerialNumber=? where SerialNumber=?"
change it to
将其更改为
.... ScreenSize= ? where SerialNumber=?"
Get rid of that extra where.
摆脱那个额外的地方。