jdbc连接数据库

写了一个简单的jdbc连接:
package Struts2.Util;

import java.io.IOException;

public class BaseDao {
	private Connection conn;
	private PreparedStatement pt;
	private ResultSet rs;

	private String url;
	private String driver;
	private String user;
	private String pwd;

	/**
	 * 
	 * 得到配置文件
	 */
	public void getProperties() {
		Properties pt = new Properties();
		InputStream in = Object.class.getResourceAsStream("/dbconfig.properties");
		try {
			pt.load(in);
			driver = pt.getProperty("jdbc.driverClassName").trim();
			url = pt.getProperty("jdbc.url").trim();
			user = pt.getProperty("jdbc.username").trim();
			pwd = pt.getProperty("jdbc.password").trim();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public BaseDao() {
		super();
	}

	/*
	 * 
	 * 得到数据库连接
	 */
	private Connection getConn() {
		getProperties();
		try {
			// 注册数据库驱动
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	/*
	 *关闭数据库连接 
	 * 
	 */
	public void CloseConn(){
		 if(rs != null) try{ rs.close(); }catch(Exception e){}
	        if(pt != null) try{ pt.close(); }catch(Exception e){}
	        if(conn != null) try{ conn.close(); }catch(Exception e){}		
	}
	/*public static void main(String[] args){
		BaseDao basedao=new BaseDao();
		System.out.println(basedao.getConn());
	}*/
}

其中dbconfig.properties是在src目录下面,其代码为:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.9.206.168:1521:orcl
jdbc.username=omsystem
jdbc.password=tj123456
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
jdbc.initialPoolSize=10
hibernate.show_sql=true
hibernate.cache.use_query_cache=true
hibernate.substitutions=true 1, false 0, yes 'Y', no 'N'
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

你可能感兴趣的:(jdbc)