+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++ DBUtils +++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ DBUtils 框架是一款简化JDBC代码的强大工具 , 底层依赖反射 //实体类创建尽可能用包装类,基本类型会和许多的库不兼容 ,如Redis. DBUtil在传入Javabean的时候,对象必须符合bean的封装要求 DBUtil是根据无参构造方法实例化对象的... _______________________________________________________________ ----> gender.equals("") ----> "".equals(gender) 二者相比 前者更具有稳定性. ______________________________________________________________ 使用步骤: 1/ 导入包 2/ 使用其提供的工具类 , 创建 QueryRunner 对象 ------------------------------------------------ QueryRunner queryRunner = new QueryRunner(); ------------------------------------------------- 3/ 通过工具类调用方法 ,实现CRUD操作 a/ 创建工具类 ----> 笔记末尾提供工具类代码块儿 b/ 测试CRUD操作 ------------------------------------------------- //增加 : public void insertOne() throws SQLException { queryRunner.update(connection , "INSERT INTO persons SET name = '赵老三' ,idcard = '10003' , gender = '1' , address = '山东蓝翔'; "); } //增加 传入对象数组.. public static int insertOneByObj(Object[] objects) throws SQLException { return queryRunner.update(connection , "INSERT INTO persons SET name = ? ,idcard = ? , gender = ? , address = ? ",objects); } //更新 public void updataOne() throws SQLException { queryRunner.update(connection , "UPDATE persons SET `name` = 'updata' WHERE id =5; "); } //查找: DBUtil 返回一个封装好的map , 值得注意的值这里的第三个参数是 MapHandler ==> 返回类型是map public void selectOne() throws SQLException { Map<String ,Object > map =queryRunner.query(connection,"SELECT * FROM persons WHERE id = 1;",new MapHandler()); } } //查找: 返回实体类 第三个参数是 BeanHandler 返回一个实体类型 public void selectOne1() throws Exception { Persons persons =queryRunner.query(connection,"SELECT * FROM persons WHERE id = 1;",new BeanHandler<Persons>(Persons.class)); System.out.println(persons); } //查找 返回实体类集合 第三个参数是 BeanListHandler 返回一个实体类型集合 public void selectOnelist() throws Exception { List<Persons> list = queryRunner.query(connection,"SELECT * FROM persons",new BeanListHandler<Persons>(Persons.class)); } //查找 返回实体类集合 第三个参数是 MapListHandler 返回一个实体类型集合 public void selectOnemap() throws Exception { List<Map<String , Object >> list = queryRunner.query(connection,"SELECT * FROM persons",new MapListHandler()); } -------------------------------------------------- ________________________________________________________________________________________________________________________ ________________________________________________________________________________________________________________________ ________________________________________________________________________________________________________________________ public class JdbcUtil { private static String driver = null;// 驱动 private static String url = null;// 连接地址 private static String username = null;// 用户名 private static String password = null;// 密码 static { try { Properties props = new Properties(); InputStream ins = JdbcUtil.class.getClassLoader().getResourceAsStream("database.properties"); props.load(ins); driver = props.getProperty("jdbc.driverClassName"); url = props.getProperty("jdbc.url"); username = props.getProperty("jdbc.username"); password = props.getProperty("jdbc.password"); Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } } /** * * Description: 获取数据库连接<br/> */ public static Connection getConnection() throws Exception { return DriverManager.getConnection(url, username, password);// 硬编码 } /** * Description: 释放数据库资源<br/> */ public static void close(ResultSet rs, Statement st, Connection conn) throws Exception { if (rs != null) rs.close(); if (st != null) st.close(); if (conn != null) conn.close(); } } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DBUtils 学习笔记 , 简简单单了解,简简单单用用
最新推荐文章于 2020-10-09 00:01:19 发布