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

贴一个Oracle数据源的数据库连接公共类实例

公共类

package DAOHelp;

import java.sql.Connection;
import oracle.jdbc.pool.OracleDataSource;

public class DataBase {
	private static Connection conn = null;
	private static String SERVANDDB = "jdbc:oracle:thin:@192.168.2.7:1521:dogorcl";
	private static String USER = "crazy";
	private static String PWD = "apple";
	private static OracleDataSource ods=null;
	static {
		try {
			ods=new OracleDataSource();
			ods.setURL(SERVANDDB);
			ods.setUser(USER);
			ods.setPassword(PWD);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection() {
		if (conn == null) {
			try {
				conn=ods.getConnection();
				if (conn.isClosed()) {
					conn=ods.getConnection();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return conn;
	}
}
 




使用方式:

import DAOHelp.DataBase;

public class test {

	public static void main(String[] args) {
		Connection conn = DataBase.getConnection();
		/*测试*/
		
		System.out.println("test");
	}
}