编写一个简单带有图形界面的SSMS,主要实现增删改查功能
开发工具:IntelliJIDEA、mysql(8.0版本)
需要下载数据库驱动,版本必须和数据库版本一致。驱动官网下载地址
项目总构架:
1、登录页面
2、欢迎主页面
3、添加学生信息
4、删除学生信息
5、学生列表信息
6、查询学生信息
7、修改学生信息
一、创建一个命名为lib文件夹来放数据库驱动,然后点右击Add加载jar包
二、如果想要用图片来作为背景用,可以创建一个images放图片,然后在view包再修改各个类的背景图片路径
三、创建数据库命名为ssms
四、用户名:admin
初始密码:111111
注:登录密码和用户名可以任意修改
连接数据库一个工具类,这里的User和PWD要改为你自己的数据库用户名和密码
package utils;
import java.sql.*;
/**
* @author shkstart
* @create 2021-07-11 13:04
*/
public class DbUtil
{
private static final String User = "*****"; //数据库用户名
private static final String PWD = "******"; //数据库密码
private static final String URL = "jdbc:mysql://localhost:3306/ssms";
/**
* jdbc:mysql:// 是指JDBC连接方式;
localhost: 是指你的本机地址;
3306 :SQL数据库的端口号;
ssms :就是你的数据库名字
*/
private static final String DRIVER = "com.mysql.cj.jdbc.Driver"; //驱动
static
{
try
{
Class.forName(DRIVER);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static Connection getConnection()
{
try
{
return DriverManager.getConnection(URL,User,PWD);
}
catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
public static void close(Connection connection, Statement statement)
{
if (statement!=null)
{
try
{
statement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
public static void close(Connection connection, Statement statement, ResultSet rs)
{
if (statement!=null)
{
try
{
statement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection!=null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (rs!=null)
{
try
{
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
操作数据库里学生信息类
package dao;
import model.Student;
import utils.DbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* @author shkstart
* @create 2021-07-11 13:01
*/
public class StuDao
{
public boolean addStu(Student student)
{
Connection connection = DbUtil.getConnection();
String sql = "INSERT INTO student(id,name,grade,departments,phone)values(?,?,?,?,?)";
try
{
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,student.getId());
ps.setString(2,student.getName());
ps.setString(3,student.getGrade());
ps.setString(4,student.getDepartments());
ps.setString(5,student.getPhone());
if (!ps.execute())
{
DbUtil.close(connection,ps);
return true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return false;
}
public boolean delStu(String id)
{
Connection connection = DbUtil.getConnection();
String sql = "delete from student where id=?";
try
{
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,id);
if (!ps.execute())
{
DbUtil.close(connection,ps);
return true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return false;
}
public boolean updateStu(Student student)
{
Connection connection = DbUtil.getConnection();
String sql = "update student set name=?,grade=?,departments=?,phone=? where id=?";
try
{
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,student.getName());
ps.setString(2,student.getGrade());
ps.setString(3,student.getDepartments());
ps.setString(4,student.getPhone());
ps.setString(5,student.getId());
if (!ps.execute())
{
DbUtil.close(connection,ps);
return true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return false;
}
public Student findStu(String id)
{
Connection connection = DbUtil.getConnection();
String sql = "SELECT id,name,grade,departments,phone FROM student where id=?";
try
{
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
if (rs.next())
{
Student student = new Student(rs.getString("id"),rs.getString("name"),rs.getString("grade"),rs.getString("departments"),rs.getString("phone"));
DbUtil.close(connection,ps);
return student;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
public ArrayList<Student>listStu()
{
ArrayList<Student> students = new ArrayList<>();
Connection connection = DbUtil.getConnection();
String sql = "SELECT id,name,grade,departments,phone FROM student";
try
{
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next())
{
Student student = new Student(rs.getString("id"),rs.getString("name"),rs.getString("grade"),rs.getString("departments"),rs.getString("phone"));
students.add(student);
}
DbUtil.close(connection,ps);
return students;
}
catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
}
项目全部代码请参考:链接:https://pan.baidu.com/s/10xrR2ZMhLZPdlcMWR2nU-Q
提取码:linv