【整体思路】在java包中创建一个Dao类(数据类),通过创建方法实现对实体类对象的操作
文章目录
- 一、实体类展示
- 二、JDBCutil工具类的展示
- 三、Dao类展示
- 3.1 数据库初始数据
- 3.1 增加
- 3.2 删除
- 3.3 修改
- 3.4 查询
一、实体类展示
package JDBCConnectJava;
import java.sql.Date;
public class EmployeeModel {
private String code; // 员工号
private String name; // 员工姓名
private String gender; // 性别
private String birthday; // 出生日期
private Date entryDate; // 入职时间
private String tel; // 电话
public EmployeeModel(String code, String name, String gender, String birthday, Date entryDate, String tel) {
super();
this.code = code;
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.entryDate = entryDate;
this.tel = tel;
}
public EmployeeModel(String code) {
super();
this.code = code;
}
public EmployeeModel() {
super();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGendar() {
return gender;
}
public void setGendar(String gendar) {
this.gender = gendar;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "员工信息[员工号:" + code + ", 姓名:" + name + ", 性别:" + gender + ", 出生日期:" + birthday + ", 入职时间:" + entryDate
+ ", 电话:" + tel + "]";
}
}
一个简单的employee实体类~
二、JDBCutil工具类的展示
package JDBCConnectJava;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class JDBCutil {
static ResourceBundle re = ResourceBundle.getBundle("package1.jdbc");
static String driver = re.getString("driver");
static String url = re.getString("url");
static String user = re.getString("user");
static String pass = re.getString("pass");
// 私有的构造 防止外边创建对象
private JDBCutil() {
}
// 每次使用类时,自动加载驱动
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取连接
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, pass);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// 关闭--1
public static void close(Connection con, Statement st) {
try {
st.close();
st = null;
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭--2
public static void close(Connection con, Statement st, ResultSet re) {
try {
st.close();
st = null;
con.close();
con = null;
re.close();
re = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
在JDBC模块的最后有过对工具类简单介绍。这里可以把工具类应用到具体代码中。
三、Dao类展示
3.1 数据库初始数据
3.1 增加
// 添加数据
public Integer intsert(EmployeeModel em) {
String sql = "insert into employee(code,name,gender,birthday,entry_time,tel) values(?,?,?,?,?,?)";
Connection con = null;
PreparedStatement st = null;
Integer result = null;//返回值
con = JDBCutil.getConnection();
try {
st = con.prepareStatement(sql);
st.setString(1, em.getCode());
st.setString(2, em.getName());
st.setString(3, em.getGendar());
st.setString(4, em.getBirthday());
st.setDate(5, em.getEntryDate());
st.setString(6, em.getTel());
result = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCutil.close(con, st);
}
return result;
}
main方法:
public static void main(String[] args) {
IEmployeeDao dao = new IEmployeeDao();
// 1--添加
EmployeeModel em = new EmployeeModel("05","刘晓开","男","1997-05-21",Date.valueOf(LocalDate.now()),"15223656987");
if(dao.intsert(em)==null)
{
System.out.println("添加失败!");
}else {
System.out.println("添加成功!");
}
}
执行结果:
添加成功!
执行后的表数据:
可以看到05号已被添加入表中。
连接的关键主要就是从对象em中获得要添加的数据。
3.2 删除
// 删除数据
public Integer delete(EmployeeModel em) {
String sql = "delete from employee where code = ?";
Connection con = JDBCutil.getConnection();
PreparedStatement st = null;
Integer result = null;
try {
st = con.prepareStatement(sql);
st.setString(1, em.getCode());
result = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCutil.close(con, st);
}
return result;
}
main方法:
public static void main(String[] args) {
IEmployeeDao dao = new IEmployeeDao();
//2--删除
EmployeeModel em = new EmployeeModel("01");
if(dao.delete(em)==null)
{
System.out.println("删除失败!");
}else {
System.out.println("删除成功!");
}
}
结果:
删除成功!
这是数据库的01号已被删除:
3.3 修改
// 修改数据
public Integer update(EmployeeModel em) {
String sql = "update employee set name = ?,gender = ?,birthday = ?,entry_time = ?,tel = ?where code = ?";
Connection con = JDBCutil.getConnection();
PreparedStatement st = null;
Integer result = null;
try {
st = con.prepareStatement(sql);
st.setString(1, em.getName());
st.setString(2, em.getGendar());
st.setString(3, em.getBirthday());
st.setDate(4, em.getEntryDate());
st.setString(5, em.getTel());
st.setString(6, em.getCode());
result = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCutil.close(con, st);
}
return result;
}
main中执行:
public static void main(String[] args) {
IEmployeeDao dao = new IEmployeeDao();
//3--修改
EmployeeModel em = new EmployeeModel("02", "李晓美", "女", "1999-05-21", Date.valueOf(LocalDate.now()),"15562599863");
if (dao.update(em) == null) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
执行结果:
修改成功!
此时的数据库数据已修改:
3.4 查询
【查询要考虑的问题是查询条件的不确定性,那么如何解决这个问题呢?】
【我们可以先定义一个方法,把查询的条件即where后边的内容确定下来:】
// 添加查询条件
private ArrayList<Object> addWhere(StringBuffer sql, EmployeeModel em) {
ArrayList<Object> ob = new ArrayList<Object>();
String name = em.getName();
if (name != null && !name.trim().isEmpty()) {
sql.append("and name like ? ");
ob.add(name);
}
String code = em.getCode();
if (code != null && !code.trim().isEmpty()) {
sql.append("and code like ? ");
ob.add(code);
}
String gender = em.getGendar();
if (gender != null && !gender.trim().isEmpty()) {
sql.append("and gender like ? ");
ob.add(gender);
}
return ob;
}
- sql为初始的sql查询语句
select code,name,birthday,gender,entry_time,tel from employee where 1=1
,定义为StringBuffer类型可以方便地用append()方法追加查询条件; - 此时的实体类对象包含的属性全部是查询条件组成,下边的判空就是找到查询条件并追加在sql语句上;
- 此方法的返回值是查询条件的集合,用此集合可以方便地对预处理状态集进行赋值,并且因为ArrayList的顺序性,可以使set方法找到对应的占位符的位置。
【确定好查询条件后,剩下的查询就好做了:】
// 查询数据
public ArrayList<EmployeeModel> select(EmployeeModel em) {
StringBuffer sql = new StringBuffer("select code,name,birthday,gender,entry_time,tel from employee where 1=1 ");
ArrayList<Object> arr = addWhere(sql,em);
Connection con = JDBCutil.getConnection();
PreparedStatement st = null;
ResultSet re = null;
ArrayList<EmployeeModel> list = new ArrayList<EmployeeModel>();
try {
st = con.prepareStatement(sql.toString());
//占位符赋值
for(int i=0;i<arr.size();i++) {
st.setObject(i+1, arr.get(i));
}
re = st.executeQuery();
//将结果集添加到要返回的集合中
while(re.next()) {
EmployeeModel e = new EmployeeModel();
e.setCode(re.getString("code"));
e.setName(re.getString("name"));
e.setGendar(re.getString("gender"));
e.setBirthday(re.getString("birthday"));
e.setEntryDate(re.getDate("entry_time"));
e.setTel(re.getString("tel"));
list.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCutil.close(con, st, re);
}
return list;
}
- sql语句的
1=1
保证了查询条件为空时也能正确运行sql语句,为恒真; - 将结果集添加到要返回的集合中。
main方法的执行:
public static void main(String[] args) {
IEmployeeDao dao = new IEmployeeDao();
//4--查询
EmployeeModel em = new EmployeeModel();
em.setName("王%");
ArrayList<EmployeeModel> arr = dao.select(em);
for(EmployeeModel e:arr) {
System.out.println(e.toString());
}
}
执行结果:
员工信息[员工号:03, 姓名:王五, 性别:男, 出生日期:2000-07-15, 入职时间:2015-07-08, 电话:17866639856]
员工信息[员工号:04, 姓名:王欧凯, 性别:男, 出生日期:2000-07-10, 入职时间:2015-07-08, 电话:17889653216]