本文主要是介绍【javaweb】采用ResourceBundle进行数据库配置文件读取,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package jdbc.utils;
/*** 采用ResourceBundle进行数据库配置文件读取,从而创建出一个用于数据库连接和关闭的工具类* ResourceBundle类方便与获取数据库的配置文件,比之前我采用的通过获取IO流的方式更加地简便!* 所以我极力推进!* @author Administrator**/
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 String driver;private static String url;private static String username;private static String password;static {//获取配置文件中的数据//注意:db为配置文件的名称,全名为db.propertiesResourceBundle bundle = ResourceBundle.getBundle("db");driver=bundle.getString("driver");url=bundle.getString("url");username=bundle.getString("username");password=bundle.getString("password");}/*** 获取数据库连接的方法* @return*/public static Connection getConnection() {Connection conn=null;//1.加载驱动try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}//2.获取链接try {conn=DriverManager.getConnection(url,username, password);} catch (SQLException e) {e.printStackTrace();}return conn;}/*** 关闭数据库资源的方法* @param rs* @param ps* @param conn*/public static void release(ResultSet rs,PreparedStatement ps,Connection conn) {if(rs!=null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if(ps!=null) {try {ps.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}
}
这篇关于【javaweb】采用ResourceBundle进行数据库配置文件读取的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!