JavaWeb——Using DBUtils to realize database addition, deletion, modification and query operations

Experiment name:

Use DBUtils to implement database addition, deletion, modification and query operations                                                   

Purpose:

(1) Understand what a database connection pool is, and will use DBCP and C3P0 data sources

(2) Understand the common APIs in the DBUtils tool

(3) Learn to use the DBUtils tool to add, delete, modify and query data

Experimental content and principle:

Establish databases and tables, and use database connection pool technology and DBUtils to implement database addition, deletion, modification, and query operations.

Require:

(1) In the basic operation of JDBC, because every time the database is operated, the operation of creating and disconnecting the Connection object will be executed, and the frequent operation of the Connection object will greatly affect the access efficiency of the database and increase the amount of code. In actual development, developers usually use database connection pools to solve these problems. The use of data connection pool technology can reduce the number of creation and disconnection of database connections and improve the access efficiency of the database. The Apache organization also provides a DBUtils tool class library, which implements a simple encapsulation of JDBC and can greatly simplify the coding workload of JDBC without affecting performance.

(2) Create a jdbc database, create a users data table, including id, name, password attributes, input records, and use DBUtils database jdbc to add, delete, modify, and query operations.

(3) Load the Junit4 test unit for testing.

Experimental equipment and experimental procedures:

1. Take a screenshot of the established users table and paste it below.

2. Add the c3p0 jar package and c3p0-config.xml file in the project, and build the path, intercept the project structure diagram and paste it below

3. Create the DBUtilsDao class and create a method to insert a record in it

code show as below:

public class DBUtilsDao {
    @Test
    public void testInsert() throws SQLException {
        //1 建立数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //2 创建DBUtils中的QueryRunner对象
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //3 增加记录
        queryRunner.update("insert into user value(null,?,?)","Jerry","abc111");
        queryRunner.update("insert into user value(null,?,?)","Tony","xyz222");
    }

4. Create the method of modifying records in the DBUtilsDao class

code show as below:

@Test
public void testUpdate() throws SQLException {
    //1 建立数据源
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //2 创建DBUtils中的QueryRunner对象
    QueryRunner queryRunner = new QueryRunner(dataSource);
    //3 修改更新
    queryRunner.update("update user set password=? where id=?","abc222",2);
}

5. Create a method for deleting records in the DBUtilsDao class

code show as below:

  @Test
    public void testDelete() throws SQLException {
        //1 建立数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //2 创建DBUtils中的QueryRunner对象
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //3 删除记录
        queryRunner.update("delete from user where id=? ",1);
    }

6. The method of creating query records in the DBUtilsDao class

code show as below:
 

  @Test
    public void testSelect() throws SQLException {
        //1 建立数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //2 创建DBUtils中的QueryRunner对象
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //3 查询单个对象
        User user1 =queryRunner.query("select * from user where id=?",new BeanHandler<User>(User.class),2);
        System.out.println(user1.toString());
    }
}

7. Screenshot of the running result graph:

(1) Run the testInsert() method.

(2) Run the testUpdate() method.

(3) Run the testDelete() method.

(4) Run the testSelect() method.

Questions and thoughts:

Through this study, I have mastered the establishment of databases and tables, and can independently complete the operation of adding, deleting, modifying and checking the database using database connection pool technology and DBUtils. When completing the project, the password does not appear when the query is encountered. After adjusting the getPassword() and setPassword() methods of the password of the User class, the password appears normally.

Guess you like

Origin blog.csdn.net/Liwo4418/article/details/129669977