DBUtils(增删改查)

1:如果只使用JDBC进行开发,会导致冗余代码过多,为了简化JDBC开发,采用apache commons组件一个成员:DBUtils。
DBUtils就是JDBC的简化开发工具包。需要项目导入commons-dbutils-1.4.jar(还有更高的版本)才能够正常使用DBUtils工具。

2:首先在Maven工程下导入jar包,在pom.xml下,找到最底下的一对,在它们中间输入然后输入commons-dbutils,选择1.4版本.

3:Dbutils三个核心功能介绍

●QueryRunner中提供对sql语句操作的API.
●ResultSetHandler接口,用于定义select操作后,怎样封装结果集.
●DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法

QueryRunner中提供对sql语句操作的API.
update(Connection conn, String sql, Object... params) ,用来完成表数据的增加、删除、更新操作
query(Connection conn, String sql, ResultSetHandler rsh, Object... params) ,用来完成表数据的查询操作

4:增删改查
@增加:

```
public class dbs {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="insert into stu(name,age,sex) values(?,?,?)";
        Connection connection = JdbcUtil.getConnection();
        Object []objects={"王芝洋",21,"女"};
        int update = qr.update(connection, sql, objects);
    }
```

注意:如果不设置id,就会自动在第一行前面设置第0行,加入此条语句,但是只能操作一次,如果想再加入,就必须在最后一行增加语句,找到最后一行的id,将id加入sql语句,如下图,在最后一行(12)行加入id:

```
public class dbs {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="insert into stu(id,name,age,sex) values(?,?,?,?)";
        Connection connection = JdbcUtil.getConnection();
        Object []objects={12,"王芝洋",100,"女"};
        int update = qr.update(connection, sql, objects);
    }
```

@修改:
比如将第二个id的名字修改为“王博聪”,年龄修改为111岁,代码如下:

```
public class dbsup {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="update stu set name=?,age=? where id=?";
        Connection connection = JdbcUtil.getConnection();
        Object []objects={"王博聪",111,2};
        int update = qr.update(connection, sql, objects);
    }
```

@删除
删除id=12的信息

```
public class dbsup {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="delete from stu where id=?";
        Connection connection = JdbcUtil.getConnection();
        Object []objects={12};
        int update = qr.update(connection, sql, objects);
    }
```

@查询
●ArrayHandler:将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值

```
public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from stu";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={};
        Object[] objects = qr.query(connection, sql, new ArrayHandler(), parms);
        System.out.println(Arrays.toString(objects));
        connection.close();
    }
```

结果如下:

```
[0, 王芝洋, 21, 女, null]
```

●ArrayListHandler:将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组再封装到List集合中。(查询全部语句)

public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from stu";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={};
        List list = qr.query(connection, sql, new ArrayListHandler(), parms);
        for (Object [] objects:list) {
            System.out.println(Arrays.toString(objects));
        }
        connection.close();
    }
}

查询结果:

[0, 王芝洋, 21, 女, null]
[1, 王博聪, 90, 女, 1]
[2, 王博聪, 111, 男, 2]
[4, ewdwe, 66, 女, 1]
[5, ewdw, 77, 女, 1]
[6, 张得到, 23, 女, 2]
[7, 张给他, 23, 女, 2]
[8, 放入福, 23, 女, 2]
[9, 一湖光塔影, 33, 男, 1]
[11, 汉斯, 30, 男, 2]
●条件查询
```
public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from stu where age>?";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={50};
        List list = qr.query(connection, sql, new ArrayListHandler(), parms);
        for (Object [] objects:list) {
            System.out.println(Arrays.toString(objects));
        }
        connection.close();
    }
}
```

查询结果:

```
[1, 王博聪, 90, 女, 1]
[2, 王博聪, 111, 男, 2]
[4, ewdwe, 66, 女, 1]
[5, ewdw, 77, 女, 1]
```

5:BeanHandler与BeanListHandler查询
●BeanHandler :将结果集中第一条记录封装到一个指定的javaBean中。

public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from stu where age>?";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={50};
        Student student = qr.query(connection, sql, new BeanHandler(Student.class), parms);
        System.out.println(student);
        connection.close();
    }
}

查询结果:

Student{id=1,
 name='王博聪',
 age=90, 
sex='女,
 tname='null'
}

●BeanListHandler :将结果集中每一条记录封装到指定的javaBean中,将这些
javaBean在封装到List集合中。

public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from stu where age>?";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={50};
        List query = qr.query(connection, sql, new BeanListHandler(Student.class), parms);
        System.out.println(query);
        connection.close();
    }
}

查询结果:

[Student{id=1,
 name='王博聪',
 age=90, 
sex='女39,
 tname='null'
}
, Student{id=2,
 name='王博聪',
 age=111, 
sex='男39,
 tname='null'
}
, Student{id=4,
 name='ewdwe',
 age=66, 
sex='女39,
 tname='null'
}
, Student{id=5,
 name='ewdw',
 age=77, 
sex='女39,
 tname='null'
}
]

6:● ColumnListHandler:将结果集中指定的列的字段值,封装到一个List集合中
查询年龄在50岁以上的人,ColumnListHandler()里什么都不写默认的是id值。如果想查询具体的字段比如年龄name,则用ColumnListHandler("name")

public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from stu where age>?";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={50};
        List student = qr.query(connection, sql, new ColumnListHandler(), parms);
        System.out.println(student);
        connection.close();
    }
}
 
 
运行结果为:
[1, 2, 4, 5]
  ●ScalarHandler:它是用于单数据。例如select count(*) from 表操作。
查询最大年龄:输入sql语句“select Max(age) from stu”,用ScalarHandler()。
```
public class dbQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select Max(age) from stu";
        Connection connection = JdbcUtil.getConnection();
        Object [] parms={};
        Object query = qr.query(connection, sql, new ScalarHandler(), parms);
        System.out.println(query);
        connection.close();
    }
}
```
运行结果为最大年龄 :111.













你可能感兴趣的:(DBUtils(增删改查))