gpt4 book ai didi

java - 错误说列数与值计数不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:17 27 4
gpt4 key购买 nike

我正在尝试摆脱这个错误。你介意在这里帮我一下吗?下面是我的代码。

package Viewer;

import Model.JDBC;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class User extends javax.swing.JFrame
{
Model.JDBC j=new JDBC();

public User()
{
initComponents();
eid.grabFocus();
}
@SuppressWarnings("unchecked")

private void saveActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
ResultSet rs = j.getData("select eid from user where eid='" + eid.getText() + "'");
if (rs.next())
{
if (eid.getText().equals(rs.getString(1)))
{
JOptionPane.showMessageDialog(null, "Existing User Name");
pwd.setText("");
cpwd.setText("");
}
}
else
{
if (eid.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter User Name");
eid.grabFocus();
}
else if (pwd.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter Password");
}
else if (pwd.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter Password");
}
else
{
String pass = new String(pwd.getPassword());
String pas1 = new String(cpwd.getPassword());

if (pass.equals(pas1))
{
String ps = new String(pwd.getPassword());
String s = ps;
try
{
j.putData("insert into user values ('" + eid.getText() + "','" + s + "','" + ulevel.getSelectedItem() + "','"+pwd.getText()+"','"+cpwd.getText()+"')");
JOptionPane.showMessageDialog(null,"User Added Successfully.. ");
j.getCon().close();
clear();
}
catch (Exception e)
{
System.out.println(e + "101 User Adding");
}
}
else
{
JOptionPane.showMessageDialog(null,"Password Not Match");

pwd.grabFocus();
pwd.setText("");
cpwd.setText("");
}
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
private void deleteActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
ResultSet rs = j.getData("select * from user where eid='" + eid.getText() + "'");
if (rs.next())
{
if (rs.getString(1).equals(eid.getText()))
{
String a = (pwd.getText());
String b = (cpwd.getText());
if((rs.getString(2).equals(a)) && (rs.getString(2).equals(b)))
{
int t = JOptionPane.showConfirmDialog(null, "Are You Sure...?");
if(t==JOptionPane.YES_OPTION)
{
j.putData("delete from user where eid='"+eid.getText()+"'");
clear();
}
}
else if(!rs.getString(2).equals(a))
{
JOptionPane.showMessageDialog(null, "Password Not Match");
pwd.setText("");
cpwd.setText("");
}
else if(!rs.getString(2).equals(b))
{
JOptionPane.showMessageDialog(null, "Password Not Match");
pwd.setText("");
cpwd.setText("");
}

}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid User Name");
clear();
}

}
catch (Exception ex)
{
ex.printStackTrace();
}

}

private void eidKeyPressed(java.awt.event.KeyEvent evt)
{
if (evt.getKeyCode() == 10)
{
if (eid.getText().isEmpty())
{
//ms.massage("Enter User Name");
eid.grabFocus();
}
ulevel.grabFocus();
}
}

private void ulevelKeyPressed(java.awt.event.KeyEvent evt)
{
if (evt.getKeyCode() == 10)
{
pwd.grabFocus();
}
}

private void pwdKeyPressed(java.awt.event.KeyEvent evt)
{
if (evt.getKeyCode() == 10)
{
if (pwd.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter Password");
}
else
{
cpwd.grabFocus();
}
}
}

private void cpwdKeyPressed(java.awt.event.KeyEvent evt)
{
if (evt.getKeyCode() == 10)
{
String pass = new String(pwd.getPassword());
System.out.println(pass);
String pas1 = new String(cpwd.getPassword());
System.out.println(pas1);
if (pass.equals(pas1))
{
save.grabFocus();

}
else
{
JOptionPane.showMessageDialog(null,"Password Not Match");

pwd.grabFocus();
pwd.setText("");
cpwd.setText("");
}
}
}
private void pwdActionPerformed(java.awt.event.ActionEvent evt)
{}
public void clear()
{
eid.setText("");
ulevel.setSelectedItem("Admin");
pwd.setText("");
cpwd.setText("");
}

public static void main(String args[])
{
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new User().setVisible(true);
}
});
}
private javax.swing.JPasswordField cpwd;
private javax.swing.JButton delete;
private javax.swing.JTextField eid;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField pwd;
private javax.swing.JButton save;
private javax.swing.JButton search;
private javax.swing.JComboBox<String> ulevel;
private javax.swing.JButton update;
}

通过上面的代码,我试图运行一个名为“userregistry”的用户信息获取表单,其中一个名为“eid”的文本字段获取用户名,组合框名称“ulelevel”给​​出用户类型(管理员/用户),以及一个名为“pwd”的密码字段用于提供密码,另一个名为“cpwd”的密码字段用于确认密码。有 4 个按钮,分别命名为保存、搜索、更新、删除来执行上述操作。(搜索和更新的代码不在这里)。

我有使用这种语法创建的 SQL 表

mysql> create table user(eid varchar(45) primary key, ulevel int, pwd varchar(45
), cpwd varchar(45));

我的问题是:当我输入详细信息并单击 netbeans 说的保存按钮时

java.sql.SQLException: Column count doesn't match value count at row 101 User Adding

这可能是什么原因?

我也试过 01

catch (Exception e)
{
System.out.println(e+"01 User adding");
}

在它给我消息的那个特定地方

java.sql.SQLException: Column count doesn't match value count at row 101 User Adding

最佳答案

你得到这个错误是因为你在查询中使用的列数比你的表多,你的表包含 4 列 eid, ulelevel, pwd, cpwd :

 user(eid varchar(45) primary key, ulevel int, pwd varchar(45), cpwd varchar(45))

并且您使用了一个包含 5 列的查询 eid.getText(), s, ulelevel.getSelectedItem(), pwd.getText(), cpwd.getText():

j.putData("insert into user values ('" + eid.getText() + "','" + s + "','" +  
ulevel.getSelectedItem() + "','"+pwd.getText()+"','"+cpwd.getText()+"')");

所以解决这个问题,一切都会好的。

注意

您的方法可能会出现语法错误或导致SQL Injection ,所以改为使用 Statement 使用 PrepapredStatement 帮助更全面、更安全。

祝你好运。

关于java - 错误说列数与值计数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42361781/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com