DButils数据库查询

DButils使用示例:

1-3. 查询
4-5. 插入
6 . 删除
7 . 修改

1:查询单个记录,返回实体Bean

User user = qr.query(conn,  
                "select * from user where username='admin'", new BeanHandler<User>(User.class)); 

2:查找单个记录,返回Map集合

Map<String, Object> map = qr.query(conn, "SELECT * FROM user where username=?", new MapHandler(),"admin");

3:查找所有记录,返回List集合

List<User> list = (List) qr.query(conn, "SELECT * FROM user", new BeanListHandler(User.class));

4:插入一条数据

qr.update(conn, "insert into user (username,password) values (?,?)","admin3", "1234");  

5:插入多条数据

qr.batch(conn,  "insert into user (username,password) values (?,?)",new Object[][] { {"admin4", "1234"}, {"admin5", "1234"} });

6:删除

//删除单条记录(SQL语句中带好条件)
qr.update(conn, "delete from user where username='admin2'");

//删除单条记录(SQL语句中条件预处理)
qr.update(conn, "delete from user where username=?", "admin3");

//删除多条记录
qr.batch(conn, "delete from user where username=?", new Object[][] { { "admin5" }, { "admin4" } }); 

7:修改一条记录

qr.update(conn,"update user set password = ? where username=?", "set修改的内容", "where条件");

你可能感兴趣的:(数据库,jdbc,查询,select,DbUtils)