代码
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC工具类 使用Durid连接池
*/
public class JDBCUtils {
private static DataSource ds;
static {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//2.初始化连接池对象
try {
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDataSource() {
return ds;
}
/**
* 获取连接Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
{lamp/}
代码2
package com.icis.util;
import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;
//数据库工具类 读取属性文件 获得一个数据库连接池
public class JDBCUtils {
private static Properties pp;
//静态代码块 读取属性文件
static {
try{
pp=new Properties();
InputStream ins = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
//把流加载到 pp中
pp.load(ins);
}catch (Exception e){
e.printStackTrace();
System.out.println("数据读取失败.....");
}
}
//创建一个数据库连接池对象
public static DataSource getDataSource(){
DruidDataSource ds=new DruidDataSource();
//给数据库连接池设置属性 连接哪一个数据库
ds.setDriverClassName(pp.getProperty("driverClassName"));
ds.setUrl(pp.getProperty("url"));
ds.setUsername(pp.getProperty("username"));
ds.setPassword(pp.getProperty("password"));
ds.setInitialSize(Integer.parseInt(pp.getProperty("initialSize")));
ds.setMaxActive(Integer.parseInt(pp.getProperty("maxActive")));
ds.setMaxWait(Long.parseLong(pp.getProperty("maxWait")));
return ds;
}
}
在需要使用的地方创建对象
//声明JDBCTemplate对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
对象调方法即可
template.
例如查询用户并返回一行数据,存储到List集合中
public List<User> getList() {
//查询数据库
String listusersql = "SELECT * FROM USER";
List<User> userList=template.query(listusersql, new BeanPropertyRowMapper<User>(User.class) {
@Override
//返回类型User
public User mapRow(ResultSet resultSet, int i) throws SQLException {
//赋值,取值
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String gender = resultSet.getString("gender");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
String qq = resultSet.getString("qq");
String email = resultSet.getString("email");
//给对象设置属性
User user = new User(id, name, gender, age, address, qq, email);
return user;
}
});
System.out.println(userList);
return userList;
}
druid.properties配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/day17
username=root
password=12345678
initialSize=5
maxActive=10
maxWait=3000
{message type="success" content="druid.properties一般存放的位置"/}
{message type="info" content="jdbc依赖包有以下"/}