(二)创建项目
1、创建Java项目
2、在项目里创建文件夹
3、在lib里添加MySQL数据库驱动程序包
4、在images里添加图片
5、在help里添加帮助文档
1、创建Java项目
2、在项目里创建文件夹
3、在lib里添加MySQL数据库驱动程序包
下载数据库驱动程序包
链接:https://pan.baidu.com/s/1xE-jJVMGiPQ5Qdxi7q-KZw 提取码:og6p
作为库添加到项目里(Add as Library…)
右键点击驱动程序包点击——Add as Library…
4、在images里添加图片
下载图片素材
链接:https://pan.baidu.com/s/1V3afx3Lw_4sw3R2Gyy2mZg 提取码:4sjd
下载帮助文档
链接:https://pan.baidu.com/s/11TG9x3dfxej-06eWbcsqGw 提取码:iv6z
将帮助文档拷贝到help目录
(三)创建实体类
1、创建学校实体类对应学校表
2、创建状态实体类对应状态表
3、创建学生实体类对应学生表
4、创建用户实体类对应用户表
依次创建四个实体类:College、Status、Student与User,分别对应t_college表、t_status表、t_student表与t_user表。
实体类的属性对应于数据表的字段,主要两者的数据类型要匹配,网上可以搜索到MySQL数据类型与Java数据类型的匹配对应表。
package net.zm.student.bean;
import java.util.Date;
/**
* 创建学校实体类
*/
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 + '\'' +
'}';
}
}
package net.zm.student.bean;
/**
* 创建状态实体类
*/
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 + '\'' +
'}';
}
}
package net.zm.student.bean;
/**
* 创建学生实体类
*/
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 + '\'' +
'}';
}
}
package net.zm.student.bean;
import java.sql.Timestamp;
/**
* 创建用户实体类
*/
public class User {
private int id; // 用户标识符
private String username; // 用户名
private String password; // 密码
private String telephone; // 联系电话
private Timestamp 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 Timestamp getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Timestamp registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
'}';
}
}
(四)创建数据库连接管理类方法
1、在程序里定义数据库连接属性常量方法
2、从数据库配置文件里读取属性值
在程序里定义数据库连接属性常量,也可以将数据库连接属性值写入数据库配置文件,并且保存在项目内,在程序中读取文件中的信息,拿到数据库连接属性值,从而进行数据库连接。
package net.zm.student.dbutli;
import javax.swing.*;
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"; //数据库统一资源标识符
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();
}
}
}
public static void main(String[] args) {
//获取数据库连接
Connection conn = getConnection();
//判断是否连接成功
if (conn != null){
JOptionPane.showMessageDialog(null,"恭喜,数据库连接成功!");
}else {
JOptionPane.showMessageDialog(null,"遗憾,数据库连接失败!");
}
//关闭数据库连接
closeConnection(conn);
}
}
1.创建DisplayAllUsers显示全部用户记录类(使用普通语句对象)
2.创建FindUserById按编号查找用户记录类
3.创建Login用户登录类(使用预备语句对象)
4.创建AddUser添加用户记录类
5.创建UpdateUser更新用户记录类
6.创建DeleteUserById删除用户记录类
package net.zm.student.test;
import net.zm.student.bean.User;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 功能:显示用户表记录
* 全部或部分记录
*/
public class DisplayAIIUsers {
public static void main(String[] args) {
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
try {
// 2. 定义SQL语句字符串
String strSQL = "select * from t_user";
// 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"));
// 输出用户实体对象
System.out.println(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
}
package net.zm.student.test;
/**
* 创建查找用户表
* 按编号查找用户记录
*/
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class FindUserById {
public static void main(String[] args) {
int id;
Scanner sc = new Scanner(System.in);
System.out.print("请输入待查用户编号:");
id = sc.nextInt();
//1.获取数据库连接
Connection conn = ConnectionManager.getConnection();
try {
//2.定义sql字符串
String strSQL = "select * from t_user where id = ?";
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setInt(1,id);
//5.执行sql查询,返回结果集
ResultSet rs = pstmt.executeQuery();
//6.判断结果集是否
if (rs.next()){
//7.输出当前记录各个字段值
System.out.println("编号:"+rs.getInt("id"));
System.out.println("用户名:"+rs.getString("username"));
System.out.println("密码:"+rs.getString("password"));
System.out.println("电话:"+rs.getString("telephone"));
System.out.println("注册时间:"+rs.getString("register_time"));
}else {
System.out.println("查询结果:编号为["+id+"]的用户不存在!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 8.关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
}
package net.zm.student.test;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
/**
* 功能:用户登录程序
* 连接数据库,判读是否是合法用户
* 然后给与用户相应的提示
*/
public class Login {
public static void main(String[] args) {
// 声明部分
String username;
String password;
Scanner sc = new Scanner(System.in);
// 输入部分
System.out.print("输入用户名:");
username = sc.next();
System.out.print("输入密码:");
password = sc.next();
// 处理部分
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
try {
// 2. 定义SQL字符串
String strSQL = "select * from t_user where username = ? and password = ?";
// 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()) {
System.out.println("恭喜,登录成功!");
} else {
System.err.println("遗憾,登录失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
}
package net.zm.student.test;
import net.zm.student.bean.User;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Scanner;
public class AddUser {
public static void main(String[] args) {
// 声明部分
String username, password, telephone;
Scanner sc = new Scanner(System.in);
// 输入部分
System.out.print("用户名:");
username = sc.next();
System.out.print("密码:");
password = sc.next();
System.out.print("电话:");
telephone = sc.next();
// 处理部分
// 1. 创建用户实体对象
User user = new User();
// 2. 设置实体属性
user.setUsername(username);
user.setPassword(password);
user.setTelephone(telephone);
user.setRegisterTime(new Timestamp(new Date().getTime()));
// 3. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 4. 定义SQL字符串
String strSQL = "insert into t_user (username, password, telephone, register_time) values (?, ?, ?, ?)";
try {
// 5. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 6. 设置占位符的值
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, user.getRegisterTime());
// 7. 执行SQL,返回添加的记录数
int count = pstmt.executeUpdate();
// 8. 判断是否添加成功
if (count > 0) {
// 提示用户添加成功
System.out.println("恭喜,用户记录添加成功!");
} else {
// 提示用户添加失败
System.out.println("遗憾,用户记录添加失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
}
package net.zm.student.test;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
/**
* 更新用户记录
*/
public class UpdateUser {
public static void main(String[] args) {
// 声明部分
int id;
String username, password;
Scanner sc = new Scanner(System.in);
// 输入部分
System.out.print("待编辑记录的id:");
id = sc.nextInt();
System.out.print("新用户名:");
username = sc.next();
System.out.print("新密码:");
password = sc.next();
// 处理部分
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "update t_user set username = ?, password = ? where id = ?";
try {
// 3. 创建预备语句
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setInt(3, id);
// 5. 执行SQL,返回更新成功的记录数
int count = pstmt.executeUpdate();
// 6. 判断是否更新成功
if (count > 0) {
// 提示用户更新成功
System.out.println("恭喜,用户记录更新成功!");
} else {
// 提示用户更新失败
System.out.println("遗憾,用户记录更新失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
}
package net.zm.student.test;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
/**
* 按用户编号删除用户记录
*/
public class DeleteUserByld {
public static void main(String[] args) {
// 声明部分
int id;
Scanner sc = new Scanner(System.in);
// 输入部分
System.out.print("待删记录的id:");
id = sc.nextInt();
// 处理部分
// 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,返回删除的记录数
int count = pstmt.executeUpdate();
// 6. 判断是否删除成功
if (count > 0) {
// 提示用户删除成功
System.out.println("恭喜,用户记录删除成功!");
} else {
// 提示用户删除失败
System.out.println("遗憾,用户记录删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
}