一.项目介绍
1)需要管理学生信息,包括:学号、姓名、性别、出生日期、政治面貌、家庭住址、
电话、宿舍号
2)实现查询、增、删、改等功能。
二.设计思路
mvc设计模式
四.创建包
dao,model,view,util
将项目导入到idea中;
2正确导入项目后我们来看下包结构:分为
Dad
model. view.util四个顶层包,采用的是mvc的设计模式
3. 第二部需要导入数据库我们用的是
navicat数据库管理软件,非常好用,建议大
家也使用这个
4 首先先新建一个名为db_ student_swing的
数据库,然后双击新建的数据库打开。
.导入项目根目录下的sql文件,导入后刷新
下可以看到数据表。配置项目中数据库连接的用户名密码。,运行登录界面的程序,即LogOnFrm类。下面我们来看看运行截图:下面是登陆界面,登录密码默认是admin,admin, 即用户名密码都是admin。
下面是登录界面,用户名密码都是admin
登录进去后的主界面
先看看班级信息的管理:
班级信息添加:
班级信息列表:
下面是学生信息管理:
添加学生信息:
学生信息列表
选中一个学生信息后可修改:
删除一条记录
下面是代码:
src:
basedataclass
package baseDataClass;
/**
* 班级实体
* @author llq-artisan
*
*/
public class SchoolClass {
private int id; // 编号
private String className; // 班级名称
private String calssDesc; // 备注
public SchoolClass() {
super();
// TODO Auto-generated constructor stub
}
public SchoolClass(String className, String calssDesc) {
super();
this.className = className;
this.calssDesc = calssDesc;
}
public SchoolClass(int id, String className, String calssDesc) {
super();
this.id = id;
this.className = className;
this.calssDesc = calssDesc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getCalssDesc() {
return calssDesc;
}
public void setCalssDesc(String calssDesc) {
this.calssDesc = calssDesc;
}
@Override
public String toString() {
return className;
}
}
student
/**
* 学生实体
* @author llq-artisan
*
*/
public class Student {
private int id; // 编号
private String name; // 姓名
private String sn; // 学号
private String sex; // 性别
private String dept; // 所在院系
private Integer classId; // 班级ID
private String className; // 班级名称
private String address; // 家庭住址
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, String sn, String sex, String dept, Integer calssId, String address) {
super();
this.name = name;
this.sn = sn;
this.sex = sex;
this.dept = dept;
this.classId = calssId;
this.address = address;
}
public Student(int id, String name, String sn, String sex, String dept, Integer calssId, String address) {
super();
this.id = id;
this.name = name;
this.sn = sn;
this.sex = sex;
this.dept = dept;
this.classId = calssId;
this.address = address;
}
public Student(String name, String sn, Integer calssId) {
super();
this.name = name;
this.sn = sn;
this.classId = calssId;
}
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 getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public Integer getClassId() {
return classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
user
package baseDataClass;
/**
* 用户
* @author Administrator
*
*/
public class User {
private int id; //
private String userName; //
private String password; //
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
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;
}
}
package database
mysqldemo
package dataBase;
import java.sql.*;
public class MySQLDemo {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/management?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "London73";
public static void main(String[] args){
Connection conn = null;
PreparedStatement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// 执行查询
System.out.println(" 实例化Statement对象...");
stmt = (PreparedStatement)conn.createStatement();
String sql = "INSERT INTO `t_user` VALUES ('1', 'admin', 'admin');";
stmt.execute(sql);
//sql = "SELE/CT id, name, url FROM websites";
//ResultSet rs = stmt.executeQuery(sql);
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
schoolclassdao
package dataBase;
import java.sql.*;
import baseDataClass.SchoolClass;
import util.StringUtil;
/**
* 班级Dao类
* @author Administrator
*
*/
public class SchoolClassDao {
public int add(Connection con,SchoolClass schoolClass)throws Exception{
String sql="insert into t_school_class values(null,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, schoolClass.getClassName());
System.out.println(schoolClass.getClassName());
pstmt.setString(2, schoolClass.getCalssDesc());
return pstmt.executeUpdate();
}
/**
* 查询班级集合
* @param con
* @param schoolClass
* @return
* @throws Exception
*/
public ResultSet list(Connection con,SchoolClass schoolClass)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_school_class");
if(StringUtil.isNotEmpty(schoolClass.getClassName())){
sb.append(" and className like '%"+schoolClass.getClassName()+"%'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
}
/**
* 删除班级
* @param con
* @param id
* @return
* @throws Exception
*/
public int delete(Connection con,String id)throws Exception{
String sql="delete from t_school_class where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 更新班级
* @param con
* @param schoolClass
* @return
* @throws Exception
*/
public int update(Connection con,SchoolClass schoolClass)throws Exception{
String sql="update t_school_class set className=?,classDesc=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, schoolClass.getClassName());
pstmt.setString(2, schoolClass.getCalssDesc());
pstmt.setInt(3, schoolClass.getId());
return pstmt.executeUpdate();
}
}
studentdao
package dataBase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import baseDataClass.Student;
import util.StringUtil;
/**
* 学生Dao类
* @author Administrator
*
*/
public class StudentDao {
/**
* 学生
* @param con
* @param student
* @return
* @throws Exception
*/
public int add(Connection con,Student student)throws Exception{
String sql="insert into t_student values(null,?,?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSn());
pstmt.setString(3, student.getSex());
pstmt.setString(4, student.getDept());
pstmt.setInt(5, student.getClassId());
pstmt.setString(6, student.getAddress());
return pstmt.executeUpdate();
}
/**
* 学生信息查询
* @param con
* @param student
* @return
* @throws Exception
*/
public ResultSet list(Connection con,Student student)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_student b,t_school_class bt where b.classId=bt.id");
if(StringUtil.isNotEmpty(student.getName())){
sb.append(" and b.name like '%"+student.getName()+"%'");
}
if(StringUtil.isNotEmpty(student.getSn())){
sb.append(" and b.sn like '%"+student.getSn()+"%'");
}
if(student.getClassId()!=null && student.getClassId()!=-1){
sb.append(" and b.classId="+student.getClassId());
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
/**
* 学生信息删除
* @param con
* @param id
* @return
* @throws Exception
*/
public int delete(Connection con,String id)throws Exception{
String sql="delete from t_student where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 学生信息修改
* @param con
* @param student
* @return
* @throws Exception
*/
public int update(Connection con,Student student)throws Exception{
String sql="update t_student set name=?,sn=?,sex=?,dept=?,address=?,classId=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSn());
pstmt.setString(3, student.getSex());
pstmt.setString(4, student.getDept());
pstmt.setString(5, student.getAddress());
pstmt.setInt(6, student.getClassId());
pstmt.setInt(7, student.getId());
return pstmt.executeUpdate();
}
/**
* 指定班级下是否存在学生
* @param con
* @param
* @return
* @throws Exception
*/
public boolean existStudentByclassId(Connection con,String classId)throws Exception{
String sql="select * from t_student where classId=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, classId);
ResultSet rs=pstmt.executeQuery();
return rs.next();
}
}
userdao
package dataBase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import baseDataClass.User;
/**
* 用户Dao类
* @author Administrator
*
*/
public class UserDao {
/**
* 登录验证
* @param con
* @param user
* @return
* @throws Exception
*/
public User login(Connection con,User user)throws Exception{
User resultUser=null;
String sql="select * from t_user where userName=? and password=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
}
maininterface
artisaninte