weixin_38998783 2019-11-22 23:02 采纳率: 0%
浏览 240

QueryRunner的无connection是怎样获得连接的???

代码在下面已经给出 2 种方式查询到的结果一样,那么QueryRunner的无connection是怎样获得连接的???

//需要用到的工具类
public class DBCPUtils {
private static DataSource dataSource;
static{

    try {
        //1.加载找到properties文件输入流
        InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties");
        //2.加载输入流
        Properties props = new  Properties();
        props.load(is);

        //3.创建数据源  基本数据源工厂
        dataSource = BasicDataSourceFactory.createDataSource(props);
    } catch (Exception e) {
        throw  new RuntimeException(e);
    }

}

public static DataSource getDataSource(){
    return dataSource;  
}

public static Connection getConnection6666(){
    try {
        return dataSource.getConnection();
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }

}

//有Connection 参数的
@Test
public void testQueryAll(){

    try {
        Connection conn = DBCPUtils.getConnection6666();
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
        String sql = "select * from tbl_user";
        List<User> users = qr.query(conn, sql, new BeanListHandler<>(User.class));
        for (User user : users) {
            System.out.println(user.getUname()+"   "+user.getUpassword());
        }


    } catch (SQLException e) {
        throw new RuntimeException(e);

    }

//没有Connection 参数的
public void testQueryAll1(){

    try {
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
       String sql = "select * from tbl_user";
        List<User> users = qr.query( sql, new BeanListHandler<>(User.class));

        for (User user : users) {
            System.out.println(user.getUname()+"   "+user.getUpassword());
        }


    } catch (SQLException e) {
        throw new RuntimeException(e);

    }

}
  • 写回答

1条回答 默认 最新

  • 明初啥都能学会 2024-04-18 22:04
    关注
    //需要用到的工具类
    public class DBCPUtils {
        private static DataSource dataSource;
        static {
            try {
                //1.加载找到properties文件输入流
                InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties");
                //2.加载输入流
                Properties props = new  Properties();
                props.load(is);
     
                //3.创建数据源  基本数据源工厂
                dataSource = BasicDataSourceFactory.createDataSource(props);
            } catch (Exception e) {
                throw  new RuntimeException(e);
            }
        }
     
        public static DataSource getDataSource(){
            return dataSource;  
        }
     
        public static Connection getConnection6666(){
            try {
                return dataSource.getConnection();
            } catch (SQLException e) {
                throw  new RuntimeException(e);
            }
        }
    }
     
    public class TestQuery {
        @Test
        public void testQueryAll(){
            try {
                Connection conn = DBCPUtils.getConnection6666();
                QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
                String sql = "select * from tbl_user";
                List<User> users = qr.query(conn, sql, new BeanListHandler<>(User.class));
                for (User user : users) {
                    System.out.println(user.getUname()+"   "+user.getUpassword());
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
     
        public void testQueryAll1(){
            try {
                QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
                String sql = "select * from tbl_user";
                List<User> users = qr.query(sql, new BeanListHandler<>(User.class));
     
                for (User user : users) {
                    System.out.println(user.getUname()+"   "+user.getUpassword());
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突
  • ¥15 如何在线硕士了解,广告太多,希望有真实接触过的人回答下?(标签-学习|关键词-在线硕士)
  • ¥15 zabbix6.4与frp如何进行联动
  • ¥15 如何使用Echarts制作途中时间序列表
  • ¥15 图论相关的数学问题,共10个
  • ¥15 EtherCAT的问题,创建一个XML文件