1.导入相关jar包

2.新建连接通过配置文件方式(传统方式)
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/bookdb?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC username=root password=123456
public class Main {
public static void main(String[] args) {
Main main1=new Main();
main1.getConn();
}
public void getConn() {
Connection connection=null;
try {
//1.加载配置文件将配置文件加载到Properties对象中
Properties pro=new Properties();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pro.load(resourceAsStream);
//2.通过工厂方式创建连接池,将值交给连接池
DataSource dataSource = DruidDataSourceFactory.createDataSource(pro);
//3.建立连接
connection = dataSource.getConnection();
System.out.println(connection);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
3.新建连接通过配置文件方式(优化方式)
由于新建连接池只需要建立一个,避免冗余采用静态代码块方式建立连接和关闭工具类。
public class Utils_Connection {
private static DataSource dataSource;
static {
try {
//由于只需要创建一个数据库连接池
Properties properties=new Properties();
properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
public static void getConnectionClose(Connection connection){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
4.通过Druid连接实现对数据库的操作
通过OMR编程思想
QueryRunner queryRunner=new QueryRunner(); //通过实例方式调用不同的方法实现对数据库的增删改查。
public class Update {
com.company.Dao.Update update=new UpdateImpl();
//1.向数据库中插入一条数据
public void Insert(User_table user_table){
update.insert(user_table);
}
//2.向数据库中更新一条数据
public void Update(User_table user_table){
update.update(user_table);
}
//3.向数据库中删除一条字段
public void Delete(int id) {
update.delete(id);
}
//4.向数据库中查询对象类型类型返回值
public User_table UserObject(int id){
return update.UserObject(id);
}
//5.向数据库中查询批量对象类型返回值
public List<User_table> UserObjectList(int id){
return update.UserObjectList(id);
}
//向数据库中查询特定返回值
public Long UserObjectInt(){
return update.UserObjectInt();
}
}
public interface Update {
public void insert(User_table user_table);
public void update(User_table user_table);
public void delete(int id);
User_table UserObject(int id);
List<User_table> UserObjectList(int id);
Long UserObjectInt();
}
public class UpdateImpl implements Update{
public void insert(User_table user_table) {
Connection connection=null;
try {
QueryRunner queryRunner=new QueryRunner();
connection = Utils_Connection.getConnection();
String sql="INSERT INTO user_table VALUES(?,?,?)";
int update = queryRunner.update(connection,sql,user_table.getId(),user_table.getUsername(),user_table.getPassword());
System.out.println(update);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
/*try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}*/
if (connection!=null)
Utils_Connection.getConnectionClose(connection);
}
}
public void update(User_table user_table){
Connection connection = null;
try {
QueryRunner queryRunner=new QueryRunner();
connection = Utils_Connection.getConnection();
String sql="UPDATE user_table SET username=? WHERE id =?";
int admin1 = queryRunner.update(connection, sql, user_table.getUsername(),user_table.getId());
System.out.println(admin1);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
@Override
public void delete(int id) {
Connection connection=null;
try {
QueryRunner queryRunner=new QueryRunner();
connection = Utils_Connection.getConnection();
String sql="DELETE FROM user_table WHERE id=?";
int update = queryRunner.update(connection,sql,id);
System.out.println(update);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (connection!=null){
Utils_Connection.getConnectionClose(connection);
}
}
}
@Override
public User_table UserObject(int id) {
Connection connection = null;
try {
QueryRunner queryRunner=new QueryRunner();
connection = Utils_Connection.getConnection();
String sql="SELECT id,username,password FROM user_table WHERE id=?";
BeanHandler<User_table> beanHandler=new BeanHandler<>(User_table.class);
User_table query = queryRunner.query(connection, sql, beanHandler, id);
return query;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
Utils_Connection.getConnectionClose(connection);
}
return null;
}
@Override
public List<User_table> UserObjectList(int id) {
Connection connection = null;
List<User_table> query = null;
try {
QueryRunner queryRunner=new QueryRunner();
connection = Utils_Connection.getConnection();
String sql="SELECT id,username,password FROM user_table WHERE id>?";
BeanListHandler<User_table> beanListHandler=new BeanListHandler<>(User_table.class);
query = queryRunner.query(connection, sql, beanListHandler, id);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
Utils_Connection.getConnectionClose(connection);
}
return query;
}
@Override
public Long UserObjectInt() {
Connection connection = null;
Object query=null;
try {
QueryRunner queryRunner=new QueryRunner();
connection = Utils_Connection.getConnection();
String sql="SELECT COUNT(*) FROM user_table";
ScalarHandler scalarHandler=new ScalarHandler();
query = queryRunner.query(connection, sql, scalarHandler);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
Utils_Connection.getConnectionClose(connection);
}
return (Long) query;
}
}
public class Test {
Update update=new Update();
@org.junit.Test
public void insertTest(){
User_table user_table=new User_table();
user_table.setUsername("admin2");
user_table.setPassword("123123");
update.Insert(user_table);
}
@org.junit.Test
public void updatetTest(){
User_table user_table=new User_table();
user_table.setId(3);
user_table.setUsername("admin1");
update.Update(user_table);
}
@org.junit.Test
public void deleteTest(){
int id=5;
update.Delete(id);
}
@org.junit.Test
public void selectObjectTest(){
User_table user_table = update.UserObject(3);
System.out.println(user_table.toString());
}
@org.junit.Test
public void selectObjectListTest(){
List<User_table> user_table = update.UserObjectList(2);
System.out.println(user_table.toString());
}
@org.junit.Test
public void selectObjectIntTest(){
Long i = update.UserObjectInt();
System.out.println(i);
}
}
5.针对4中的查询方法中ResultSetHandler接口及实现类
ArrayHandler:把结果集中的第一行数据转成对象数组。
ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。
BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。
BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
ColumnListHandler:将结果集中某一列的数据存放到List中。
KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里,再把这些map再存到一个map里,其key为指定的key。
MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
ScalarHandler:查询单个值对象
本文介绍了如何使用Druid数据库连接池,从导入jar包开始,详细讲解了通过配置文件的传统与优化方式创建连接,利用静态代码块实现连接池的初始化和关闭。接着展示了Druid连接进行数据库操作的方法,特别是通过ORM编程思想。最后,文章详细阐述了ResultSetHandler的各种实现类,如ArrayHandler、ArrayListHandler、BeanHandler等,以及它们在处理查询结果集时的不同作用。
1955

被折叠的 条评论
为什么被折叠?



