并使用了读取工程下的JDBC.Properties文件,这样只需要修改文件中的信息,实现了代码的复用。
public class JDBCUitl2 {
private JDBCUitl2(){
}
private static Connection connection;
private static Properties properties;
private static Properties readFile() {
// 通过类加载器获取bin文件夹下的文件的字节流
InputStream is = JDBCUitl2.class.getClassLoader().getResourceAsStream("JDBC.Properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
static {
// 只读一次文件
properties = readFile();
try {
Class.forName(properties.getProperty("driverClass"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Registration driver failure");
}
}
public static Connection getConnection() {
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("connection failure");
}
return connection;
}
//关闭资源
public static void JDBCClose(Connection connection, java.sql.Statement statement, ResultSet rs) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
然后需要获取连接时 直接使用 JDBCUitl2.getConnection();就行了
commons-dbutils-1.6.jar(提供快速操作数据库方法)
private static void fun1() throws SQLException {
// 插入
// 创建查询对象
QueryRunner queryRunner = new QueryRunner();
Connection conn = JDBCUitl2.getConnection();
String sql = "insert into sort values (null,?,?,?)";
// 创建一个数组
Object[] params = {"mac",20,"呜呜呜,百日做梦就完事了嗷"};
int row = queryRunner.update(conn, sql, params);
System.out.println(row);
DbUtils.closeQuietly(conn);
}
String sql = " select * from sort ";
QueryRunner queryRunner = new QueryRunner();
Connection conn = JDBCUitl2.getConnection();
/*
* 测试ArrayHandler
* 默认返回第一条数据
* 把该数据放入object数组中返回
*/
ArrayHandler rsh = new ArrayHandler();
System.out.println(rsh);
Object[] query = queryRunner.query(conn, sql, rsh);
for (Object object : query) {
System.out.println(object);
}
DbUtils.closeQuietly(conn);
commons-dbcp-1.4.jar包下
public class DataSourceUtil {
// 创建连接池对象
private static BasicDataSource dataSource = new BasicDataSource();
// 使用静态代码块 对数据库连接池 进行设置
static {
// 基础设置
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/wljdbc01");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// 扩展设置
// 初始化连接数
dataSource.setInitialSize(10);
// 设置最大连接数
dataSource.setMaxActive(10);
// 最大空闲连接数 最多空闲多少个连接 多余的会被系统销毁
dataSource.setMaxIdle(5);
// 设置最小空闲连接数 少于最小空闲 系统会帮你创建出连接
dataSource.setMinIdle(1);
}
private DataSourceUtil() {
}
// 获取数据库连接池的方法
public static DataSource getDataSource() {
return dataSource;
}
}
/*
* 测试DataSource工具类
*
* DBUtile中的QueryRunner 配合DataSource一起使用
*/
public class Demo05 {
public static void main(String[] args) throws SQLException {
QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
String sql = "select * from sort";
BeanListHandler<Sort> beanListHandler = new BeanListHandler<Sort>(Sort.class);
List<Sort> query = queryRunner.query(sql, beanListHandler);
for (Sort sort : query) {
System.out.println(sort);
}
}
}