`
ioio
  • 浏览: 141923 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics