JDBCUtils.getDataSource()是什么意思
时间: 2024-06-02 07:11:55 浏览: 48
JDBCUtils.getDataSource()是一个Java方法,它返回一个数据源对象。数据源是一个Java对象,它用于管理数据库连接。在Java应用程序中,使用JDBC连接到数据库时,数据源是一个常见的模式。使用数据源可以更方便地管理数据库连接,而不必在每个连接上进行独立的管理。这个方法通常是在Java Web应用程序中使用,它可以返回一个配置好的数据源,这样就可以在应用程序中轻松地获取数据库连接。
相关问题
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语句,并返回结果或抛出异常。方法名和注释说明了各个方法的具体功能。
public List<Movie> getPageData(int startindex, int pagesize, String category_id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from movie where category_id=? limit ?,?"; Object params[] = {category_id, startindex, pagesize}; return (List<Movie>)runner.query(sql, params, new BeanListHandler(Movie.class)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public int getTotalRecord(String category_id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select count(*) from movie where category_id=?"; long totalrecord = (Long)runner.query(sql, category_id, new ScalarHandler()); return (int)totalrecord; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //(id,name,author,price,image,description,category_id) @Override public void update(Movie movie) { try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "update movie set name=?,actor=?,price=?,poster=?,intro=?,category_id=? where id=?"; Object params[] = { movie.getName(), movie.getActor(), movie.getPrice(), movie.getPoster(), movie.getIntro(), movie.getCategory_id(),movie.getId()}; runner.update(sql, params); } 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 movie where id=?",id); } catch (SQLException e) { throw new RuntimeException(e);//抛出运行异常 } return 1;//删除成功返回1表示结束 }帮我加一下注释
好的,以下是代码的注释:
```
// 获取指定类别下的电影列表(分页)
public List<Movie> getPageData(int startindex, int pagesize, String category_id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "select * from movie where category_id=? limit ?,?"; // 查询语句
Object params[] = {category_id, startindex, pagesize}; // 查询参数
return (List<Movie>)runner.query(sql, params, new BeanListHandler(Movie.class)); // 执行查询并返回结果
} catch (Exception e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 获取指定类别下的电影总数
public int getTotalRecord(String category_id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "select count(*) from movie where category_id=?"; // 查询语句
long totalrecord = (Long)runner.query(sql, category_id, new ScalarHandler()); // 执行查询并返回结果
return (int)totalrecord; // 将结果转换为整型并返回
} catch (Exception e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 更新电影信息
@Override
public void update(Movie movie) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "update movie set name=?,actor=?,price=?,poster=?,intro=?,category_id=? where id=?"; // 更新语句
Object params[] = { movie.getName(), movie.getActor(), movie.getPrice(), movie.getPoster(), movie.getIntro(), movie.getCategory_id(),movie.getId()}; // 更新参数
runner.update(sql, params); // 执行更新操作
} 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 movie where id=?",id); // 删除操作
} catch (SQLException e) {
throw new RuntimeException(e); // 抛出运行时异常
}
return 1; // 删除成功返回1表示结束
}
```
阅读全文