(1)创建学校实体类College
package net.lyy.student.bean;
import java.util.Date;
/**
-
功能:学校实体类
-
作者:李远燕
-
日期:2019年6月26日
*/
public class College {
private int id;
private String name; //学校名称
private String president; //校长
private Date startTime; //建校时间
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 getStartTime() {
return startTime;
}public void setStartTime(Date startTime) {
this.startTime = startTime;
}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 + ‘’’ +
“, startTime=” + startTime +
“, telephone=’” + telephone + ‘’’ +
“, email=’” + email + ‘’’ +
“, address=’” + address + ‘’’ +
“, profile=’” + profile + ‘’’ +
‘}’;
}
}
(2)状态实体Status
package net.lyy.student.bean;
/**
-
功能:Status实体类
-
作者:李远燕
-
日期:2019年6月26日
*/
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 + ‘’’ +
‘}’;
}
}
(3)学生实体类Student
package net.lyy.student.bean;
/**
-
功能:Student实体类
-
作者:李远燕
-
日期:2019年6月26日
*/
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.lyy.student.bean;
import java.util.Date;
/**
-
功能:User实体类
-
作者:李远燕
-
日期:2019年6月26日
*/
public class User {
private int id; //用户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 +
‘}’;
}
}
(5)数据库连接管理类ConnectionManager
这里创建项目需要输入的全称是net.lyy.student.dbutil
因为是做完才截图所以就直接给你们看项目了。
package net.lyy.student.dbutil;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.SplittableRandom;
/**
-
功能:数据库连接管理类
-
提供获取连接与关闭连接的静态方法
-
作者:李远燕
-
日期:2019年6月26日
/
public class ConnectionManager {
/*- 数据库驱动程序
/
private static final String DRIVER = “com.mysql.jdbc.Driver”;
/* - 数据库统一资源标识符
/
private static final String URL = “jdbc:mysql://localhost:3306/student”;
/* - 数据库用户
/
private static final String USER = “root”;
/* - 数据库密码
*/
private static final String PASSWORD = “1”;
/**
- 私有化构造方法,拒绝实例化
*/
private ConnectionManager(){
}
/**
-
获取数据库连接
-
@return数据库连接对象
*/
public static Connection getConnection(){
//定义数据库连接
Connection conn = null;try {
//安装数据库驱动程序
Class.forName(DRIVER);
//获取数据库连接
conn = DriverManager.getConnection(URL,USER,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);
}
}
测试结果:
在编写代码中,我遇到很多小问题,大多数都源于粗心;比如:有些代码会写错个别字母,下个作业我会更加认真。加油