college
package c01.s01.t09.bean;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Time;
/**
* 功能:
* 作者:
* 日期:2022年06月10日
*/
public class College {
private int id;
private String username;
private String password;
private String telephone;
private Time register_time;
private String president;
private String name;
private ResultSet startTime;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Time getRegister_time() { return register_time; }
public void setRegister_time(Time register_time) { this.register_time = register_time; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + register_time +
'}';
}
public void setPresident(String president) {
this.president = president;
}
public String getPresident() {
return president;
}
public void setName(String name) {
this.name = name;
}
public void setStartTime(Date start_time) {
}
public void setEmail(String email) {
}
public void setAddress(String address) {
}
public void setProfile(String profile) {
}
public String getName() {
return null;
}
public ResultSet getStartTime() {
return startTime;
}
public String getEmail() {
return email;
}
public String getProfile() {
return null;
}
}
status
package c01.s01.t09.bean;
/**
* 功能:
* 作者:
* 日期:2022年06月10日
*/
public class Status {
private int id;
private String college;
private String version;
private String author;
private String telephone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCollege() {
return college;
}
public void setCollege(String username) {
this.college = college;
}
public String getVersion() {
return version;
}
public void setVersion(String password) {
this.version = version;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) { this.author = author;}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAdress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) { this.email = email; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + college + '\'' +
", password='" + version + '\'' +
", telephone='" + author + '\'' +
", telephone='" + telephone + '\'' +
", telephone='" + address + '\'' +
", registerTime=" + email +
'}';
}
public String getAddress() {
return null;
}
}
student
package c01.s01.t09.bean;
/**
* 功能:
* 作者:
* 日期:2022年06月10日
*/
public class Student {
private int id;
private String name;
private String sex;
private int age;
private String department;
private String t_class ;
private String telephone;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() { return sex ; }
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getT_class() { return t_class ; }
public void setT_class(String t_class) {
this.sex = t_class;
}
public String getTelephone() { return telephone ; }
public void setTelephone(String telephone) {this.telephone = telephone; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + name + '\'' +
", username='" + sex + '\'' +
", username='" + age + '\'' +
", username='" + department + '\'' +
", username='" + t_class + '\'' +
", username='" + telephone + '\'' +
'}';
}
}
user
package c01.s01.t09.bean;
import java.util.Date;
/**
* 功能:用户实体类
* 作者:
* 日期:
*/
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
'}';
}
}
t09.dao.impl(CollegeDaoImpl, StatusDaoImpl, StudentDaoImpl, UserDaoImpl)
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.Status;
import c01.s01.t09.dao.StatusDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 功能:状态数据访问接口实现类
* 作者:
* 日期:
*/
public class StatusDaoImpl implements StatusDao{
@Override
public Status findById(int id) {
// 声明状态对象
Status status = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "select * from t_status where id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化状态
status = new Status();
// 利用当前记录字段值去设置状态对象的属性
status.setId(rs.getInt("id"));
status.setCollege(rs.getString("college"));
status.setVersion(rs.getString("version"));
status.setAuthor(rs.getString("author"));
status.setTelephone(rs.getString("telephone"));
status.setAddress(rs.getString("address"));
status.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回状态对象
return status;
}
@Override
public int update(Status status) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "update t_status set college = ?, version = ?, author = ?,"
+ " telephone = ?, address = ?, email = ? where id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, status.getCollege());
pstmt.setString(2, status.getVersion());
pstmt.setString(3, status.getAuthor());
pstmt.setString(4, status.getTelephone());
pstmt.setString(5, status.getAddress());
pstmt.setString(6, status.getEmail());
pstmt.setInt(7, status.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}`
``
```java
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.Status;
import c01.s01.t09.dao.StatusDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 功能:状态数据访问接口实现类
* 作者:
* 日期:
*/
public class StatusDaoImpl implements StatusDao{
@Override
public Status findById(int id) {
// 声明状态对象
Status status = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "select * from t_status where id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化状态
status = new Status();
// 利用当前记录字段值去设置状态对象的属性
status.setId(rs.getInt("id"));
status.setCollege(rs.getString("college"));
status.setVersion(rs.getString("version"));
status.setAuthor(rs.getString("author"));
status.setTelephone(rs.getString("telephone"));
status.setAddress(rs.getString("address"));
status.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回状态对象
return status;
}
@Override
public int update(Status status) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "update t_status set college = ?, version = ?, author = ?,"
+ " telephone = ?, address = ?, email = ? where id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, status.getCollege());
pstmt.setString(2, status.getVersion());
pstmt.setString(3, status.getAuthor());
pstmt.setString(4, status.getTelephone());
pstmt.setString(5, status.getAddress());
pstmt.setString(6, status.getEmail());
pstmt.setInt(7, status.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.Student;
import c01.s01.t09.dao.StudentDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
* 功能:学生数据访问接口实现类
* 作者:
* 日期:
*/
public class StudentDaoImpl implements StudentDao {
/**
* 插入学生记录
*
* @param student
* @return 插入记录数
*/
@Override
public int insert(Student student) {
//定义插入记录数
int count = 0;
//1、获得数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "insert into t_student (id,name,sex,age,department,class,telephone)"
+ "values(?,?,?,?,?,?,?)";
try {
// 3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4、设置占位符的值
pstmt.setInt(1,student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3,student.getSex());
pstmt.setInt(4,student.getAge());
pstmt.setString(5,student.getDepartment());
pstmt.setString(6,student.getT_class());
pstmt.setString(7,student.getTelephone());
//5、执行SQL,返回插入记录数
count = pstmt.executeUpdate();
//6、关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
/**
* 按学号删除学生记录
*
* @param id
* @return 删除记录数
*/
@Override
public int deleteById(String id) {
//定义删除记录数
int count = 0;
//1、获得数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "delete from t_student where id = ?";
try {
// 3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4、设置占位符的值
pstmt.setString(1,id);
//5、执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//6、关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回删除记录数
return count;
}
/**
* 按班级删除学生记录
*
* @param clazz
* @return 删除记录数
*/
@Override
public int deleteByClass(String clazz) {
//定义删除记录数
int count = 0;
//1、获得数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "delete from t_student where class = ?";
try {
// 3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4、设置占位符的值
pstmt.setString(1,clazz);
//5、执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//6、关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回删除记录数
return count;
}
/**
* 按系部删除学生记录
*
* @param department
* @return 删除记录数
*/
@Override
public int deleteByDepartment(String department) {
//定义删除记录数
int count = 0;
//1、获得数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "delete from t_student where department = ?";
try {
// 3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4、设置占位符的值
pstmt.setString(1,department);
//5、执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//6、关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回删除记录数
return count;
}
/**
* 更新学生记录
*
* @param student
* @return 更新记录数
*/
@Override
public int update(Student student) {
//定义更新记录数
int count = 0;
//1、获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "update t_student set name = ?,sex = ?, age = ?,"
+"department = ?,class = ?,telephone = ? where id = ?";
try {
//3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4、设置占位符的值
pstmt.setString(1,student.getName());
pstmt.setString(2,student.getSex());
pstmt.setInt(3,student.getAge());
pstmt.setString(4,student.getDepartment());
pstmt.setString(5,student.getT_class());
pstmt.setString(6,student.getTelephone());
pstmt.setInt(7,student.getId());
//5、执行SQL,返回更新记录数
count = pstmt.executeUpdate();
//6、关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
return count;
}
/**
* 按学号查询学生记录
*
* @param id
* @return 学生实体
*/
@Override
public Student findById(String id) {
//声明学生对象
Student student = null;
//1、获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "select * from t_student where id = ?";
try {
//3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4、设置占位符的值
pstmt.setString(1,id);
//5、执行SQL,返回更新记录数
ResultSet rs = pstmt.executeQuery();
//6、判断结果集是否有记录
if (rs.next()){
//创建学生实体
student = new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生对象
return student;
}
/**
* 按姓名查询学生记录
*
* @param name
* @return 学生列表
*/
@Override
public List<Student> findByName(String name) {
//声明学生列表
List<Student> students = new ArrayList<Student>();
//1、获取数据库连接
Connection conn = ConnectionManager.getConnection();
//2、定义SQL字符串
String strSQL = "select * from t_student where name like ?";
try {
//3、创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4、设置占位符的值
pstmt.setString(1,name + "%");
//5、执行SQL,返回更新记录数
ResultSet rs = pstmt.executeQuery();
//6、遍历结果集
while (rs.next()){
//创建学生实体
Student student = new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
//将实体添加到学生列表
students.add(student);
}
//7、关闭结果集
rs.close();
//8、关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
/**
* 按班级查询学生记录
*
* @param clazz
* @return 学生列表
*/
@Override
public List<Student> findByClass(String clazz) {
// 声明学生列表
List<Student> students = new ArrayList<Student>();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_student where class like ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, clazz + "%");
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
* 按系部查询学生记录
*
* @param department
* @return 学生列表
*/
@Override
public List<Student> findByDepartment(String department) {
// 声明学生列表
List<Student> students = new ArrayList<Student>();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_student where department like ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, department + "%");
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
* 查询全部学生记录
*
* @return 学生列表
*/
@Override
public List<Student> findAll() {
// 声明学生列表
List<Student> students = new ArrayList<Student>();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_student";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 6. 关闭结果集
rs.close();
// 7. 关闭语句对象
stmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
* 按性别统计学生人数
*
* @return 统计结果向量
*/
@Override
public Vector findRowsBySex() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select sex as '性别', count(*) as '人数'"
+ " from t_student group by sex order by sex desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector<String> currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("性别"));
currentRow.addElement(rs.getInt("人数") + "");
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
/**
* 按班级统计学生人数
*
* @return 统计结果向量
*/
@Override
public Vector findRowsByClass() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select class as '班级', count(*) as '人数'"
+ " from t_student group by class order by class desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector<String> currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("班级"));
currentRow.addElement(rs.getInt("人数") + "");
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
/**
* 按系部统计学生人数
*
* @return 统计结果向量
*/
@Override
public Vector findRowsByDepartment() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select department as '系部', count(*) as '人数'"
+ " from t_student group by department order by department desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector<String> currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("系部"));
currentRow.addElement(rs.getInt("人数") + "");
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
}
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.User;
import c01.s01.t09.dao.UserDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 功能:用户数据访问接口实现类
* 作者:
* 日期:
*/
public class UserDaoImpl implements UserDao{
/**
* 插入用户记录
*
* @param user
* @return 插入记录数
*/
@Override
public int insert(User user) {
// 定义插入记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "insert into t_user (username, password, telephone, register_time)"
+ " values (?, ?, ?, ?)";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3,user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
// 5. 执行SQL,返回插入记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回插入记录数
return count;
}
@Override
public int delete(int id) {
return 0;
}
/**
* 按id删除用户记录
*
* @param id
* @return 删除记录数
*/
@Override
public int deleteById(int id) {
// 定义删除记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "delete from t_user where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setInt(1, id);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
* 更新用户记录
*
* @param user
* @return 更新记录数
*/
@Override
public int update(User user) {
// 定义更新记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "update t_user set username = ?, password = ?, telephone = ?,"
+ " register_time = ? where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getId());
// 5. 执行SQL,返回更新记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
* 按id查询用户
*
* @param id
* @return 用户实体
*/
@Override
public User findById(int id) {
// 声明用户对象
User user = null;
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_user where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setInt(1, id);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 创建用户实体
user = new User();
// 利用当前记录各字段值设置用户实体属性
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户对象
return user;
}
/**
* 查询所有用户
*
* @return 用户列表
*/
@Override
public List<User> findAll() {
// 声明用户列表
List<User> users = new ArrayList<User>();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_user";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 创建用户实体
User user = new User();
// 利用当前记录各字段值设置用户实体属性
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
// 将实体添加到用户列表
users.add(user);
}
// 6. 关闭结果集
rs.close();
// 7. 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户列表
return users;
}
/**
* 用户登录
*
* @param username
* @param password
* @return 登录用户实体
*/
@Override
public User login(String username, String password) {
// 声明用户对象
User user = null;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "select * from t_user where username = ? and password = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, username);
pstmt.setString(2, password);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 实例化用户
user = new User();
// 利用当前记录各字段值设置用户实体属性
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户对象
return user;
}
}
c09.dao(CollegeDao, StatusDao, StudentDao, UserDao)
package c01.s01.t09.dao;
import c01.s01.t09.bean.College;
/**
* 功能:学校数据访问接口
* 作者:
* 日期:
*/
public interface CollegeDao {
College findById(int id);
int update(College college);
}
package c01.s01.t09.dao;
import c01.s01.t09.bean.Status;
/**
* 功能:状态数据访问接口
* 作者:
* 日期:
*/
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
package c01.s01.t09.dao;
import c01.s01.t09.bean.Student;
import java.util.List;
import java.util.Vector;
/**
* 功能:学生数据访问接口
* 作者:
* 日期:
*/
public interface StudentDao {
int insert(Student student);
int deleteById(String id);
int deleteByClass(String clazz);
int deleteByDepartment(String department);
int update(Student student);
Student findById(String id);
List<Student> findByName(String name);
List<Student> findByClass(String clazz);
List<Student> findByDepartment(String department);
List<Student> findAll();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
package c01.s01.t09.dao;
import c01.s01.t09.bean.User;
import java.util.List;
/**
* 功能:用户数据访问接口
* 作者:
* 日期:
*/
public interface UserDao {
int insert(User user);
int delete(int id);
int deleteById(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
c09.dbutils
package c01.s01.t09.dbutils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 功能:数据库连接管理类
* 作者:
* 日期:
*/
public class ConnectionManager {
// 数据库连接属性
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "123456"; // 改成自己数据库的密码
/**
* 私有化构造方法,拒绝实例化
*/
private ConnectionManager() {
}
/**
* 获取数据库连接静态方法
*
* @return 数据库连接
*/
public static Connection getConnection() {
// 定义数据库连接
Connection conn = null;
try {
// 安装数据库驱动
Class.forName(DRIVER);
// 获取数据库连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
// 提示用户数据库连接成功
System.out.println("提示:数据库连接成功~");
} catch (ClassNotFoundException e) {
System.err.println("异常:数据库驱动程序未找到!");
} catch (SQLException e) {
System.err.println("异常:数据库连接失败!");
}
// 返回数据库连接
return conn;
}
/**
* 关闭数据库连接静态方法
*
* @param conn
*/
public static void closeConnection(Connection conn) {
// 判断连接是否为空
if (conn != null) {
try {
// 判断连接是否关闭
if (!conn.isClosed()) {
// 关闭数据库连接,释放资源
conn.close();
// 提示用户
System.out.println("提示:数据库连接关闭~");
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
/**
* 主方法测试数据库连接
*
* @param args
*/
public static void main(String[] args) {
// 获取数据库连接
Connection conn = getConnection();
// 关闭数据库连接
closeConnection(conn);
}
}
c09.gui.(AddFrame, ChangeFrame, DeleteFrame, LogonFrame, MainFrame, RegisterFrame, SeleteFrame, Start)
package c01.s01.t09.gui;
/**
* 功能:
* 作者:
* 日期:
*/
import c03.SQLHelp;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class AddFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddFrame frame = new AddFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddFrame() {
// 和前面类似
setResizable(false);
setTitle("添加学生");
// 这个地方,添加学生只是这个程序的一部分,所以当关闭这部分的时候,程序不直接退出,只是关闭该部分程序
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel TitleLabel = new JLabel("请输入新学生的信息:");
TitleLabel.setFont(new Font("宋体", Font.BOLD, 20));
TitleLabel.setBounds(71, 34, 220, 45);
panel.add(TitleLabel);
JLabel idLabel = new JLabel("学号:");
idLabel.setFont(new Font("宋体", Font.PLAIN, 16));
idLabel.setBounds(71, 105, 50, 30);
panel.add(idLabel);
idField = new JTextField();
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
nameLabel.setBounds(71, 160, 50, 30);
panel.add(nameLabel);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel genderLabel = new JLabel("性别:");
genderLabel.setFont(new Font("宋体", Font.PLAIN, 16));
genderLabel.setBounds(71, 215, 50, 30);
panel.add(genderLabel);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel dobLabel = new JLabel("出生日期:");
dobLabel.setFont(new Font("宋体", Font.PLAIN, 16));
dobLabel.setBounds(41, 270, 80, 30);
panel.add(dobLabel);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel batchLabel = new JLabel("班级:");
batchLabel.setFont(new Font("宋体", Font.PLAIN, 16));
batchLabel.setBounds(71, 325, 50, 30);
panel.add(batchLabel);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
JButton addButton = new JButton("添加");
// 添加鼠标监听事件
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 先取出数据
// getText() 返回字符串类型的
// Integer.parseInt 数据类型转换
int id = Integer.parseInt(idField.getText());
String nameString = nameField.getText();
String genderString = genderField.getText();
String dobfieldString = dobField.getText();
int batch = Integer.parseInt(batchField.getText());
// 输出一下学生的信息,方便修改
System.out.println(id + "\t" + nameString + "\t" + genderString + "\t" + dobfieldString + "\t" + batch);
// SQLHelp 是自己写的一个工具类
SQLHelp sqlHelp = new SQLHelp();
try {
// 调用添加学生信息的方法
sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch);
// 弹出对话框,提示用户添加成功
JOptionPane.showMessageDialog(AddFrame.this, "添加成功!");
} catch (SQLException e1) {
// 这里捕获一下异常,因为学生的学号是唯一的,所以当学号已经存在的时候,提示用户,该学生信息已存在
// 异常代码为 23000
if(e1.getSQLState().equals("23000")) {
JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在");
}
e1.printStackTrace();
}
}
});
addButton.setFont(new Font("宋体", Font.PLAIN, 18));
addButton.setBounds(182, 389, 97, 33);
panel.add(addButton);
}
}
package c01.s01.t09.gui;
/**
* 功能:
* 作者:
* 日期:2022年06月11日
*/
import c03.SQLHelp;
import c03.Student;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class ChangeFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
private JTextField searchField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeFrame frame = new ChangeFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChangeFrame() {
setResizable(false);
setTitle("修改信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(71, 0, 208, 29);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(71, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1.setBounds(71, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
panel.add(lblNewLabel_1_1_1_1);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
JButton changeButton = new JButton("修改");
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int id = Integer.parseInt(searchField.getText());
String nameString = nameField.getText();
String gendeString = genderField.getText();
String dobString = dobField.getText();
String batch = batchField.getText();
SQLHelp sqlHelp = new SQLHelp();
try {
sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch);
JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");
e.printStackTrace();
}
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(182, 389, 97, 33);
panel.add(changeButton);
searchField = new JTextField();
searchField.setBounds(71, 39, 208, 45);
panel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("查找");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int id = Integer.parseInt(searchField.getText());
try {
SQLHelp sqlHelp = new SQLHelp();
Student student = sqlHelp.queryStudent(id);
if(student != null) {
// id 是直接读入的,所以就不需要从数据库中读出了
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());
dobField.setText(student.getDob());
batchField.setText(String.valueOf(student.getBatch()));
} else {
JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(289, 42, 97, 39);
panel.add(searchButton);
}
}
package c01.s01.t09.gui;
/**
• 功能:
• 作者:
• 日期:2022年06月11日
*/import c03.SQLHelp;
import c03.Student;import javax.swing.;
import javax.swing.border.EmptyBorder;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;class QueryFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
private JTextField searchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
QueryFrame frame = new QueryFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public QueryFrame() {
setResizable(false);
setTitle("查询信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(71, 0, 208, 29);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(71, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1.setBounds(71, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
panel.add(lblNewLabel_1_1_1_1);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
searchField = new JTextField();
searchField.setBounds(71, 39, 208, 45);
panel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("查找");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// 1.读出要查询的学生输入的id
// 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据
// 转化一下数据类型
int id = Integer.parseInt(searchField.getText());
// 2.执行JDBC语句
try {
SQLHelp sqlHelp = new SQLHelp();
Student student = sqlHelp.queryStudent(id);
// 3.将查询结果填到文本框中
// 前提是学生存在
if(student != null) {
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());//性别
dobField.setText(student.getDob());//出生日期
batchField.setText(String.valueOf(student.getBatch()));//班级
} else {
// 此时学生不存在,提示用户,不存在该学生
JOptionPane.showMessageDialog(QueryFrame.this, "无此学生");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(289, 42, 97, 39);
panel.add(searchButton);
}
}
```java
package c01.s01.t09.gui;
import c01.s01.t09.bean.User;
import c01.s01.t09.service.UserService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
/**
* 功能:用户登录窗口
* 作者:
* 日期:
*/
public class LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JPanel panel, panel1, panel2, panel3;
/**
* 有参构造方法
*
* @param title
*/
public LoginFrame(String title) {
super(title);
initGUI(); // 调用初始化图形用户界面方法
eventsHandling();
}
/**
* 事件处理
*/
private void eventsHandling() {
// 给【确定】按钮注册监听器
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的用户名和密码
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
// 创建用户服务对象
UserService userService = new UserService() {
@Override
public User login(String username, String password) {
return null;
}
@Override
public int deleteUserById(int i) {
return 0;
}
@Override
public User findUserById(int i) {
return null;
}
@Override
public void updateUser(User user) {
}
@Override
public List<User> findAllUsers() {
return null;
}
};
// 调用服务对象的登录方法
User user;
user = userService.login(username, password);
// 判断用户登录是否成功
if (user != null) {
// 弹出消息框提示用户
JOptionPane.showMessageDialog(null, "恭喜,[" + username + "]登录成功~");
dispose(); // 关闭登录窗口
} else {
// 弹出消息框提示用户
JOptionPane.showMessageDialog(null, "遗憾,用户名或密码错误~");
// 清空两个文本框
txtUsername.setText("");
txtPassword.setText("");
// 让姓名文本框获取焦点
txtUsername.requestFocus();
}
}
});
// 给【取消】按钮注册监听器
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0); // 退出应用程序
}
});
}
/**
* 初始化图形用户界面方法
*/
private void initGUI() {
// 实例化组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
// 将控件添加到三个小面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
// 设置主面板为三行一列的网格布局
panel.setLayout(new GridLayout(3, 1));
// 将三个小面板依次添加到主面板
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
// 设置按钮热键字母
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
// 设置密码框回显字符
txtPassword.setEchoChar('*');
// 设置窗口大小
setSize(250, 200);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
package c01.s01.t09.gui;
/**
* 功能:
* 作者:
* 日期:2022年06月10日
*/
import c03.SelectFrame;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class MainFrame extends JFrame {
private JPanel contentPane;
// // 有了main函数才可以单独运行 取消注释可以单独测试该文件
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// MainFrame frame = new MainFrame();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
public MainFrame() {
// 设置大小不可改变
setResizable(false);
// 设置标题
setTitle("学生管理系统");
// 由于这是主页面,所有当主页面关闭的时候,程序就直接退出
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(int x,int y,int width,int height);
// x 和 y 是窗口打开时的位置
// width 和 height 是窗口打宽度和高度
setBounds(100, 100, 450, 500);
// setLocationRelativeTo(c)
// 设置窗口相对于 c 的位置,当 c 为空或者 null 时,默认为是相对于屏幕中央
setLocationRelativeTo(null);
// 实例化一个 pane
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("学生管理系统");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 25));
lblNewLabel.setBounds(140, 10, 163, 44);
contentPane.add(lblNewLabel);
JButton addButton = new JButton("添加学生");
// addActionListener 注册监听器
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 设置新窗口可见
new AddFrame().setVisible(true);
}
});
addButton.setFont(new Font("宋体", Font.PLAIN, 18));
addButton.setBounds(167, 64, 114, 37);
contentPane.add(addButton);
JButton changeButton = new JButton("修改信息");
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 同上
new ChangeFrame().setVisible(true);
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(167, 121, 114, 37);
contentPane.add(changeButton);
JButton deleteButton = new JButton("删除学生");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 同上
new DeleteFrame().setVisible(true);
}
});
deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));
deleteButton.setBounds(167, 180, 114, 37);
contentPane.add(deleteButton);
JButton queryButton = new JButton("查询信息");
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 同上
new QueryFrame().setVisible(true);
}
});
queryButton.setFont(new Font("宋体", Font.PLAIN, 18));
queryButton.setBounds(167, 240, 114, 37);
contentPane.add(queryButton);
JButton selectButton = new JButton("查询所有学生记录");
selectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
SelectFrame s = new SelectFrame();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
selectButton.setBounds(134,300,180,37);
selectButton.setFont(new Font("宋体", Font.PLAIN, 18));
contentPane.add(selectButton);
}
private class DeleteFrame {
private boolean visible;
public void setVisible(boolean visible) {
this.visible = visible;
}在这里插入代码片
public boolean getVisible() {
return visible;
}
}
}
package c01.s01.t09.gui;
/**
* 功能:
* 作者:
* 日期:2022年06月11日
*/
import c03.SQLHelp;
import c03.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class Register extends JFrame {
JLabel username;
JLabel password;
JLabel passwordAgain;
JTextField usernameFile;
JPasswordField passwordFile;
JPasswordField PasswordFileAgain;
// FlowLayout flowLayout;
JButton sign;
JPanel p;
JPanel p1;
// private Connection connection = null;//初始化conn~
// private PreparedStatement pStatement = null;//初始化stat~
// private ResultSet rSet = null;//初始化结果
final int WIDTH = 410;
final int HEIGHT = 610;
// flowLayout = new FlowLayout(FlowLayout.CENTER);
public Register() {
init();//调用init方法
setResizable(false);//这是不可变窗口
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置窗口关闭默认关闭方式
validate();
setVisible(true);//可显示
Toolkit kit = Toolkit.getDefaultToolkit();//获取屏幕宽度
Dimension screenSize = kit.getScreenSize();//初始化变量
int width = screenSize.width;//这边都是设置大小的,当然后面我老师i给我讲的时候发现根本不需要
int height = screenSize.height;
int x = (width - WIDTH) / 2;
int y = (height - HEIGHT) / 2;
setBounds(x, y, WIDTH, HEIGHT);//设置主窗口位置和大小
setTitle("账号注册");//设置主窗口的标题
}
void init() {
// p.setBorder(BorderFactory.createTitledBorder("基本信息处理"));
// p.setPreferredSize(new Dimension(WIDTH, HEIGHT));
// p.setLayout(new FlowLayout(FlowLayout.CENTER));
// p.setBounds(0,0,WIDTH,HEIGHT);
// p.setLayout(null);
// p.setOpaque(false);
p = new JPanel();//初始化窗口p
p.setLayout(null);//设置布局为空
p.setVisible(true);//设置可见
p1 = new JPanel();//这里因为不知道什么原因如果不重新声明一个Panle那么我的内容显示不出来
// p1.setLayout(null);
p1.setVisible(true);//设置可见
p1.setLayout(null);//设置恐怖剧
p1.setBounds(0, 0, 400, 600);//设置位置和大小
username = new JLabel("账号:");//这里全是定义所需要的元素
password = new JLabel("密码:");
passwordAgain = new JLabel("确认密码:");
usernameFile = new JTextField(20);//这里是设置长度
passwordFile = new JPasswordField(20);//注意TextFile和PasswordFile在后续获取的时候不一样
PasswordFileAgain = new JPasswordField(20);
passwordFile.setEchoChar('*');//这个是输入密码的填充
PasswordFileAgain.setEchoChar('*');
sign = new JButton("注册");//设置注册按钮
//这里开始设置元素位置和大小
sign.setBounds(160, 300, 100, 50);
username.setBounds(130, 150, 40, 20);
password.setBounds(130, 200, 40, 20);
passwordAgain.setBounds(130, 250, 120, 20);
usernameFile.setBounds(200, 150, 100, 20);
passwordFile.setBounds(200, 200, 100, 20);
PasswordFileAgain.setBounds(200, 250, 100, 20);
//将元素逐层添加
p1.add(username);
p1.add(password);
p1.add(passwordAgain);
p1.add(usernameFile);
p1.add(passwordFile);
p1.add(PasswordFileAgain);
p1.add(sign);
p.add(p1);
this.add(p);
//这里我们为注册按钮添加了监听事件
sign.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strusername = usernameFile.getText();//获取usernameFile的内容
String strpassword = new String(passwordFile.getPassword());//在这里我们获取usernameFile和passwordFile方式就不一样了,看好
String strpasswordAgain = new String(PasswordFileAgain.getPassword());
SQLHelp sqlHelp = new SQLHelp();//这里我们调用SQLHelp文件 我在那里写了注册所需的模块
try {
User users = sqlHelp.userpassweord(strusername); //这里我们先接收看看能不能接收到数据
if (users == null) {//没数据说明没有账户可以注册
if (strpassword.equals(strpasswordAgain)) {
System.out.println("注册成功");//判断是否进入
String username = strusername;//当然这里也可以不用在初始化一次,因为上面可以直接使用
String password = strpassword;
sqlHelp.addManeger(username,password);//这里就是向SQLHelp文件里卖弄的addManager模块传入数据
JOptionPane.showMessageDialog(null, "注册成功", "点击确定后返回登录界面", JOptionPane.INFORMATION_MESSAGE);
//这里是两次密码不同
} else {
System.out.println("注册失败");
JOptionPane.showMessageDialog(null, "注册失败,请检查两次密码是否相同", "登录失败提示窗口", JOptionPane.INFORMATION_MESSAGE);
}
//这里是账号重复
}else{
System.out.println("已有账号无法注册");//控制台提示信息
JOptionPane.showMessageDialog(null,"注册失败了,已拥有该账号,请检查密码是否输入错误,目前不支持找回密码","账号已有窗口",JOptionPane.INFORMATION_MESSAGE);
// dispose();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
}
//这里是单独测试Register文件的时候用的
public static void main(String[] args) {
new Register();
}
}
package c01.s01.t09.gui;
/**
* 功能:
* 作者:
* 日期:2022年06月11日
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
public class SeleteFrame extends JFrame {
public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.
public static final String DB_URL = "jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问好之后的东西都是一些去重连接中可能会报错的地方
public static final String username = "root"; //数据库账号
public static final String password = "111111";//数据库密码
//创建窗口
public SeleteFrame() throws SQLException {
// JTable resultarea = new JTable();
Connection connection=null;//初始化conn
connection = DriverManager.getConnection(DB_URL,username,password);
PreparedStatement preparedStatement=connection.prepareStatement("select *from student");//sql语句编译预处理
ResultSet res=preparedStatement.executeQuery();//获取ResultSet结果集
res.last();//游标移动到查询到的数据库数据记录的最后一条
int row=res.getRow();//获取记录的总条数
res.beforeFirst();//游标回到第一个记录前的位置
String arr[]= {"姓名","性别","出生日期","班级"};//定义表格的列名称
String comm[][] = new String[row][4];//row行,4列
res.next();//游标回到第一个记录的位置
for(int i=0;i<row;i++) {
comm[i][0]=res.getString("name");
comm[i][1]=res.getString("sex");
comm[i][2]=res.getString("birthday");
comm[i][3]=res.getString("gender");
res.next();
}
JTable jTable=new JTable(comm,arr);
jTable.setPreferredScrollableViewportSize(new Dimension(800,200));//设置可滚动视图的大小
JScrollPane jScrollPane=new JScrollPane(jTable);
add(jScrollPane,BorderLayout.CENTER);//将滚动条放到窗体
// setDefaultCloseOperation(EXIT_ON_CLOSE);//这个是关闭全部程序
setVisible(true);//设置窗体可见
validate();//设置组件可见
// setLocationRelativeTo(null);
this.setBounds(550,400,800,200);
pack();//自动调整组建大小使每个组键都能显示
connection.close();
//设置程序关闭方式,
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(null, "关闭该窗口?","学生管理", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
dispose();
} else {
try {
new SeleteFrame();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
});
}
}
package c01.s01.t09.gui;
import c03.LoginStar;
/**
* 功能:
* 作者:
* 日期:2022年06月11日
*/
public class Start {
public static void main(String[] args) {
LoginStar l = new LoginStar();
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.College;
import c01.s01.t09.dao.CollegeDao;
import c01.s01.t09.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 功能:测试学校数据访问接口实现类
* 作者:
* 日期:
*/
public class TestCollegeDaoImpl {
@Before
public void beforeTest(){
System.out.println("准备工作开始了!");
}
@Test
public void testFindById() {
CollegeDao dao = new CollegeDaoImpl();
College college = dao.findById(1);
System.out.println(college);
}
@Test
public void testUpdate(){
CollegeDao dao = new CollegeDaoImpl();
College college = dao.findById(1);
college.setPresident("王洪礼");
dao.update(college);
college = dao.findById(1);
System.out.println(college);
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.College;
import c01.s01.t09.service.CollegeService;
import c01.s01.t09.service.impl.CollegeServiceImpl;
import org.junit.Test;
public class TestCollegeServiceImpl {
@Test
public void testFindCollegeById(){
CollegeService service = (CollegeService) new CollegeServiceImpl();
College college = service.findCollegeById(1);
System.out.println(college);
}
@Test
public void testUpdateCollege(){
CollegeService service = (CollegeService) new CollegeServiceImpl();
College college = service.findCollegeById(1);
college.setPresident("王洪礼");
college.setTelephone("3152639");
int count = service.updateCollege(college);
if (count > 0){
System.out.println("恭喜,学校记录更新成功!" + college);
}else {
System.out.println("遗憾,学校记录更新失败!");
}
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Status;
import c01.s01.t09.dao.StatusDao;
import c01.s01.t09.dao.impl.StatusDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestStatusDaoImpl {
@Before
public void beforeTest(){
System.out.println("准备工作开始了!");
}
@Test
public void testFindById() {
StatusDao dao = (StatusDao) new StatusDaoImpl();
Status college = dao.findById(1);
System.out.println(college);
}
@Test
public void testUpdate(){
StatusDao dao = (StatusDao) new StatusDaoImpl();
Status status = dao.findById(1);
status.setAuthor("王洪礼");
dao.update(status);
status = dao.findById(1);
System.out.println(status);
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Status;
import org.junit.Test;
public class TestStatusServiceImpl {
@Test
public void testFindStatusById(){
TestStatusServiceImpl service = new TestStatusServiceImpl();
Status status = service.findStatusById(1);
System.out.println(status);
}
private Status findStatusById(int i) {
return null;
}
@Test
public void testUpdateStatus(){
TestStatusServiceImpl service = new TestStatusServiceImpl();
Status status = service.findStatusById(1);
status.setAuthor("巍巍妹纸");
status.setTelephone("181210000000");
int count = service.updateStatus(status);
if (count > 0){
System.out.println("恭喜,状态记录更新成功!" );
status = service.findStatusById(1);
System.out.println(status);
}else {
System.out.println("遗憾,状态记录更新失败!");
}
}
private int updateStatus(Status status) {
return 0;
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Student;
import c01.s01.t09.dao.StudentDao;
import c01.s01.t09.dao.impl.StudentDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
* 功能:测试学生数据访问接口实现类
* 作者:
* 日期:
*/
public class TestStudentDaoImpl {
@Before
public void beforeTest() {
System.out.println("准备工作开始了!");
}
@Test
public void testInsert() {
Student student = new Student();
student.setId(18101001);
student.setName("小可爱");
student.setSex("女");
student.setAge(19);
student.setDepartment("信息工程系");
student.setT_class("18大数据1班");
student.setTelephone("15890674568");
StudentDao dao = new StudentDaoImpl();
int count = dao.insert(student);
if (count > 0) {
System.out.println("恭喜,学生记录插入成功!");
} else {
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testDeleteById() {
StudentDao dao = new StudentDaoImpl();
String id = "18101001";
int count = dao.deleteById(id);
if (count > 0) {
System.out.println("恭喜,学生记录删除成功!");
} else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteByClass() {
StudentDao dao = new StudentDaoImpl();
String clazz = "10英教1班";
int count = dao.deleteByClass(clazz);
if (count > 0) {
System.out.println("恭喜,[" + clazz + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + clazz + "]学生记录删除失败!");
}
}
@Test
public void testDeleteByDepartment() {
StudentDao dao = new StudentDaoImpl();
String department = "信息工程系";
int count = dao.deleteByDepartment(department);
if (count > 0) {
System.out.println("恭喜,[" + department + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + department + "]学生记录删除失败!");
}
}
@Test
public void testUpdate() {
StudentDao dao = new StudentDaoImpl();
Student student = dao.findById("10080301");
student.setName("圆圆");
int count = dao.update(student);
if (count > 0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
@Test
public void testFindById() {
StudentDao dao = new StudentDaoImpl();
Student student = dao.findById("10080301");
System.out.println(student);
}
@Test
public void testFindByName() {
StudentDao dao = new StudentDaoImpl();
String name = "李";
List<Student> students = dao.findByName(name);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("温馨提示:查无此人!");
}
}
@Test
public void testFindRowsByClass() {
StudentDao dao = new StudentDaoImpl();
String clazz = "10英教1班";
List<Student> students = dao.findByClass(clazz);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("温馨提示:查无此班!");
}
}
@Test
public void testFindByDepartment() {
StudentDao dao = new StudentDaoImpl();
String department = "外语系";
List<Student> students = dao.findByDepartment(department);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("温馨提示:查无此系!");
}
}
@Test
public void testFindAll() {
StudentDao dao = new StudentDaoImpl();
String all;
List<Student> students = dao.findAll();
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testRowsFindBySex() {
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testRowsFindByClass(){
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment() {
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Student;
import c01.s01.t09.service.StudentService;
import c01.s01.t09.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TestStudentServiceImpl {
@Test
public void testDeleteStudentById() {
StudentService service = (StudentService) new StudentServiceImpl();
String id = "18101001";
int count = service.deleteStudentById(id);
if (count > 0) {
System.out.println("恭喜,学生记录删除成功!");
} else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteStudentClass() {
StudentService service = (StudentService) new StudentServiceImpl();
String clazz = "10英教1班";
int count = service.deleteStudentsByClass(clazz);
if (count > 0) {
System.out.println("恭喜,[" + clazz + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + clazz + "]学生记录删除失败!");
}
}
@Test
public void testDeleteStudentDepartment() {
StudentService service = (StudentService) new StudentServiceImpl();
String department = "信息工程系";
int count = service.deleteStudentsByDepartment(department);
if (count > 0) {
System.out.println("恭喜,[" + department + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + department + "]学生记录删除失败!");
}
}
@Test
public void testupdateStudent() {
StudentService service = (StudentService) new StudentServiceImpl();
Student student = service.findStudentById("10080301");
int count = service.updateStudent(student);
if (count > 0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
@Test
public void testStudentById() {
StudentService service = (StudentService) new StudentServiceImpl();
Student student = service.findStudentById("10080301");
System.out.println(student);
}
@Test
public void testFindStudentsByName(){
StudentService service = (StudentService) new StudentServiceImpl();
String name = "李";
List<Student> students = service.findStudentsByName(name);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindStudentsByClass(){
StudentService service = (StudentService) new StudentServiceImpl();
String clazz = "15软件1班";
List<Student> students = service.findStudentsByClass(clazz);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindStudentsByDepartment(){
StudentService service = (StudentService) new StudentServiceImpl();
String department = "信息工程系";
List<Student> students = service.findStudentsByDepartment(department);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindAllStudents(){
StudentService service = (StudentService) new StudentServiceImpl();
String all = "信息工程系";
List<Student> students = service.findAllStudents();
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindRowsBySex() {
StudentService service = (StudentService) new StudentServiceImpl();
Vector rows = service.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByClass() {
StudentService service;
service = (StudentService) new StudentServiceImpl();
Vector rows = service.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment() {
StudentService service = (StudentService) new StudentServiceImpl();
Vector rows = service.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.User;
import c01.s01.t09.dao.UserDao;
import c01.s01.t09.dao.impl.UserDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
/**
* 功能:测试用户数据访问接口实现类
* 作者:
* 日期:
*/
public class TestUserDaoImpl {
@Before
public void beforeTest() {
System.out.println("准备工作开始了!");
}
@Test
public void testInsert() {
User user = new User();
user.setUsername("欢欢");
user.setPassword("root");
user.setTelephone("12345678908");
user.setRegisterTime(new Timestamp(new Date().getTime()));
UserDao dao = (UserDao) new UserDaoImpl();
int count = dao.insert(user);
if (count > 0) {
System.out.println("恭喜,用户记录插入成功!");
} else {
System.out.println("遗憾,用户记录插入失败!");
}
}
@Test
public void testDeleteById() {
UserDao dao = (UserDao) new UserDaoImpl();
String id = "1";
int count = dao.deleteById(1);
if (count > 0) {
System.out.println("恭喜,用户记录删除成功!");
} else {
System.out.println("遗憾,用户记录删除失败!");
}
}
@Test
public void testUpdate() {
UserDao dao = (UserDao) new UserDaoImpl();
User user = dao.findById(1);
user = dao.findById(1);
System.out.println(user);
}
@Test
public void testFindById(){
UserDao dao = (UserDao) new UserDaoImpl();
User user = dao.findById(1);
System.out.println(user);
}
@Test
public void testFindAll() {
UserDao dao = (UserDao) new UserDaoImpl();
String all;
List<User> users = dao.findAll();
for (User user : users) {
System.out.println(users);
}
}
@Test
public void testLogin(){
UserDao dao = (UserDao) new UserDaoImpl();
String username,password;
username = "admin";
password = "12345";
User user = dao.login(username,password);
if (user != null){
System.out.println("恭喜,用户和密码输入成功!");
}else {
System.out.println("遗憾,用户和密码输入错误!");
}
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.User;
import c01.s01.t09.service.UserService;
import c01.s01.t09.service.impl.UserServiceImpl;
import org.junit.Test;
import java.util.List;
public class TestUserServiceImpl {
@Test
public void testDeleteUserById() {
UserService service = (UserService) new UserServiceImpl();
String id = "1";
int count = service.deleteUserById(1);
if (count > 0) {
System.out.println("恭喜,用户记录删除成功!");
} else {
System.out.println("遗憾,用户记录删除失败!");
}
}
@Test
public void testUpdateUser() {
UserService service = (UserService) new UserServiceImpl();
User user = service.findUserById(1);
user.setUsername("袁雨晴");
service.updateUser(user);
user = service.findUserById(1);
System.out.println(user);
}
@Test
public void testAllUsers() {
UserService service = (UserService) new UserServiceImpl();
String all;
List<User> users = service.findAllUsers();
for (User user : users) {
System.out.println(users);
}
}
@Test
public void testLogin(){
UserService service = (UserService) new UserServiceImpl();
String username,password;
username = "admin";
password = "12345";
User user = service.login(username,password);
if (user != null){
System.out.println("恭喜,用户和密码输入成功!");
}else {
System.out.println("遗憾,用户和密码输入错误!");
}
}
}