日期:2014-05-16  浏览次数:20606 次

JAVA读取db.properties文件

整理整理基础,JAVA读取db.properties文件连接数据库

1.PropUtil

package xxxx;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * PropUtil
 * 
 * put the DB.properties[many] under src folder
 * 
 * @since 2012.8.30
 * @author dml
 * 
 */
public class PropUtil {

	private String filePath = null;

	public PropUtil(String fileName) {

		this.filePath = getClass().getClassLoader().getResource("/").getPath()
				+ fileName;
		// fixed tomcat readpath problem dml@2012.9.12
		filePath = filePath.replaceAll("%20", " ");
	}

	public Properties getProp() {
		Properties prop = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					filePath));
			prop.load(in);
			in.close();

		} catch (Exception err) {
			err.printStackTrace();
		}
		return prop;
	}
}
2.DBUtil

package xxxx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 
 * DBUtil
 * 
 * @author dml
 * @since2012.8.27
 */
public class DBUtil {

	public static Connection getConnect() {
		PropUtil pu = new PropUtil("DB.properties");
		Properties p = pu.getProp();
		Connection con = null;
		try {
			String url = p.getProperty("url");
			String Driver = p.getProperty("driver");
			String user = p.getProperty("userName");
			String pwd = p.getProperty("password");
			Class.forName(Driver);
			con = DriverManager.getConnection(url, user, pwd);
			if (con == null) {
				System.out.println("can not find con");
			}
		} catch (Exception e) {
			System.out.println("conn bad!");
			e.printStackTrace();
		}
		return con;
	}

	public static Connection getRomoteConnect() {
		PropUtil pu = new PropUtil("Romote_DB.properties");
		Properties p = pu.getProp();
		Connection con = null;
		try {
			String url = p.getProperty("url");
			String Driver = p.getProperty("driver");
			String user = p.getProperty("userName");
			String pwd = p.getProperty("password");
			Class.forName(Driver);
			con = DriverManager.getConnection(url, user, pwd);
			if (con == null) {
				System.out.println("can not find con");
			}
		} catch (Exception e) {
			System.out.println("conn bad!");
			e.printStackTrace();
		}
		return con;
	}

	public static void rsClose(ResultSet rs) {
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void stmtClose(Statement stmt) {
		try {
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void connClose(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

3.db.properties

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc\:sqlserver\://127.0.0.1\:1433;databaseName\=master
userName=sa
password=xxxx


4.xxxxHandle

package xxxx;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

import xxxx.DBUtil;

/**
 * 
 * 
 * <p>
 * Title: 组信息 /p>
 * 
 * <p>
 * Description: 示例 业务类
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * 
 * @author dml@2013-1-14
 * @version 1.0
 */
public class GroupHandle {
	private static Connection conn = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;
	private static Logger log = Logger.getLogger("