DBUtils的使用

DBUtils的使用

QueryRunner的插入使用

@Test
public void testInsert() throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    String insert = "insert into `demo1`(`id`,`user`) values (?,?)";

    Connection connection = null;
    try {
        Properties properties = new Properties();
        FileInputStream is = new FileInputStream("D:\\MYcode_K\\JDBC\\learn01\\src\\druid.properties");
        properties.load(is);
        // 使用德鲁伊数据池提高性能,可以使用普通JDBC
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection = dataSource.getConnection();
        int update = queryRunner.update(connection, insert, 5, "你好");
        System.out.println(update + "行受影响");
    } finally {
        connection.close();
    }
}

查询单行返回单个bean对象

实体类

package com.druid;

public class User {
    private Integer id;
    private String user;

    public User() {
    }

    public User(Integer id, String user) {
        this.id = id;
        this.user = user;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "user{" +
                "id=" + id +
                ", user='" + user + '\'' +
                '}';
    }

}

测试类

@Test
public void testQuery() throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    String query = "SELECT * FROM demo1";
    Properties properties = new Properties();
    Connection connection = null;

    try {
        FileInputStream is = new FileInputStream("D:\\MYcode_K\\JDBC\\learn01\\src\\druid.properties");
        properties.load(is);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection = dataSource.getConnection();

        // 定义返回结果格式
        // 这次我们返回一个Bean对象
        BeanHandler<User> beanHandler = new BeanHandler<>(User.class);

        User result = queryRunner.query(connection, query, beanHandler);

        System.out.println(result);
    } finally {
        connection.close();
    }
}

查询多行返回BeanList

@Test
public void testQueryList() throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    String query = "SELECT * FROM demo1";
    Properties properties = new Properties();
    Connection connection = null;

    try {
        FileInputStream is = new FileInputStream("D:\\MYcode_K\\JDBC\\learn01\\src\\druid.properties");
        properties.load(is);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection = dataSource.getConnection();

        BeanListHandler<User> listHandler = new BeanListHandler<>(User.class);

        List<User> result = queryRunner.query(connection, query, listHandler);

        for (User user : result) {
            System.out.println(user);
        }
    } finally {
        connection.close();
    }
}

查询返回单个值

@Test
public void testQueryCount() throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    String query = "SELECT count(*) FROM demo1";
    Properties properties = new Properties();
    Connection connection = null;

    try {
        FileInputStream is = new FileInputStream("D:\\MYcode_K\\JDBC\\learn01\\src\\druid.properties");
        properties.load(is);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection = dataSource.getConnection();

        ScalarHandler scalarHandler = new ScalarHandler();

        Object result = queryRunner.query(connection, query, scalarHandler);
        System.out.println(result);
    } finally {
        connection.close();
    }
}
posted @ 2022-03-09 21:10  CoderCatIce  阅读(25)  评论(0编辑  收藏  举报