java-DButils simplified operation JBDC

DButils implement CRUD

/**
 * 测试DButils的增删改
 */
public class DBUtilsTest {

    /**
     * DButils 添加
     */
    @Test
    public void testadd(){
        try{
            QueryRunner qr = new QueryRunner();
            String sql = "insert into t1 value(?,?)";
            Object[] params = {9,"hhh"};
            Connection conn = C3P0utils.getConnection();
            int rows = qr.update(conn,sql,params);
            if (rows > 0) {
                System.out.println("添加成功!");
            } else {
                System.out.println("添加失败!");
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    /**
     * DButils 更改
     */
    @Test
    public void testupdate(){
        try{
            QueryRunner qr = new QueryRunner();
            String sql = "update t1 set name=? where id = ?";
            Object[] params = {"sxf",9};
            Connection conn = C3P0utils.getConnection();
            int rows = qr.update(conn,sql,params);
            if (rows > 0) {
                System.out.println("修改成功!");
            } else {
                System.out.println("修改失败!");
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    /**
     * DButils 删除
     */
    @Test
    public void testdel(){
        try{
            QueryRunner qr = new QueryRunner();
            String sql = "delete from t1 where id = ?";
            Object[] params = {7};
            Connection conn = C3P0utils.getConnection();
            int rows = qr.update(conn,sql,params);
            if (rows > 0) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}

 


DButils inquiry   

BeanListHandler query all devices list
BeanHandler specify the first day of the inquiry information
Data for single operation ScalarHandler
public class DBUtilsTest2 {
    /**
     * 查询所有
     */
    @Test
    public void testQueryAll() {
        try{
            QueryRunner qr = new QueryRunner();
            String sql = "select * from t1";
            Connection conn = C3P0utils.getConnection();
            List<user> users = qr.query(conn,sql,new BeanListHandler<user>(user.class));
            for(user u:users){
                System.out.println(u.getId() + " : " +u.getName ()); 
            } 
        } the catch (SQLException E) {
             the throw  new new a RuntimeException (E); 
        } 
    } 

    / * 
     * The method according to the querying user id 
     * / 
    @Test 
    public  void testQueryUserById () {
         the try {
             // 1. Obtaining Core class queryRunner 
            QueryRunner QR = new new QueryRunner (C3P0utils.getDataSource ());
             // 2. write sql statement 
            String sql = "SELECT * from T1 WHERE ID =?" ;
             // 3. placeholder setting value 
            Object [] params 5 =};
             // 4 performs a query operation 
            User User qr.query = (SQL, new new BeanHandler <User> (User. Class ), the params); 
            System.out.println (user.getId () + ":" + User. getName ()); 
        } the catch (SQLException E) {
             the throw  new new a RuntimeException (E); 
        } 
    } 

    / * 
     * the total number of all users 
     * / 
    @Test 
    public  void testQueryCount () {
         the try {
             // 1. Get core classes queryRunner 
            QueryRunner QR = new newQueryRunner (C3P0utils.getDataSource ());
             // 2. write sql statement 
            String sql = "the SELECT COUNT (*) from T1" ;
             // 4. execute the query operation 
            Long COUNT = (Long) qr.query (sql, new new ScalarHandler ()); 
            System.out.println (COUNT); 
        } the catch (SQLException E) {
             the throw  new new a RuntimeException (E); 
        } 
    } 


}

 

Guess you like

Origin www.cnblogs.com/zhuzhiwei-2019/p/11300618.html