1.先将学生信息管理系统的思维导图用百度脑图绘制出来
2.创建MySQL数据库以及表
(1)创建student数据库
(2)创建student表
(3)创建user表
(4)创建college表
(5)创建status表
(6)向student表中插入数据
(7)向user表中插入数据
(8)向college表中插入数据
(9)向status表中插入数据
3.新创建IntelliJ IDEA项目
4.新建项目中创建包
(1)向help包中加入帮助文档
(2)向images包中导入图片
(3)向lib包添加连接MySQL数据库的jar包
并作为库添加到项目里(Add as Library…)
5.创建实体类
(1)学校实体类College
package net.wj.student.bean;
import java.util.Date;
/**
* 功能:学校实体
* 作者:王洁
* 日期:2019年6月17日
*/
public class College {
/**
* 学校标识符
*/
private int id;
/**
* 学校名称
*/
private String name;
/**
* 校长
*/
private String president;
/**
* 建校时间
*/
private Date starttime;
/**
* 联系方式
*/
private String phone;
/**
* 电子邮件
*/
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 getStarttime() {
return starttime;
}
public void setStarttime(Date starttime) {
this.starttime = starttime;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
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 + '\'' +
", starttime=" + starttime +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
", profile='" + profile + '\'' +
'}';
}
}
(2)状态实体类Status
package net.wj.student.bean;
/**
* 功能:状态实体
* 作者:王洁
* 日期:2019年6月17日
*/
public class Status {
private int id;
private String college;
private String version;
private String author;
private String telephone;
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 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 + '\'' +
", email='" + email + '\'' +
'}';
}
}
(3)学生实体类Student
package net.wj.student.bean;
/**
* 功能:学生实体
* 作者:王洁
* 日期:2019年6月17日
*/
public class Student {
private String id;
private String name;
private String sex;
private int age;
private String department;
private String clazz;
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 getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
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 + '\'' +
", clazz='" + clazz + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
(4)用户实体类User
package net.wj.student.bean;
/**
* 功能:用户实体
* 作者:王洁
* 日期:2019年6月17日
*/
public class User {
private int id;
private String username;
private String password;
private String telephone;
private String registerTime;
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 String getRegisterTime() {
return registerTime;
}
public void setRegisterTime(String registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime='" + registerTime + '\'' +
'}';
}
}
6.创建包dbutil并创建数据库连接管理类ConnectionManager
package net.wj.student.dbutil;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 功能:数据库连接管理类
* 作者:王洁
* 日期:2019年6月17日
*/
public class ConnectionManger {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/student";
private static final String USERNAME = "root";
private static final String PASSWORD = "1";
private ConnectionManger(){
}
/**
* 获得数据库连接
*
* @return 数据库连接对象
*/
public static Connection getConnection(){
//定义数据库连接
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL +
"?useUnicode=true&characterEncoding=UTF8",USERNAME,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库
*
* @param conn
*/
public static void closeConnection(Connection conn){
//判断数据库连接是否为空
if(conn != null){
//判断数据库连接是否关闭
try {
if (!conn.isClosed()){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 测试数据库连接是否成功
*
* @param args
*/
public static void main(String[] args) {
//获得数据库连接
Connection conn = getConnection();
//判断是否连接成功
if(conn != null){
JOptionPane.showMessageDialog(null,"恭喜,连接数据库成功!");
} else {
JOptionPane.showMessageDialog(null,"遗憾,数据库连接失败!");
}
//关闭数据库连接
closeConnection(conn);
}
}
运行程序,测试数据库连接是否成功:
7.新建包dao并创建数据访问接口
(1)学校数据访问接口CollegeDao
(2)状态数据访问接口StatusDao
(3)学生数据访问接口StudentDao
(4)用户数据访问接口UserDao