题目来自某节点的视频。
github链接:代码
首先数据库中有2个表,部门表(dept.frm),用户信息表(user.frm)
部门表结构如下图:含三个字段deptno(部门编号),dname(部门名称)loc,(部门地址)。主键字段为deptno
用户信息表结构如下:包含uname(用户名称)、upassword(用户密码)
小系统的结构
如下图,包含4个模块:
test中为程序入口
dao即数据库访问对象,类中封装的是增删改查操作,减少重复代码
util即工具,封装的是JDBC所需资源的创建与销毁,减少重复代码
entity即表的实体,其中的类是dept表的实体类,类的属性即表的各个字段,用于查找时接受数据行,即每个实例对象代表一个数据行
jdbc.properties中存放的是配置信息(url,数据库账号与密码),便于信息修改
jdbc.properties文件内容如下:
各个类的代码如下,看注释应该可以看懂,就不详细介绍了:
程序中的数据操作对象都是PreparedStatement类型,即只编译一次,防止SQL注入。
JDBCtest类
import dao.DeptDao;
import entity.Dept;
import util.JDBCUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JDBCtest {
public static void main(String[] args) {
//要求使用者输入的相关变量
Scanner scanner = new Scanner(System.in);
String usrname, password;//用户登录名,用户登录密码
int deptno;//部门编号
String loc, dname;//部门地址与部门名称
DeptDao dao = new DeptDao();//dao对象
JDBCUtil util = new JDBCUtil();//工具对象
//登录实现
System.out.println("****欢迎来到部门管理界面****");
System.out.println("****请输入用户名****");
usrname = scanner.nextLine();
System.out.println("****请输入用户密码****");
password = scanner.nextLine();
String sql = "select count(*) from user where uname = ? and upassword = ?";
PreparedStatement preparedStatement;
preparedStatement = util.creatPreparedStatement(sql);
ResultSet resultSet = null;
int result = -2;//储存各种输入与返回值
try {
preparedStatement.setString(1, usrname);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
result = resultSet.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
util.close(resultSet);//关闭各种资源
}
if (result == 1){
System.out.println("登录成功");
}else{
System.out.println("密码或用户名错误");
return;
}
//增删改查功能实现
while (true){
System.out.println("****查询部门信息请输入:1****");
System.out.println("****删除部门信息请输入:2****");
System.out.println("****改动部门信息请输入:3****");
System.out.println("****增加部门信息请输入:4****");
System.out.println("****退出系统请输入:0****");
int input = scanner.nextInt();
if (input == 1) {
List<Dept> depts = new ArrayList<>();
depts = dao.select();
for (Dept dept : depts){
System.out.println(dept);
}
} else if(input == 2){
System.out.println("请输入要删除的部门");
deptno = scanner.nextInt();
result = dao.delete(deptno);
if (result == 1) System.out.println("删除成功");
else System.out.println("删除失败");
}else if (input == 3){
System.out.println("请输入要更新的部门");
deptno = scanner.nextInt();
System.out.println("请输入部门新名字");
dname = scanner.next();
System.out.println("请输入部门新地址");
loc = scanner.next();
result = dao.update(deptno, dname, loc);
if (result == 1) System.out.println("修改成功");
else System.out.println("修改失败");
}else if (input == 4){
System.out.println("请输入新部门编号");
deptno = scanner.nextInt();
System.out.println("请输入新部门名称");
dname = scanner.next();
System.out.println("请输入新部门地址");
loc = scanner.next();
result = dao.insert(deptno, dname, loc);
if (result == 1) System.out.println("增加部门成功");
else System.out.println("增加部门失败");
}else if (input == 0){
break;
}else{
System.out.println("输入有误");
}
}
}
}
JDBCUtil类:
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCUtil {
private static String url;
private static String mysqlusr;
private static String mysqlpassword;
private Connection connection;
private PreparedStatement preparedStatement;
//静态代码块,用于配置信息与驱动(Driver)的加载
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败");
}
ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");//配置文件常用的读取方法
url = resourceBundle.getString("url");
mysqlusr = resourceBundle.getString("mysqlusr");
mysqlpassword = resourceBundle.getString("mysqlpassword");
System.out.println("配置文件读取成功");
}
//java与数据库连接通道创建方法
public void creatConnection(){
try {
connection = DriverManager.getConnection(url, mysqlusr, mysqlpassword);
} catch (SQLException e) {
e.printStackTrace();
}
}
//数据操作对象的创建方法,由于调用了通道创建方法,所以通道连接在主程序中不用调用了
public PreparedStatement creatPreparedStatement(String sql){
creatConnection();
try {
preparedStatement = this.connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return preparedStatement;
}
//通道与数据访问对象的关闭,增删改三种操作调用它
public void close(){
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//重载,并且调用上面的无参方法,关闭通道、数据访问对象与返回结果对象。查询操作的资源关闭调用此方法
public void close(ResultSet resultSet){
close();
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Dept类:
package entity;
public class Dept {
private int deptno;
private String dname;
private String loc;
public Dept() { }
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "部门编号:" + deptno +
" 部门名:" + dname +
" 部门地址:" + loc ;
}
}
DeptDao类:
import entity.Dept;
import util.JDBCUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DeptDao {
private PreparedStatement preparedStatement;
private JDBCUtil util;
private ResultSet resultSet;
public DeptDao() {
this.preparedStatement = null;
this.util = new JDBCUtil();
this.resultSet = null;
}
//增(增加部门的方法)
public int insert(int deptno, String dname, String loc){
String sql = "insert into dept(deptno, dname, loc) values(?,?,?)";
preparedStatement = util.creatPreparedStatement(sql);
int result = 0;
try {
preparedStatement.setInt(1, deptno);//替换第一个占位符
preparedStatement.setString(2, dname);//替换第二个占位符
preparedStatement.setString(3, loc);//替换第三个占位符
result = preparedStatement.executeUpdate();//执行sql语句并返回更新的行数,1为成功
} catch (SQLException e) {
e.printStackTrace();
}finally {
util.close();//关闭资源
}
return result;
}
//改(改动指定部门名称与地址的方法)
public int update(int deptno, String dname, String loc){
String sql = "update dept set dname = ?, loc = ? where deptno = ?";
preparedStatement = util.creatPreparedStatement(sql);
int result = 0;
try {
preparedStatement.setString(1, dname);
preparedStatement.setString(2, loc);
preparedStatement.setInt(3, deptno);
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
util.close();
}
return result;
}
//删(删除指定部门的方法)
public int delete(int deptno){
String sql = "delete from dept where deptno = ?";
preparedStatement = util.creatPreparedStatement(sql);
int result = 0;
try {
preparedStatement.setInt(1, deptno);
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
util.close();
}
return result;
}
//查(查询部门表信息)、返回的是一个表的实体类的集合
public List<Dept> select(){
String sql = "select * from dept";
preparedStatement = util.creatPreparedStatement(sql);
List<Dept> result = new ArrayList<>();
try {
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
Dept dept = new Dept(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3));
result.add(dept);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
util.close(resultSet);
}
return result;
}
}