You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.3 KiB
170 lines
5.3 KiB
package 学生成绩查询管理系统.JDBC;
|
|
|
|
import javax.swing.*;
|
|
import java.sql.*;
|
|
|
|
public class JDBCUtils {
|
|
|
|
private static Connection conn;
|
|
private static Statement stmt;
|
|
private static final String drivername = "com.mysql.cj.jdbc.Driver";
|
|
private static final String Url = "jdbc:mysql://localhost:3306/studentmanager";
|
|
private static final String username = "root";
|
|
private static final String password = "123456";
|
|
|
|
static {
|
|
try {
|
|
Class.forName(drivername);
|
|
} catch (ClassNotFoundException e) {
|
|
JOptionPane.showMessageDialog(null, "加载数据库驱动出错,请查明原因!");
|
|
}
|
|
try {
|
|
conn = DriverManager.getConnection(Url, username, password);
|
|
stmt = conn.createStatement();
|
|
} catch (SQLException e) {
|
|
JOptionPane.showMessageDialog(null, "数据库驱动出错,用户名或密码错误!");
|
|
}
|
|
}
|
|
|
|
public boolean admincheckLogin(String useraccount, String userpassword) {
|
|
Connection connection = null;
|
|
PreparedStatement statement = null;
|
|
ResultSet resultSet = null;
|
|
|
|
try {
|
|
// 创建数据库连接
|
|
connection = DriverManager.getConnection(Url, username, password);
|
|
|
|
// 编写SQL查询语句
|
|
String sql = "SELECT * FROM userdatabases WHERE account = ? AND password = ?";
|
|
|
|
// 创建预编译的语句对象
|
|
statement = connection.prepareStatement(sql);
|
|
|
|
// 设置参数
|
|
statement.setString(1, useraccount);
|
|
statement.setString(2, userpassword);
|
|
|
|
// 执行查询并获取结果集
|
|
resultSet = statement.executeQuery();
|
|
|
|
// 返回是否存在匹配的记录
|
|
return resultSet.next();
|
|
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
// 关闭数据库资源
|
|
try {
|
|
if (resultSet != null) {
|
|
resultSet.close();
|
|
}
|
|
if (statement != null) {
|
|
statement.close();
|
|
}
|
|
if (connection != null) {
|
|
connection.close();
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
return false; // 出现异常或查询失败时返回false
|
|
}
|
|
|
|
public boolean teachercheckLogin(String useraccount, String userpassword) {
|
|
Connection connection = null;
|
|
PreparedStatement statement = null;
|
|
ResultSet resultSet = null;
|
|
|
|
try {
|
|
// 创建数据库连接
|
|
connection = DriverManager.getConnection(Url, username, password);
|
|
|
|
// 编写SQL查询语句
|
|
String sql = "SELECT * FROM teacheraccount WHERE account = ? AND password = ?";
|
|
|
|
// 创建预编译的语句对象
|
|
statement = connection.prepareStatement(sql);
|
|
|
|
// 设置参数
|
|
statement.setString(1, useraccount);
|
|
statement.setString(2, userpassword);
|
|
|
|
// 执行查询并获取结果集
|
|
resultSet = statement.executeQuery();
|
|
|
|
// 返回是否存在匹配的记录
|
|
return resultSet.next();
|
|
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
// 关闭数据库资源
|
|
try {
|
|
if (resultSet != null) {
|
|
resultSet.close();
|
|
}
|
|
if (statement != null) {
|
|
statement.close();
|
|
}
|
|
if (connection != null) {
|
|
connection.close();
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
return false; // 出现异常或查询失败时返回false
|
|
}
|
|
|
|
public boolean studentcheckLogin(String useraccount, String userpassword) {
|
|
Connection connection = null;
|
|
PreparedStatement statement = null;
|
|
ResultSet resultSet = null;
|
|
|
|
try {
|
|
// 创建数据库连接
|
|
connection = DriverManager.getConnection(Url, username, password);
|
|
|
|
// 编写SQL查询语句
|
|
String sql = "SELECT * FROM studentaccount WHERE account = ? AND password = ?";
|
|
|
|
// 创建预编译的语句对象
|
|
statement = connection.prepareStatement(sql);
|
|
|
|
// 设置参数
|
|
statement.setString(1, useraccount);
|
|
statement.setString(2, userpassword);
|
|
|
|
// 执行查询并获取结果集
|
|
resultSet = statement.executeQuery();
|
|
|
|
// 返回是否存在匹配的记录
|
|
return resultSet.next();
|
|
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
// 关闭数据库资源
|
|
try {
|
|
if (resultSet != null) {
|
|
resultSet.close();
|
|
}
|
|
if (statement != null) {
|
|
statement.close();
|
|
}
|
|
if (connection != null) {
|
|
connection.close();
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
return false; // 出现异常或查询失败时返回false
|
|
}
|
|
}
|