プロファイルでデータベースを接続する

1746 ワード

1.        :
conn_url=jdbc:mysql://localhost:3306/buy?useUnicode=true&characterEncoding=UTF-8
conn_user=root
conn_pwd=root
conn_driver=com.mysql.jdbc.Driver
2.          :

  
	private static Properties prop = new Properties();
	static {
		try {
			prop.load(Config.class
					.getResourceAsStream("config.properties"));
			
		} catch (IOException e) {
			System.out.println("File:config.properties no find,PLS check out!");
			e.printStackTrace();
		}
	}	
	public static String CONNECTION_TYPE = prop.getProperty("conn_type");
	public static String CONNECTION_URL = prop.getProperty("conn_url");
	public static String CONNECTION_USER = prop.getProperty("conn_user");
	public static String CONNECTION_PWD = prop.getProperty("conn_pwd");
	public static String CONNECTION_DRIVER = prop.getProperty("conn_driver");
		
}
3.     :

 
	public static Connection getConnection() {
		Connection conn = null;
			try {
				
				Class.forName(Config.CONNECTION_DRIVER).newInstance();
				
				conn = DriverManager.getConnection(Config.CONNECTION_URL,
						Config.CONNECTION_USER, Config.CONNECTION_PWD);
			} catch (Exception e) {
				e.printStackTrace();
			}
		if (conn == null) {
			System.out.println("no get connection!throws Exception");
		}
		return conn;
	}
}