java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
填写了用户名和密码确认后报错:"系统出现异常错误,请检查数据库。系统即将退出!"代码如下:publicvoidregUser(){Stringname=JOptionPa...
填写了用户名和密码确认后报错:"系统出现异常错误,请检查数据库。系统即将退出!"
代码如下:
public void regUser() {
String name = JOptionPane.showInputDialog("请设置用户名:");
JPasswordField pwd_field = new JPasswordField();
JOptionPane.showConfirmDialog(null, pwd_field, "请设置密码", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);// 设置包含密码框的确认框
String pwd = new String(pwd_field.getPassword());
String[] param = new String[2];
param[0] = username;
param[1] = password;
int i = 0;
// 查询users表
String sql = "select * from users";
// 数据库访问
DBUtil db = new DBUtil();
try {
db.getConnection();
ResultSet rs = db.executeQuery(sql, param);
if (rs.next()) {
i = rs.getInt(1);
}
String nSql = "insert into users values('"+ i + "','" + name + "','" + pwd + "', 0, 0)";
int count = db.executeUpdate(nSql, null);
if (count == 1) {
JOptionPane.showMessageDialog(this, "注册成功!", "成功:", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "注册失败!", "失败:", 0);
}
} catch (Exception e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "系统出现异常错误,请检查数据库。系统即将退出!", "错误", 0);
} finally {
db.closeAll();
}
} 展开
代码如下:
public void regUser() {
String name = JOptionPane.showInputDialog("请设置用户名:");
JPasswordField pwd_field = new JPasswordField();
JOptionPane.showConfirmDialog(null, pwd_field, "请设置密码", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);// 设置包含密码框的确认框
String pwd = new String(pwd_field.getPassword());
String[] param = new String[2];
param[0] = username;
param[1] = password;
int i = 0;
// 查询users表
String sql = "select * from users";
// 数据库访问
DBUtil db = new DBUtil();
try {
db.getConnection();
ResultSet rs = db.executeQuery(sql, param);
if (rs.next()) {
i = rs.getInt(1);
}
String nSql = "insert into users values('"+ i + "','" + name + "','" + pwd + "', 0, 0)";
int count = db.executeUpdate(nSql, null);
if (count == 1) {
JOptionPane.showMessageDialog(this, "注册成功!", "成功:", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "注册失败!", "失败:", 0);
}
} catch (Exception e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "系统出现异常错误,请检查数据库。系统即将退出!", "错误", 0);
} finally {
db.closeAll();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询