public boolean JudgeRegister() throws SQLException, ClassNotFoundException { if(this.name.equals("")) { JOptionPane.showMessageDialog(null, "用户名不能为空!", "用户名", JOptionPane.ERROR_MESSAGE); return false; } if(this.ID.equals("")) { JOptionPane.showMessageDialog(null, "账号不能为空!", "账号为空", JOptionPane.ERROR_MESSAGE); return false; } if(this.password.equals("")) { JOptionPane.showMessageDialog(null, "密码不能为空!", "密码为空", JOptionPane.ERROR_MESSAGE); return false; } if(!this.password.equals(this.confirmpassword)) { JOptionPane.showMessageDialog(null, "两次输入的密码不一致!", "密码不一致", JOptionPane.ERROR_MESSAGE); return false; } //符合规则,弹出注册成功的窗口,并将账号添加数据库 JOptionPane.showMessageDialog(null, "注册成功"); addAdmin(); return true; } //向数据库添加Admin账户 void addAdmin() throws ClassNotFoundException, SQLException { String sql="insert into admin (id, name, password) values (?,?,?)"; Class.forName(driver); try { Connection conn = DriverManager.getConnection(url, user, sqlpassword); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, this.ID); ps.setString(2, this.name); ps.setString(3, this.password); int res=ps.executeUpdate(); if (res>0){ System.out.println("成功!"); } ps.close(); conn.close(); }catch(SQLException ex) { System.out.println("添加用户失败!"); ex.printStackTrace(); } } }
时间: 2024-02-14 18:36:15 浏览: 21
这段代码是用来实现注册功能的,主要是判断用户输入的信息是否符合要求,然后将用户的信息添加到数据库中。其中,addAdmin() 方法是用来向数据库中添加 Admin 账户的。这段代码使用了 JDBC 技术来连接数据库,并使用 PreparedStatement 对象来执行 SQL 语句,实现了防 SQL 注入的功能。
相关问题
public boolean find(a02 p){ for(int i = 0;i < name.length;i++){ if(this.name.equals(p.name[i])){ return true; } } }
在你提供的代码片段中,有一个方法名为find,返回类型为boolean,接受一个参数p,类型为a02。该方法的作用是在数组name中查找是否存在与p.name[i]相等的元素,如果存在则返回true,否则返回false。
然而,你的代码存在一些问题。在方法的结尾处,没有返回值。根据你的逻辑,当找到匹配的元素时应该返回true,但是当没有找到匹配的元素时应该返回false。因此,你需要在方法的最后添加一个默认的返回值,例如return false。修正后的代码如下所示:
public boolean find(a02 p){
for(int i = 0;i < name.length;i++){
if(this.name.equals(p.name[i])){
return true;
}
}
return false; // 添加默认的返回值
}
优化代码。 private void check(String name, String password) { boolean result = false; try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("数据库加载失败" + e); } try { String url = "jdbc:mysql://localhost:3306/student"; String user = "root", pwd = "271332"; Connection con; Statement sql; ResultSet rs; con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String mysql = "select*from users " + "where uname='" + name + "'and upwd='" + password + "'"; System.out.println(mysql); rs = sql.executeQuery(mysql); if (rs.next()) { JOptionPane.showMessageDialog(this, "欢迎" + name + "登陆!", "登陆成功", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, "用户名或密码输入错误,请重试!", "登陆失败", JOptionPane.ERROR_MESSAGE); System.out.println(rs.getString("uname").equals(name)); System.out.println(rs.getString("upwd").equals(password)); result = false; } } catch (SQLException ee) { JOptionPane.showMessageDialog(null, "数据读取失败" + ee, "失败", JOptionPane.ERROR_MESSAGE); } }
有几个方面可以优化这段代码:
1.防止 SQL 注入攻击
在这段代码中,SQL 查询语句是通过字符串拼接的方式生成的,这存在 SQL 注入攻击的风险。建议使用 PreparedStatement 来代替 Statement,这样可以使用参数化查询,防止 SQL 注入攻击。
2.使用 try-with-resources
在代码中打开了 Connection、Statement 和 ResultSet,需要手动关闭,这存在资源泄漏的风险。建议使用 try-with-resources,这样可以自动关闭资源。
3.将数据库连接信息配置在配置文件中
在代码中直接硬编码了数据库连接信息,这样不方便修改和维护。建议将数据库连接信息配置在配置文件中,这样可以方便修改和维护。
4.合理处理异常
在代码中,如果发生异常,只是简单地打印了错误信息,没有进行合理的处理。建议使用合适的方式来处理异常,比如抛出异常或者返回错误码。
优化后的代码如下所示:
```
private void check(String name, String password) {
boolean result = false;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String pwd = "271332";
String sql = "select * from users where uname=? and upwd=?";
try (Connection con = DriverManager.getConnection(url, user, pwd);
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, name);
ps.setString(2, password);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
JOptionPane.showMessageDialog(this, "欢迎" + name + "登陆!", "登陆成功", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "用户名或密码输入错误,请重试!", "登陆失败", JOptionPane.ERROR_MESSAGE);
result = false;
}
}
}
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "数据库加载失败" + e, "失败", JOptionPane.ERROR_MESSAGE);
} catch (SQLException ee) {
JOptionPane.showMessageDialog(null, "数据读取失败" + ee, "失败", JOptionPane.ERROR_MESSAGE);
}
}
```