java连接oracle数据库 返回一个单例连接

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;


public class ConnectionCreator {

	private static final ConnectionCreator singleton = new ConnectionCreator();

	private final PropertyResourceBundle bundle;

	private ConnectionCreator() {
		try {
			bundle = (PropertyResourceBundle) ResourceBundle.getBundle("jdbc");
			Class.forName(bundle.getString("jdbc.driverClassName"));

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static ConnectionCreator getInstance() {
		return singleton;
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(
				bundle.getString("jdbc.url"),
				bundle.getString("jdbc.username"),
				bundle.getString("jdbc.password")
		);
	}
}

 配合 jdbc.properties 文件

# database configuration
# HSQLDB
#jdbc.driverClassName=org.hsqldb.jdbcDriver
#jdbc.url=jdbc:hsqldb:hsql://localhost/testDB
#jdbc.username=sa
#jdbc.password=
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.0.77:1521:AHUZL
jdbc.username=SCOTT
jdbc.password=TIGER

猜你喜欢

转载自ahuzl007.iteye.com/blog/1631789