Druid连接数据库,DBUtils工具类的使用
1.在maven中添加Druid依赖
com.alibaba
druid
1.2.0
2.封装Druid连接方法
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class DruidCon {
private static DruidDataSource dataSource = null;
static {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getDruidConnection() throws Exception {
Connection con = dataSource.getConnection();
return con;
}
}
3.使用自己写的工具类进行测试
import com.jdbc.dao.BaseDao;
import com.pyc.dao.Book;
import java.sql.Connection;
import java.util.List;
public class DruidTest01 {
public static void main(String[] args) throws Exception {
//1.连接数据库池
Connection con = DruidCon.getDruidConnection();
String sql = "select id,bname,price,publishTime from book";
List books = BaseDao.excuteSelectAll(con, Book.class, sql);
books.forEach(System.out::println);
}
}
4.使用apache的DBUtils进行测试
在maven中配置依赖
commons-dbutils
commons-dbutils
1.7
注:QueryRunner对象用来执行sql语句,ResultSetHandler的具体实现类用来存储结果集数据
ResultSetHandler的实现类:
import com.pyc.dao.Book;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
public class DruidTest02 {
public static void main(String[] args) {
Connection con = null;
try {
//1.获取数据库连接
con = DruidCon.getDruidConnection();
//2.创建QueryRunner对象
QueryRunner runner = new QueryRunner();
String sql = "insert into book(bname,price,publishTime) values(?,?,?)";
//3.创建对应ResultSetHandler的实现类
//创建一个BeanHandler用于存储插入数据,该实现类是存储自定义对象的
//插入一条数据
BeanHandler beanHandler = new BeanHandler(Book.class);
Book book = runner.insert(con, sql, beanHandler, "演员的自我休养", 39.9, new Date(52213165461L));
System.out.println("成功插入了" + book);
//查询一条数据
BeanHandler beanHandler2 = new BeanHandler(Book.class);
String sql2 = "select id,bname,price,publishTime from book where id=?";
Book book1 = runner.query(con, sql2, beanHandler2, 20);
System.out.println(book1);
//查询所有数据
BeanListHandler bookBeanListHandler = new BeanListHandler<>(Book.class);
String sql3 = "select id,bname,price,publishTime from book";
List bookList = runner.query(con, sql3, bookBeanListHandler);
bookList.forEach(System.out::println);
//特殊查询
ScalarHandler scalarHandler = new ScalarHandler();
String sql4 = "select count(*) from book";
Long bookNum = runner.query(con,sql4, scalarHandler);
System.out.println(bookNum);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//5.关闭资源
DbUtils.close(con);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}