这次我们要做学生管理系统,总的来说有这几个操作:系统设置,数据操作,查询学生,人数统计和帮助。系统设置里的方法有:设置学校信息,设置状态栏信息和修改用户密码。数据操作主要就是对我们数据库的增删改查操做的训练,查询学生其实也是对数据库的查询,只是不是查询所以,而是有条件的查询(主要就是改一下sql语句,后面加个where 条件),人数的统计也是对sql语句的一个复习(主要是group by)分类汇总,帮助和关于是每个项目都必不可少的。这就是我对这次做这个系统的提前分析。
在数据库student中创建4个表:student、user、college、status,并插入记录。
新的一天,今天大概做了这样的一些类
bean包里装的是数据库中表的实体类,dao包里的是impl包和数据库中表的接口类,其中impl包里的是数据访问接口实现类,对接口进行实例化。dbutil包里是连接数据库的数据库连接类,test包里是test测试类。
下面展示其中的代码:
首先得写bean包里装的是数据库中表的实体类:
College
package net.lgs.student.bean;
import java.util.Date;
/**
* 功能:创建college实体类
* 作者:刘国淞
* 日期:2019.6.17
*/
public class College {
private int id;
private String name;
private String president;
private Date start_time;
private String telephone;
private String email;
private String address;
private String profile;
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 getPresident() {
return president;
}
public void setPresident(String president) {
this.president = president;
}
public Date getStart_time() {
return start_time;
}
public void setStart_time(Date start_time) {
this.start_time = start_time;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
@Override
public String toString() {
return "College{" +
"id=" + id +
", name='" + name + '\'' +
", president='" + president + '\'' +
", start_time=" + start_time +
", telephone='" + telephone + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
", profile='" + profile + '\'' +
'}';
}
}
Status:
package net.lgs.student.bean;
/**
* 功能:创建status实体类
* 作者:刘国淞
* 日期:2019.6.17
*/
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 college) {
this.college = college;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
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 getAddress() {
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 "Status{" +
"id=" + id +
", college='" + college + '\'' +
", version='" + version + '\'' +
", author='" + author + '\'' +
", telephone='" + telephone + '\'' +
", address='" + address + '\'' +
", email='" + email + '\'' +
'}';
}
}
Student
package net.lgs.student.bean;
/**
* 功能:创建student实体类
* 作者:刘国淞
* 日期:2019.6.17
*/
public class Student {
private String id;
private String name;
private String sex;
private int age;
private String department;
private String class_1;
private String telephone;
public String getId() {
return id;
}
public void setId(String 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 getClass_1() {
return class_1;
}
public void setClass_1(String class_1) {
this.class_1 = class_1;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", department='" + department + '\'' +
", class_1='" + class_1 + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
User:
package net.lgs.student.bean;
import java.util.Date;
/**
* 功能:创建user实体类
* 作者:刘国淞
* 日期:2019.6.17
*/
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date register_time;
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 getRegister_time() {
return register_time;
}
public void setRegister_time(Date register_time) {
this.register_time = register_time;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", register_time=" + register_time +
'}';
}
}
CollegeDao:
package net.lgs.student.dao;
import net.lgs.student.bean.College;
/**
*功能:学校数据访问接口CollegeDao
* 作者:刘国淞
* 日期:2019.1.17
*/
public interface CollegeDao {
College findById(int id);
int update(College college);
}
StatusDao:
package net.lgs.student.dao;
import net.lgs.student.bean.Status;
/**
*功能:学校数据访问接口StatusDao
* 作者:刘国淞
* 日期:2019.1.17
*/
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
StudentDao:
import net.lgs.student.bean.Student;
import java.util.List;
import java.util.Vector;
/**
*功能:学校数据访问接口student
* 作者:刘国淞
* 日期:2019.1.17
*/
public interface StudentDao {
int insert(Student student);
int deleteById(String id);
int deleteByDepartment(String department);
int update(Student student);
Student findById(String id);
List findByName(String name);
List findByClass(String clazz);
List findByDepartment(String department);
List findAll();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
UserDao:
package net.lgs.student.dao;
/**
*功能:学校数据访问接口User
* 作者:刘国淞
* 日期:2019.1.17
*/
import net.lgs.student.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int deleteById(int id);
int update(User user);
User findById(int id);
List dindAll();
User login(String username,String password);
}
ColleageDaoImpl:
package net.lgs.student.dao.impl;
import net.lgs.student.bean.College;
import net.lgs.student.dao.CollegeDao;
import net.lgs.student.dbutil.ConnectionManager;
import java.sql.*;
/**
* 功能:学校数据访问接口实现类
* 作者:刘国淞
* 日期:2019年6月18日
*/
public class ColleageDaoImpl implements CollegeDao{
@Override
public College findById(int id) {
// 声明学校对象
College college = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnetction();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_college WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化学校
college = new College();
// 利用当前记录字段值去设置学校对象的属性
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStart_time(rs.getDate("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回学校对象
return college;
}
@Override
public int update(College college) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnetction();
// 定义SQL字符串
String strSQL = "UPDATE t_college SET name = ?, president = ?, start_time = ?,"
+ " telephone = ?, email = ?, address = ?, profile = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStart_time().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, college.getProfile());
pstmt.setInt(8, college.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
StatusDaoImpl:
package net.lgs.student.dao.impl;
import net.lgs.student.bean.Status;
import net.lgs.student.dao.StatusDao;
import net.lgs.student.dbutil.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.getConnetction();
//定义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.getConnetction();
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;
}
}
TestCollegeDaoImpl:
package net.lgs.student.test;
import net.lgs.student.bean.College;
import net.lgs.student.dao.CollegeDao;
import net.lgs.student.dao.impl.ColleageDaoImpl;
import org.junit.After;
import org.junit.Test;
public class TestCollegeDaoImpl {
@After
public void afterTest(){
System.out.println("单元测试结束了~");
}
@Test
public void testFindById(){
CollegeDao dao = new ColleageDaoImpl();
College college =dao.findById(1);
System.out.println(college);
}
@Test
public void testUpdate(){
CollegeDao dao = new ColleageDaoImpl();
College college = dao.findById(1);
college.setProfile("王洪礼");
int count = dao.update(college);
if(count > 0){
System.out.println("恭喜,学校记录更新成功!");
}else {
System.out.println("遗憾,学校记录更新失败!");
}
}
}