基于MVC经典的架构模式,各层之间分工明确,保证了低耦合。具体如下:
基于JDBC实现一个简易的人员信息管理系统,该系统基本可以完成人员信息的添加、删除、查询、更新(修改)等过程(即CRUD),视图层使用简单的控制台方式来展示(因为视图层在这里不是重点)。
打开Navicat 12 for MySQL,新建一个连接,这里为“mysql”,然后创建一个数据库,这里为“mydatabase”,然后右键该数据库名,进入命令列界面输入如下MySQL语句建立人员信息表:
create table people_information(
id int primary key auto_increment,
userName varchar(30) not null,
sex int,
age int,
birthday date,
email varchar(30),
mobile varchar(11),
createUser varchar(30),
createDate date,
updateUser varchar(30),
updateDate date,
isDel int
)engine=innodb default charset=utf8 auto_increment=1;
alter database mysql default character set 'utf8';
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';
执行语句之后,界面如下:
Code:
JDBC_Connection.java
package com.wuchangi.Model_Layer;
import java.sql.*;
/*
* @program: JDBC_PeopleInformation
* @description: Tool Class to get the connection to the MySQL Database.
* @author: WuchangI
* @create: 2018-05-13-10-23
**/
public class JDBC_Connection
{
private static final String URL = "jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT&useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private static Connection connection = null;
static
{
//1.Loading driver using class name by reflection
//Old loading method: "com.mysql.jdbc.Driver"
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
//2.Get the connection to the MySQL Database
try
{
connection = DriverManager.getConnection(URL, USER, PASSWORD);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static Connection getConnection()
{
return connection;
}
}
Code:
PeopleInformation.java
package com.wuchangi.Model_Layer;
import java.util.Date;
/*
* @program: JDBC_PeopleInformation
* @description: PeopleInformation Model Class
* @author: WuchangI
* @create: 2018-05-13-10-23
**/
public class PeopleInformation
{
//人员唯一的编号,设置为主键且自增
private Integer id;
//人员的姓名
private String userName;
//人员的性别,0为男性,1为女性
private Integer sex;
//人员的年龄
private Integer age;
//人员的生日
private Date birthday;
//人员的邮箱
private String email;
//人员的手机号码
private String mobile;
//创建该记录的人的名称
private String createUser;
//更新该记录的人的名称
private String updateUser;
//该记录创建日期
private Date createDate;
//该记录更新日期
private Date updateDate;
//是否删除,1为删除,0为不删除
private Integer isDel;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public Integer getSex()
{
return sex;
}
public void setSex(Integer sex)
{
this.sex = sex;
}
public Integer getAge()
{
return age;
}
public void setAge(Integer age)
{
this.age = age;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getMobile()
{
return mobile;
}
public void setMobile(String mobile)
{
this.mobile = mobile;
}
public String getCreateUser()
{
return createUser;
}
public void setCreateUser(String createUser)
{
this.createUser = createUser;
}
public String getUpdateUser()
{
return updateUser;
}
public void setUpdateUser(String updateUser)
{
this.updateUser = updateUser;
}
public Date getCreateDate()
{
return createDate;
}
public void setCreateDate(Date createDate)
{
this.createDate = createDate;
}
public Date getUpdateDate()
{
return updateDate;
}
public void setUpdateDate(Date updateDate)
{
this.updateDate = updateDate;
}
public Integer getIsDel()
{
return isDel;
}
public void setIsDel(Integer isDel)
{
this.isDel = isDel;
}
@Override
public String toString()
{
return "PeopleInformation{" + "id=" + id + ", userName='" + userName + '\'' + ", sex=" + sex + ", age=" + age + ", birthday=" + birthday + ", email='" + email + '\'' + ", mobile='" + mobile + '\'' + ", createUser='" + createUser + '\'' + ", updateUser='" + updateUser + '\'' + ", createDate=" + createDate + ", updateDate=" + updateDate + ", isDel=" + isDel + '}';
}
}
Code:
DAO.java
package com.wuchangi.Model_Layer;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/*
* @program: JDBC_PeopleInformation
* @description: DAO Layer
* @author: WuchangI
* @create: 2018-05-13-10-23
**/
public class DAO
{
//添加一个新的人员信息
public void addPeopleInformation(PeopleInformation peopleInformation) throws SQLException
{
//拿到数据库的连接
Connection connection = JDBC_Connection.getConnection();
String sql = "" +
" insert into mydatabase.people_information(userName, sex, age, birthday, email, mobile," +
"createUser, createDate, updateUser, updateDate, isDel)"+
" values(" + "?,?,?,?,?,?,?,current_date(),?,current_date(),?6)";
//预编译SQL语句,并不直接执行
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//设置SQL语句的参数
preparedStatement.setString(1, peopleInformation.getUserName());
preparedStatement.setInt(2, peopleInformation.getSex());
preparedStatement.setInt(3, peopleInformation.getAge());
//注意getBirthday返回的是java.util.Date,而setDate的参数是java.sql.Date,需要进行转换
preparedStatement.setDate(4, new Date(peopleInformation.getBirthday().getTime()));
preparedStatement.setString(5, peopleInformation.getEmail());
preparedStatement.setString(6, peopleInformation.getMobile());
preparedStatement.setString(7, peopleInformation.getCreateUser());
preparedStatement.setString(8, peopleInformation.getUpdateUser());
preparedStatement.setInt(9, peopleInformation.getIsDel());
//执行SQL语句
preparedStatement.execute();
}
//更新id为peopleInformation.getId()的人员的信息
public void updatePeopleInformation(PeopleInformation peopleInformation) throws SQLException
{
//拿到数据库的连接
Connection connection = JDBC_Connection.getConnection();
String sql = "" +
" update mydatabase.people_information"+
" set userName=?, sex=?, age=?, birthday=?, email=?, mobile=?, updateUser=?, updateDate=current_date(), isDel=?"+
" where id=?";
//预编译SQL语句,并不直接执行
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//设置SQL语句的参数
preparedStatement.setString(1, peopleInformation.getUserName());
preparedStatement.setInt(2, peopleInformation.getSex());
preparedStatement.setInt(3, peopleInformation.getAge());
//注意getBirthday返回的是java.util.Date,而setDate的参数是java.sql.Date,需要进行转换
preparedStatement.setDate(4, new Date(peopleInformation.getBirthday().getTime()));
preparedStatement.setString(5, peopleInformation.getEmail());
preparedStatement.setString(6, peopleInformation.getMobile());
preparedStatement.setString(7, peopleInformation.getUpdateUser());
preparedStatement.setInt(8, peopleInformation.getIsDel());
preparedStatement.setInt(9, peopleInformation.getId());
//执行SQL语句
preparedStatement.execute();
}
//删除指定id的人员的信息
public void deletePeopleInformation(Integer id) throws SQLException
{
//拿到数据库的连接
Connection connection = JDBC_Connection.getConnection();
String sql = "" +
" delete from mydatabase.people_information" +
" where id=?";
//预编译SQL语句,并不直接执行
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//设置SQL语句的参数
preparedStatement.setInt(1, id);
//执行SQL语句
preparedStatement.execute();
}
//查询数据库中所有人员的基本信息(id + 姓名 + 年龄)
public List queryPeopleInformation() throws SQLException
{
Connection connection = JDBC_Connection.getConnection();
//通过数据库的连接操纵数据库,实现增删查改
Statement stmt = connection.createStatement();
//其中ResultSet使用的是java.sql.ResultSet
ResultSet resultSet = stmt.executeQuery("select id, userName, age from mydatabase.people_information");
List peopleInformations = new ArrayList();
PeopleInformation peopleInformation = null;
while(resultSet.next())
{
peopleInformation = new PeopleInformation();
peopleInformation.setId(resultSet.getInt("id"));
peopleInformation.setUserName(resultSet.getString("userName"));
peopleInformation.setAge(resultSet.getInt("age"));
peopleInformations.add(peopleInformation);
}
return peopleInformations;
}
//根据指定条件检索数据库中的所有人员的信息
//一个Map存储一个条件表达式,比如 userName = '小美'
public List queryPeopleInformation(List
Code:
View.java
package com.wuchangi.View_Layer;
import com.wuchangi.Model_Layer.PeopleInformation;
import com.wuchangi.Control_Layer.Control;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
* @program: JDBC_PeopleInformation
* @description: View Layer
* @author: WuchangI
* @create: 2018-05-13-10-23
**/
//使用控制台作为视图层
public class View
{
//主菜单提示信息
private static final String MAIN_MENU_CONTENT = "\t\t****欢迎使用人员信息管理系统!****\n\n" +
"下面是该系统的功能列表:\n\n" +
"[QUERY/Q]: 查看全部人员的基本信息(id、姓名、年龄)\n" +
"[GET/G]: 查看某个人的具体信息\n" +
"[ADD/A]: 添加新的人员信息\n" +
"[UPDATE/U]: 更新人员信息\n" +
"[DELETE/D]: 删除人员信息\n" +
"[SEARCH/S]: 查询指定的人员信息(根据姓名、手机号码查询)\n" +
"[EXIT/E]: 退出系统\n" +
"\n请输入您想使用的功能项:";
//相关操作标记
private static final String OPERATION_QUERY = "QUERY";
private static final String OPERATION_GET = "GET";
private static final String OPERATION_ADD = "ADD";
private static final String OPERATION_UPDATE = "UPDATE";
private static final String OPERATION_DELETE = "DELETE";
private static final String OPERATION_SEARCH = "SEARCH";
private static final String OPERATION_EXIT = "EXIT";
private static final String OPERATION_BREAK = "BREAK";
public static void main(String[] args)
{
//展示主菜单界面
System.out.println(MAIN_MENU_CONTENT);
Scanner scan = new Scanner(System.in);
Control control = new Control();
//记录之前执行的条件分支
String previousBranch = "";
Integer step = 1;
String inputValue = null;
PeopleInformation peopleInformation = new PeopleInformation();
List
Code:
Control.java
package com.wuchangi.Control_Layer;
import com.wuchangi.Model_Layer.DAO;
import com.wuchangi.Model_Layer.PeopleInformation;
import java.sql.SQLException;
import java.util.*;
/*
* @program: JDBC_PeopleInformation
* @description: Control Layer
* @author: WuchangI
* @create: 2018-05-13-10-23
**/
public class Control
{
//查看某个人的详细信息
public PeopleInformation get(Integer id) throws SQLException
{
DAO dao = new DAO();
return dao.getPeopleInformation(id);
}
//添加某个人的信息
public void add(PeopleInformation peopleInformation) throws SQLException
{
DAO dao = new DAO();
peopleInformation.setCreateUser("ADMIN");
peopleInformation.setUpdateUser("ADMIN");
peopleInformation.setIsDel(0);
dao.addPeopleInformation(peopleInformation);
}
//更新某个人(id为peopleInformation.getId())的人的信息
public void update(PeopleInformation peopleInformation) throws SQLException
{
DAO dao = new DAO();
peopleInformation.setCreateUser("ADMIN");
peopleInformation.setUpdateUser("ADMIN");
peopleInformation.setIsDel(0);
dao.updatePeopleInformation(peopleInformation);
}
//删除某个人的信息
public void del(Integer id) throws SQLException
{
DAO dao = new DAO();
dao.deletePeopleInformation(id);
}
//查询数据库中所有人员的基本信息(id + 姓名 + 年龄)
public List query() throws SQLException
{
DAO dao = new DAO();
List peopleInformationList =dao.queryPeopleInformation();
return peopleInformationList;
}
//查询数据库中符合指定条件的所有人员的信息
public List query(List> params) throws SQLException
{
DAO dao = new DAO();
List peopleInformationsList = dao.queryPeopleInformation(params);
return peopleInformationsList;
}
}