有勇气的牛排博客

java Bean工厂 Dao层设计初级

有勇气的牛排 1080 Java 2021-08-26 21:57:34

1 util 工具类

package net920vip.util; import java.sql.*; public class DBUtil { Connection con; PreparedStatement pstm; ResultSet rs; public Connection getCon() { try { Class.forName("com.mysql.jdbc.Driver"); String username = "root"; String password = "root"; String datebase = "student"; con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + datebase + "?useUnicode=true&characterEncoding=utf-8", username, password); } catch (Exception e) { e.printStackTrace(); } return con; } public void closeAll() { try { // 做非空判断 if (rs != null) { rs.close(); } if (pstm != null) { pstm.close(); } if (con != null) { con.close(); } // con.close(); } catch (SQLException e) { e.printStackTrace(); } } }

2 Bean工厂

package net920vip.Bean; public class Student { private int id; private String username; private String passoword; // 无参构造 public Student() { } // 带参构造 两个参数 public Student(String username, String passoword) { this.username = username; this.passoword = passoword; } // 带参构造 三个参数 public Student(int id, String username, String passoword) { this.id = id; this.username = username; this.passoword = passoword; } 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 getPassoword() { return passoword; } public void setPassoword(String passoword) { this.passoword = passoword; } @Override public String toString() { return "net920vip.Bean.Student{" + "id=" + id + ", username='" + username + '\'' + ", passoword='" + passoword + '\'' + '}'; } }

3 Dao层设计

  • Dao 数据库和java的连接

  • 专门对Student表的插入方法

如果按照下面这种方法设计,每个表都要写个Dao文件,就会特别麻烦,下篇文章,会对Dao层抽象设计,分离通用代码

package net920vip.Dao; import net920vip.Bean.Student; import net920vip.util.DBUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * Dao 数据库和java的连接 * 专门对Student表的插入方法 */ public class StudentDao { /** * 新增学生 * 返回值: * 1:修改成功 */ public int insert(Student s) throws SQLException { DBUtil db = new DBUtil(); Connection con = db.getCon(); String sql = "insert into Student(username,password) values(?,?)"; PreparedStatement pstm = con.prepareStatement(sql); pstm.setString(1, s.getUsername()); pstm.setString(2, s.getPassoword()); int i = pstm.executeUpdate(); return i; } /** * 修改学生 * 返回值: * 1:修改成功 */ public int update(Student s) throws SQLException { DBUtil db = new DBUtil(); Connection con = db.getCon(); String sql = "update Student set username = ?, password = ? where id = ?"; PreparedStatement pstm = con.prepareStatement(sql); pstm.setString(1, s.getUsername()); pstm.setString(2, s.getPassoword()); pstm.setInt(3, s.getId()); int i = pstm.executeUpdate(); db.closeAll(); return i; } /** * 根据id删除学生 * 返回值: * 1:删除成功 * 0:失败,(数据可能不存在) */ public int del(int id) throws SQLException { DBUtil db = new DBUtil(); Connection con = db.getCon(); String sql = "delete from Student where id = ?"; PreparedStatement pstm = con.prepareStatement(sql); pstm.setInt(1, id); int i = pstm.executeUpdate(); db.closeAll(); return i; } /** * 根据用户名模糊查询 * 返回值: * 1:删除成功 * 0:失败,(数据可能不存在) */ public List queryByName(String inputName) throws SQLException { ArrayList<Student> list = new ArrayList<Student>(); DBUtil db = new DBUtil(); Connection con = db.getCon(); String sql = "select * from Student where username like ?"; PreparedStatement pstm = con.prepareStatement(sql); pstm.setString(1, "%" + inputName + "%"); ResultSet rs = pstm.executeQuery(); while (rs.next()) { // 取出每一行中的每一列的数据 int id = rs.getInt(1); String username = rs.getString(2); String password = rs.getString(3); // 将数据封装到Studnet对象里 Student s = new Student(id, username, password); // 分别将每一个studnet对象,添加到list集合中 list.add(s); } db.closeAll(); return list; } /** * 查询所有学生信息 * 返回值:list */ public List queryAll() throws SQLException { ArrayList<Student> list = new ArrayList<Student>(); DBUtil db = new DBUtil(); Connection con = db.getCon(); String sql = "select * from Student"; PreparedStatement pstm = con.prepareStatement(sql); ResultSet rs = pstm.executeQuery(); while (rs.next()) { // 取出每一行中的每一列的数据 int id = rs.getInt(1); String username = rs.getString(2); String password = rs.getString(3); // 将数据封装到Studnet对象里 Student s = new Student(id, username, password); // 分别将每一个studnet对象,添加到list集合中 list.add(s); } db.closeAll(); return list; } }

4 测试

import net920vip.Bean.Student; import net920vip.Dao.StudentDao; import java.sql.SQLException; public class SDaoTest { public static void main(String[] args) throws SQLException { // 插入数据 StudentDao dao = new StudentDao(); Student s = new Student("tom", "abc"); dao.insert(s); // 更新数据 // StudnetDao dao = new StudnetDao(); // Student s = new Student(1, "tom", "CC1"); // int i = dao.update(s); // System.out.println(i); // 删除数据 // StudnetDao dao = new StudnetDao(); // int i = dao.del(4); // System.out.println(i); // 查询所有用户信息 // StudnetDao dao = new StudnetDao(); // List<Student> list = dao.queryAll(); // // if (list != null) { // for (int i = 0; i < list.size(); i++) { // Student s = list.get(i); // System.out.println("索引:" + (i + 1) + "用户名:" + s.getUsername() + "密码:" + s.getPassoword()); // } // } // 模糊查询 // System.out.println("请输入要查询的用户名"); // String inputName = "admin"; // // StudnetDao1 dao = new StudnetDao1(); // List<Student> list = dao.queryByName(inputName); // // if (list != null) { // for (int i = 0; i < list.size(); i++) { // Student s = list.get(i); // System.out.println("索引:" + (i + 1) + "用户名:" + s.getUsername() + "密码:" + s.getPassoword()); // } // } } }

上篇文章:java jdbc连接数据库

下篇文章:java Dao层抽象化设计


留言

专栏
文章
加入群聊