📄 checkuser.java
字号:
//CheckUser.java
import java.sql.*;
public class CheckUser {
// 连接数据库
public CheckUser() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
System.out.println("连接数据库错误" + e);
}
}
// 判断用户名和密码是否合法
public boolean isValidUser(String username, String password) {
boolean isValid = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String isValidStr = new String("select * from users where Username='" + username + "' and " + "Password='" + password + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 查询所输入的用户名和密码是否存在
rs = stmt.executeQuery(isValidStr);
if (rs.next()) {
isValid = true;
}
} catch (SQLException e) {
System.out.println("查询数据库错误" + e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return isValid;
}
// 获取用户类型
public int getUserType(String username) {
int flag = 1;
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user = "sa";
String pw = "";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String userTypeStr = new String("select * from users where Username='" + username + "'");
try {
con = DriverManager.getConnection(url, user, pw);
stmt = con.createStatement();
// 查询所输入的用户名和密码是否存在
rs = stmt.executeQuery(userTypeStr);
if (rs.next()) {
flag = rs.getInt("flag");
}
} catch (SQLException e) {
System.out.println("查询数据库错误" + e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return flag;
}
// 修改密码
public boolean changepw(String username, String password) {
boolean changed = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
String changepwStr = new String("update users set Password='" + password + "' where Username='" + username + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 修改密码
stmt.executeUpdate(changepwStr);
changed = true;
} catch (SQLException e) {
System.out.println("修改密码" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return changed;
}
// 添加用户
public boolean adduser(String username, String password) {
boolean add = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
String adduserStr = new String("insert into Users values('" + username + "','" + password + "'" + ",'1')");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 添加用户
stmt.executeUpdate(adduserStr);
add = true;
} catch (SQLException e) {
System.out.println("添加用户" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return add;
}
// 删除用户
public boolean deluser(String username) {
boolean del = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
String deluserStr = new String("delete from Users where Username='" + username + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 删除用户
stmt.executeUpdate(deluserStr);
del = true;
} catch (SQLException e) {
System.out.println("删除用户" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return del;
}
public boolean isExist(String username) {
boolean isexist = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user = "sa";
String pw = "";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String isExistStr = new String("select * from users where Username='" + username + "'");
try {
con = DriverManager.getConnection(url, user, pw);
stmt = con.createStatement();
// 查询所输入的用户名
rs = stmt.executeQuery(isExistStr);
if (rs.next()) {
isexist = true;
}
} catch (SQLException e) {
System.out.println("查询数据库错误" + e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return isexist;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -