版权声明:本文为博主转载文章。 https://blog.csdn.net/qq_35495339/article/details/89424545
原生的方式:自己写jdbc工具类
public class JDBCUtils {
public static String URL; //数据库地址
public static String USERNAME; //账户名
public static String PASSWORD; //密码
static { //静态代码块 加载JDBC驱动 并从配置文件中读取数据库url 用户名与密码
try {
Class.forName("com.mysql.jdbc.Driver"); //加载JDBC驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//从配置文件中中读取
Properties properties =new Properties();
BufferedReader bufferedReader=null;
try {
bufferedReader = new BufferedReader(new FileReader("D:\\IDEAProject\\firstWeb\\src\\com\\sunwenxu\\utils\\jdbc.properties"));//此处为你的配置文件绝对地址
properties.load(bufferedReader);
// 获取key对应的value值
URL=properties.getProperty("jdbc.url");
USERNAME=properties.getProperty("jdbc.userName");
PASSWORD=properties.getProperty("jdbc.password");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//获取连接对象 供外部使用
public static Connection getConnection(){
Connection connection=null;
try {
connection= DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//重载的三个数据库连接关闭方法
public static void close(Connection connection){
close(connection,null);
}
public static void close(Connection connection, Statement statement){
close(connection,statement,null);
}
public static void close(Connection connection, Statement statement, ResultSet rs){
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement !=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection !=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
配置文件(jdbc.properties)如下
jdbc.url=jdbc:mysql://localhost:3306/mydb01
jdbc.userName=root
jdbc.password=12345
使用以获取user为例
public User getUserById(String id) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
connection = JDBCUtils.getConnection(); //从工具类中获取连接对象
String sql = "select * from tb_user where userId = ?"; //编写sql语句
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id); //字符串拼接
resultSet = preparedStatement.executeQuery();//执行查询语句
/*PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1, money);
preparedStatement.setInt(2, account.getAccountId());
row = preparedStatement.executeUpdate();*/
while (resultSet.next()) {
user = new User();
//rs .getXX方法中参数:(1)字段名,(2)序号(从1开始)
String userId = resultSet.getString(1);
String userName = resultSet.getString(2);
String password = resultSet.getString(3);
user.setUserId(userId);
user.setUserName(userName);
user.setPassword(password);
}
} catch(SQLException e)
{
e.printStackTrace();
}finally {
JDBCUtils.close(connection, preparedStatement, resultSet); //断开连接
}
return user;
}
进阶1 (使用DbUtils工具类帮助我们简化dao层的操作)
配置文件和我们自己封装的jdbc工具类不用改
以修改上面获取user方法为例演示如何使用DbUtils来简化我们的操作
public User getUserByIdv2(String id) {
Connection connection = null;
User user = null;
try {
connection = JDBCUtils.getConnection();
String sql = "select * from tb_user where userId = ?";
Object[] params={id}; //参数
QueryRunner qr = new QueryRunner(); //改动 使用QueryRunner帮助我们执行sql语句并返回指定格式的对象
user = qr.query(connection, sql, new BeanHandler<>(User.class),params);
} catch(SQLException e)
{
e.printStackTrace();
}finally {
try {
DbUtils.close(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
进阶2 使用c3p0帮助我们连接维护数据库连接节省资源
创建c3p0配置文件c3p0-config.xml(程序会自动去读取)
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>//驱动 不需要改动
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb01</property> //自己需要连接的数据库
<property name="user">root</property> //用户名
<property name="password">123456</property>// 密码
<property name="initialPoolSize">5</property>//初始化连接池数量
<property name="maxPoolSize">20</property>//最大连接数量
</default-config>
</c3p0-config>
以修改上面获取user方法为例演示如何使用c3p0和DbUtils来简化我们的操作
public User getUserByIdv3(String id) {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = null;
User user = null;
try {
connection = comboPooledDataSource.getConnection();
String sql = "select * from tb_user where userId = ?";
Object[] params={id};
QueryRunner qr = new QueryRunner();
user = qr.query(connection, sql, new BeanHandler<>(User.class),params);
} catch(SQLException e)
{
e.printStackTrace();
}
return user;
}
如何在不适用框架的情况下添加jar包请查看如何在项目中添加jar包
持续更新中。。。