/** * 在本地获得数据库连接 */ package com.j2ee.db; import java.util.*; import java.sql.*; import javax.sql.*; import java.io.*; import oracle.jdbc.driver.*; import javax.naming.*; /** * 通过OCI方式获得Oracle数据库连接 */ public class DbConnection { final static String sDBDriver = "oracle.jdbc.driver.OracleDriver"; final static String sConnStr = "jdbc:oracle:oci8:sr/sr@ora199"; /** * */ public DbConnection() { } /** * 获得Oracle数据库连接 */ public java.sql.Connection connectDbByOci() { java.sql.Connection conn=null; try { Class.forName(sDBDriver); conn = DriverManager.getConnection(sConnStr); } catch (Exception e) { System.out.println("ERROR:"+e.getMessage()); } return conn; } } |
/** * 在本地获得数据库连接 */ package com.j2ee.db; import java.util.*; import java.sql.*; import javax.sql.*; import java.io.*; import oracle.jdbc.driver.*; import javax.naming.*; /** * 通过thin方式获得Oracle数据库连接 */ public class DbConnection { private String sConnStr = ""; /** * 缺省构造器 */ public DbConnection() { sConnStr = "jdbc:oracle:thin:@10.1.4.199:1521:ora199"; } /** * @param ip,serviceName */ public DbConnection(String ip,String serviceName) { sConnStr = "jdbc:oracle:thin:@"+ip+":1521:"+serviceName; } /** * 通过thin方式获得Oracle数据库的连接. */ public java.sql.Connection connectDbByThin() { java.sql.Connection conn=null; try { Class.forName(sDBDriver); conn = DriverManager.getConnection(sConnStr,"sr","sr"); } catch (Exception e) { System.out.println("ERROR:"+e.getMessage()); } return conn; } /** * 通过thin方式获得Oracle数据库的连接. * @param userId,password */ public java.sql.Connection connectByJdbc(String userId,String password) { java.sql.Connection conn=null; try { Class.forName(sDBDriver); conn = DriverManager.getConnection(sConnStr,userId,password); } catch (Exception e) { System.out.println("ERROR:"+e.getMessage()); } return conn; } } |
/** * 在本地获得数据库连接 */ package com.j2ee.db; import java.util.*; import java.sql.*; import javax.sql.*; import java.io.*; import oracle.jdbc.driver.*; import javax.naming.*; /** * 通过JdbcOdbc桥方式获得Oracle数据库连接 */ public class DbConnection { /** * */ public DbConnection() { } /** * 获得Oracle数据库连接 */ public java.sql.Connection connectDbByJdbcOdbcBridge() { java.sql.Connection conn=null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:ora199","sr","sr"); } catch (Exception e) { System.out.println("ERROR:"+e.getMessage()); } return conn; } } |
/** * 从连接池中获得数据库连接 */ package com.j2ee.db; import java.util.*; import java.sql.*; import javax.sql.*; import java.io.*; import oracle.jdbc.driver.*; import javax.naming.*; /** * 通过连接池方式获得Oracle数据库连接 */ public class DbConnection { /** * */ public DbConnection() { } /** * 获得Oracle数据库连接 */ public java.sql.Connection connectDbByConnectionPool() { java.sql.Connection conn=null; try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/credit2"); conn=ds.getConnection(); } catch (Exception e) { System.out.println("ERROR:"+e.getMessage()); } return conn; } } |