JDBC工具类封装、封装DAO方法与数据库表实体类封装
准备:
MySQL数据表:
CREATE TABLE 数据库名称
.emp
(
id
int NOT NULL COMMENT 'id',
name
varchar(255) NULL COMMENT //代码效果参考:http://www.lyjsj.net.cn/wx/art_23967.html
department
varchar(255) NULL COMMENT '任务组',
PRIMARY KEY (id
)
);
--添加几条数据
insert into emp (id,name,department) values (10,"note","159");
insert into emp (id,name,department) values (20,"lik","152");
insert into emp (id,name,department) values (30,"bybe","132");
insert into emp (id,name,department) values (40,"mrcheng","322"
JDBC 工具封装类:
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394package JDBCTest; import java.sql.; /** 方法描述 @since: 1.0.0 @param: jdbc工具类 对一些操作进行封装 @JDBCUtils: JDBCUtils @author: Mr.cheng @date: 2021/1/14 8:57 /public class JDBCUtils { Connection coon = null; PreparedStatement ps = null; //使静态代码块对需要低用该工具类就能执行,平切只执行一//代码效果参考:http://www.lyjsj.net.cn/wx/art_23965.html
次 static { try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("注册驱动已执行!"); } /** @return 注册连接 / public Connection coonManger() { try { coon = DriverManager.getConnection("jdbc:", "root", "123456"); } catch (SQLException ex) { ex.printStackTrace(); System.out.println("程序注册链接失败!"); } System.out.println("加载驱动已执行!"); return coon; } /** 方法描述 表示预编译SQL语句的对象。 @since: 1.0.0 @param: 【sql】 @return: java.sql.PreparedStatement @author: Mr @date: 2021/1/14 9:18 / public PreparedStatement prepare(String sql) throws Exception { try { ps = coon.prepareStatement(sql); } catch (SQLException eps) { eps.printStackTrace(); System.out.println("获取数据库解析失败!"); } return ps; } //关闭资源 public void close() { if (coon != null) { try { coon.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } //包含结果集ResultSet 查询结构及进行关闭 public void resultSet(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } //调用关闭资源的 close(); }}使用案列:
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960package JDBCTest; import JDBCTest.dao.EmpDao; import java.sql.Connection;import java.sql.PreparedStatement;import java.util.List;import java.util.Scanner; //代码效果参考:http://www.lyjsj.net.cn/wz/art_23963.html
public class UtilTest { public static void main(String【】 args) { Scanner scanner = new Scanner(System.in); Connection coon = null; PreparedStatement ps = null; String id, name, department; int faly = 0; JDBCUtils utils = new JDBCUtils(); Emp emp = new Emp(); EmpDao dao = new EmpDao(); System.out.println("*欢迎来到猪猪世界"); System.out.println("请输入你要办理的业务"); System.out.println("1.添加朋友"); System.out.println("2.清除朋友"); System.out.println("3.更新朋友"); System.out.println("4.我的好友"); System.out.println("请选择功能!"); faly = scanner.nextInt(); if (faly == 1) { &n