jdbc connector(1)

private static Logger LOG = Logger.getLogger(DBDAO.class.getName());
// 常量用于硬連接
public static final String JDBC_CLASS_NAME = "oracle.jdbc.driver.OracleDriver";

public static final String JDBC_CONN_URL = "jdbc:oracle:thin:@10.142.252.129:1521:mes";
public static final String JDBC_USER_NAME = "wtadmin";
public static final String JDBC_USER_PASSWORD = "wtadmin1";

// public static final String JDBC_CONN_URL = "jdbc:oracle:thin:@10.151.69.156:1521:pdm1";
// public static final String JDBC_USER_PASSWORD = "wtadmin";
// public static final String JDBC_USER_NAME = "wtadmin";

private Connection connection = null;

private Statement statement = null;

private PreparedStatement preparedStatement = null;

private ResultSet resultset = null;

private boolean isAutoCommit = true;

private static boolean count = true;

private String connUrl = "proxool.mesdb";

// 有連接池時=true;無連接池時=false;
private boolean usePool = false;

private String poolType = "PROCOOL";

private String jndiName = "PROCOOL";
/**
* 構造方法﹐直接創建連接
*
* @throws Exception
*/
public DBDAO() throws Exception {
connection = getConn();

}

/**
* 構造方法﹐直接創建連接
*
* @throws Exception
*/
public DBDAO(boolean isAutoCommit) throws Exception {

this();
this.isAutoCommit = isAutoCommit;
connection.setAutoCommit(isAutoCommit);
}

/**
* 構造方法﹐直接創建連接,isAutoCommit = true
*
* @param connUrl
* @param uid
* @param psd
* @throws Exception
*/
public DBDAO(String connUrl, String uid, String psd) throws Exception {
this(connUrl, uid, psd, true);
}

/**
* 構造方法﹐直接創建連接
*
* @param connUrl
* @param uid
* @param psd
* @param isAutoCommit,是否自動提交,true/false
* @throws Exception
*/
public DBDAO(String connUrl, String uid, String psd, boolean isAutoCommit) throws Exception {
connection = getConnection(connUrl, uid, psd);
this.isAutoCommit = isAutoCommit;
connection.setAutoCommit(isAutoCommit);
}

/**
* 返回使用中的conn
*
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception {
return this.connection;
}

/**
* 用直接連接的方式建立連接
*
* @return Connection
*/
public Connection getConnection(String connUrl, String uid, String psd) throws Exception {
try {
Class.forName(JDBC_CLASS_NAME);
connection = DriverManager.getConnection(connUrl, uid, psd);
} catch (ClassNotFoundException e) {
LOG.error(e.toString(), e);
throw e;
} catch (SQLException e) {
LOG.error(e.toString(), e);
throw e;
}
return this.connection;
}

/**
* 取得連接
*
* @return
* @throws Exception
*/
private Connection getConn() throws Exception {
if (usePool) {


if (poolType.equals("WEBLOGIC")) {
try {
InitialContext initialcontext = new InitialContext();
DataSource datasource = (DataSource) initialcontext.lookup(jndiName);
connection = datasource.getConnection();
} catch (NamingException e) {
LOG.error(e.toString(), e);
} catch (SQLException e) {
LOG.error(e.toString(), e);
}
} else if (poolType.equals("TOMCAT")) {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup(jndiName);
connection = ds.getConnection();
} else {
if (count)
connUrl = "proxool.mesdb";
else
connUrl = "proxool.mesdb";
try {
connection = DriverManager.getConnection(connUrl);
} catch (SQLException e) {
LOG.error(e.toString(), e);
throw e;
}
count = !count;
}
} else {
getConnection(JDBC_CONN_URL, JDBC_USER_NAME, JDBC_USER_PASSWORD);
}
return connection;

}

/**
* 設置是否為自動提交﹐不設置時默認為true
*
* @param b
* true或者false
*/
public void setAutoCommit(boolean b) throws SQLException {
try {
isAutoCommit = b;
connection.setAutoCommit(b);
} catch (SQLException e) {
LOG.error(e.toString(), e);
throw e;
}
}

/**
* 返回完整的SQL
*
* @param s
* @param pram
* @return
*/
public static String sqlString(String s, Object[] pram) {
String str = s;
if(pram!=null){
for (int i = 0; i < pram.length; i++) {
try {
str = str.replaceFirst("\\?", "'" + pram[i] + "'");
} catch (PatternSyntaxException e) {
System.err.println(e.toString());
}
}
}else{
LOG.error("DBDAO.sqlString:pram is null");
}
return str;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值