第一步:在src下面建一个包com.xsl.conf
第二步:在建好的包下面新建一个jdbc.properties
第三步:在jdbc.properties里写入内容如下:
driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@127.0.0.1:1521:orcl或jdbc:oracle:thin:@localhost:1521:orcl
uname = scott
upwd = tiger
第四步:建BaseDao
1 public class BaseDao 2 { 3 private static Properties ps = new Properties(); 4 static{ 5 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("com/xsl/conf/jdbc.properties"); 6 try{ 7 ps.load(is); 8 System.out.println(ps); 9 }catch(IOException e){ 10 e.printStackTrace(); 11 } 12 } 13 public static Connection getConmection(){ 14 Connection conn = null; 15 try{ 16 Class.forName(ps.getProperty("driver")); //"oracle.jdbc.driver.OracleDriver" 17 conn = DriverManager.getConnection(ps.getProperty("url"),ps.getProperty("uname"),ps.getProperty("upwd")); //"jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger" 18 }catch(ClassNotFoundException e){ 19 e.printStackTrace(); 20 }catch(SQLException e){ 21 e.printStackTrace(); 22 } 23 return conn; 24 } 25 public static void closeAll(Connection conn,Statement pstmt,ResultSet rs){ 26 try{ 27 if(rs!=null){ 28 rs.close(); 29 } 30 if(pstmt!=null){ 31 pstmt.close; 32 } 33 if(conn!=null){ 34 conn.close(); 35 } 36 }catch(Exception e){ 37 e.printStackTrace(); 38 } 39 40 } 41 42 43 public static void main(String[] args) 44 { 45 System.out.println(getConnection()); 46 } 47 }
myeclipse连接数据库oracle(添加jdbc.properties)
原文:http://www.cnblogs.com/xsl1995/p/6285258.html