String sql ; QueryRunner queryRunner ;
时间: 2024-05-31 15:09:18 浏览: 74
这段代码是在Java中使用Apache Commons DbUtils库中的QueryRunner类来执行SQL查询语句。QueryRunner是一个简化了JDBC操作的类,可以避免手动管理Connection、PreparedStatement和ResultSet等对象的创建和关闭,从而简化了JDBC代码的编写。
具体来说,第一行定义了一个字符串变量sql,用来存储SQL查询语句。第二行创建了一个QueryRunner对象queryRunner,可以通过它来执行SQL查询语句。
相关问题
{ User user = null; try { QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource()); String sql = "select * from user where username=?"; user = queryRunner.query(sql, new BeanHandler<User>(User.class), username); if (user != null) { return "true"; } else { return "false"; } } catch (Exception e) { e.printStackTrace(); } return "none"; }将此段代码中的queryRunner替换
可以使用PreparedStatement对queryRunner进行替换,示例代码如下:
```
User user = null;
try {
Connection conn = DBUtils.getConnection();
String sql = "select * from user where username=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
// ...
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
if (user != null) {
return "true";
} else {
return "false";
}
```
需要注意的是,此处需要手动关闭数据库连接,避免资源泄露。同时,代码中也需要手动从ResultSet中读取数据,再赋值给User对象,相对于BeanHandler来说,代码量稍微多一些。
public class CategoryDaoImpl implements CategoryDao { @Override public void add(Category category){ try{ QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "insert into category(id,name) values(?,?)"; Object params[] = {category.getId(), category.getName()}; runner.update(sql, params); } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } } /* (non-Javadoc) * @see dao.impl.CategoryDao#find(java.lang.String) */ @Override public Category find(String id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from category where id=?"; return (Category)runner.query(sql, id, new BeanHandler(Category.class)); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } /* (non-Javadoc) * @see dao.impl.CategoryDao#getAll() */ @Override public List<Category> getAll(){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from category"; return (List<Category>)runner.query(sql, new BeanListHandler(Category.class)); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } @Override public int delete(String id) { try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); //执行删除的sql runner.update("delete from category where id=?",id); } catch (SQLException e) { throw new RuntimeException(e);//抛出运行异常 } return 1;//删除成功返回1表示结束 } @Override public void update(Category record) { try{ QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "update category set name=?,where id=? "; Object params[] = { record.getName(), record.getId()}; runner.update(sql, params); } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } } }帮我加一下注释
```
public class CategoryDaoImpl implements CategoryDao {
// 添加分类
@Override
public void add(Category category){
try{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "insert into category(id,name) values(?,?)"; // SQL插入语句
Object params[] = {category.getId(), category.getName()}; // 插入参数
runner.update(sql, params); // 执行插入操作
} catch(Exception e){
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 根据分类ID查找分类信息
@Override
public Category find(String id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "select * from category where id=?"; // SQL查询语句
return (Category)runner.query(sql, id, new BeanHandler(Category.class)); // 执行查询并返回结果
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 获取所有分类信息
@Override
public List<Category> getAll(){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "select * from category"; // SQL查询语句
return (List<Category>)runner.query(sql, new BeanListHandler(Category.class)); // 执行查询并返回结果
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 根据分类ID删除分类信息
@Override
public int delete(String id) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
runner.update("delete from category where id=?",id); // 执行删除的SQL语句
} catch (SQLException e) {
throw new RuntimeException(e); // 抛出运行时异常
}
return 1; // 删除成功返回1表示结束
}
// 更新分类信息
@Override
public void update(Category record) {
try{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "update category set name=?,where id=? "; // SQL更新语句
Object params[] = { record.getName(), record.getId()}; // 更新参数
runner.update(sql, params); // 执行更新操作
} catch(Exception e){
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
}
```
这段代码实现了对分类信息的增、删、改、查功能。其中,每个方法都调用了QueryRunner对象执行SQL语句,并返回结果或抛出异常。方法名和注释说明了各个方法的具体功能。
阅读全文