import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class DbUtils {
private static Properties properties = new Properties();
static {
try {
// io读
FileReader fileReader = new FileReader("src/jdbc.properties");
// 集合加载数据
properties.load(fileReader);
} catch (IOException e) {
e.printStackTrace();
}
// 驱动地址
String path = properties.getProperty("path");
// 加载驱动
try {
Class.forName(path);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获得连接的方法
public static Connection getConnection() {
// 获取连接
// 连接heighschool库
String url = properties.getProperty("url");
// 用户名
String user = properties.getProperty("user");
// 密码
String password = properties.getProperty("password");
// 连接获得
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
// 返回连接
return connection;
}
// 关闭连接的方法
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
// 由小到大的关
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
JDBC 封装工具类 从配置中获取数据 将数据填入所需位置
原创
©著作权归作者所有:来自51CTO博客作者鲸鱼编程pyhui的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
封装一个工具类,拒绝重复代码!
封装一个工具类,拒绝重复代码!
List 字段 工具类 -
SwiftyStoreKit 封装工具类
封装意义: struct和class的区别: 成员属性私有化: 优点1,控制成员的读写:设置name可读可写:#include<iostream>
c++ java 算法 Powered by 金山文档 构造函数 -
ubuntu加速android虚拟机
文章目录一、开发环境二、平台内容三、 VMware安装四、创建虚拟机五、安装镜像系统 所采用的平台为正点原子已经搭建好的,目的是快速体验嵌入式Liunx的开发,后面自己会一步一步地亲手把 Ubuntu 平台搭建完成。一、开发环境 软件: VMware Workstation 16 Pro 版本
ubuntu加速android虚拟机 ubuntu linux 运维 VMware