JDBC通过加载配置文件连接数据库

时间:2021-02-09 19:06:07

我们把数据库的连接封装成工具类,数据库参数设置成配置文件,这里我的配置文件名为“mysql.properties”,配置文件中的内容为:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybase

username=root

password=123

---------------------------------------------------------------------------------------------------------------------

driver:数据库厂商提供的驱动驱动路径

url:数据库路径

username & password:数据库用户名和密码

--------------------------------------------------------------------------------------------------------------------------

加载配置文件有两种方式,一种是通过ResourceBundle.getBundle("配置文件名称")方法,另一种是把配置文件转换成流对象,并通过Properties中的load方法加载到Properties对象中,下面分别进行演示:

-----------------------------------------------------------------------------------------------------------------------

1.ResourceBundle.getBundle("配置文件名称")方法

package cn.czr.JDBCUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class JDBCUtils_V2 {
	private static final String DRIVER;
	private static final String URL;
	private static final String USERNAME;
	private static final String PASSWORD;
	//通过ResourceBundle加载配置文件
	static {
		ResourceBundle bundle = ResourceBundle.getBundle("mysql");
		DRIVER = bundle.getString("driver");
		URL = bundle.getString("url");
		USERNAME = bundle.getString("username");
		PASSWORD = bundle.getString("password");
	}
	//获取连接方法
	public static Connection getConnection() {
		try {
			//加载驱动类注册驱动
			Class.forName(DRIVER);
			//连接数据库获取连接对象
			Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			return con;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	//释放资源方法
	public static void release(Connection con,PreparedStatement pst,ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if(pst != null) {
			try {
				pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if(con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}
}

2.通过Properties中的load方法

package cn.czr.JDBCUtils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils_V3 {
	private static final String DRIVER;
	private static final String URL;
	private static final String USERNAME;
	private static final String PASSWORD;
	//通过Properties加载配置文件
	static {
		try {
			//获得文件的输入流
			InputStream is = JDBCUtils_V3.class.getClassLoader().getResourceAsStream("mysql.properties");
			Properties pro = new Properties();
			//加载到Properties对象中
			pro.load(is);
			//通过Properties对象获取配置信息
			DRIVER = pro.getProperty("driver");
			URL = pro.getProperty("url");
			USERNAME = pro.getProperty("username");
			PASSWORD = pro.getProperty("password");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	//获取连接方法
	public static Connection getConnection() {
		try {
			//加载驱动类注册驱动
			Class.forName(DRIVER);
			//连接数据库获取连接对象
			Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			return con;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	//释放资源方法
	public static void release(Connection con,PreparedStatement pst,ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if(pst != null) {
			try {
				pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if(con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}
}

测试:

下面是使用上述工具类连接数据库并执行sql语句的测试:

package cn.czr.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import cn.czr.JDBCUtils.JDBCUtils_V3;

public class JDBCUtils_test {
	
	@Test
	public void test() {
		Connection con = null;
		PreparedStatement pst = null;
		try {
			//获取连接对象
			con = JDBCUtils_V3.getConnection();
			//获取预编译执行对象
			String sql = "update sort set s_name = ? where s_id = ?;";
			pst = con.prepareStatement(sql);
			//设置参数
			pst.setString(1, "轿车");
			pst.setInt(2, 7);
			//执行sql语句
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//释放资源
			JDBCUtils_V3.release(con, pst, null);
		}
	}
}