📄 jdbcconnector.java
字号:
/*
* JDBCConnector.java
*
* Created on 2001/11/21, 19:14
*/
package jp.co.intra_mart.framework.base.data;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Iterator;
import java.util.ResourceBundle;
import jp.co.intra_mart.framework.system.log.LogManager;
import jp.co.intra_mart.framework.system.log.LogConstant;
import java.sql.SQLException;
import java.util.MissingResourceException;
/**
* JDBC傪棙梡偡傞僨乕僞僐僱僋僞偱偡丅
*
* @author INTRAMART
* @version 1.0
* @deprecated JDBC傪捈愙埖傢偢丄DataSource傪巊梡偡傞傛偆偵偟偰偔偩偝偄丅
* @see DataSourceConnector
*/
public class JDBCConnector extends DBConnector {
/** 僨乕僞儀乕僗僪儔僀僶偺僉乕 */
public static final String KEY_DRIVER = "driver";
/** 僨乕僞儀乕僗URL偺僉乕 */
public static final String KEY_URL = "url";
/** 僨乕僞儀乕僗愙懕儐乕僓柤偺僉乕 */
public static final String KEY_USERNAME = "username";
/** 僨乕僞儀乕僗愙懕儐乕僓僷僗儚乕僪偺僉乕 */
public static final String KEY_PASSWORD = "password";
/**
* 怴婯偺JDBCConnector傪惗惉偟傑偡丅
*/
public JDBCConnector() {
super();
}
/**
* 僐儈僢僩偟傑偡丅
*
* @throws DataConnectException 僐儈僢僩偵幐攕
*/
public void commit() throws DataConnectException {
Iterator connections;
Connection connection;
DataConnectException exception = null;
if (this.resources != null) {
connections = this.resources.values().iterator();
while (connections.hasNext()) {
connection = (Connection)connections.next();
try {
connection.commit();
} catch (Exception e) {
LogManager.getLogManager().getLogAgent().sendMessage(
JDBCConnector.class.getName(),
LogConstant.LEVEL_ERROR,
DataManager.LOG_HEAD + e.getMessage(),
e);
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n")
.getString(
"Common.FailedToCommit");
} catch (MissingResourceException ex) {
}
exception = new DataConnectException(message, e);
}
}
}
if (exception != null) {
throw exception;
}
}
/**
* 儘乕儖僶僢僋偟傑偡丅
*
* @throws DataConnectException 儘乕儖僶僢僋偵幐攕
*/
public void rollback() throws DataConnectException {
Iterator connections;
Connection connection;
DataConnectException exception = null;
if (this.resources != null) {
connections = this.resources.values().iterator();
while (connections.hasNext()) {
connection = (Connection)connections.next();
try {
connection.rollback();
} catch (Exception e) {
LogManager.getLogManager().getLogAgent().sendMessage(
JDBCConnector.class.getName(),
LogConstant.LEVEL_ERROR,
DataManager.LOG_HEAD + e.getMessage(),
e);
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n")
.getString(
"Common.FailedToRollBack");
} catch (MissingResourceException ex) {
}
exception = new DataConnectException(message, e);
}
}
}
if (exception != null) {
throw exception;
}
}
/**
* 僨乕僞僗僩傾偺帒尮傪夝曻偟傑偡丅
*
* @throws DataConnectException 帒尮奐曻偵幐攕
*/
public void release() throws DataConnectException {
Iterator connections;
Connection connection;
DataConnectException exception = null;
if (this.resources != null) {
connections = this.resources.values().iterator();
while (connections.hasNext()) {
connection = (Connection)connections.next();
try {
connection.close();
} catch (Exception e) {
LogManager.getLogManager().getLogAgent().sendMessage(
JDBCConnector.class.getName(),
LogConstant.LEVEL_ERROR,
DataManager.LOG_HEAD + e.getMessage(),
e);
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n")
.getString(
"Common.FailedToReleaseResource");
} catch (MissingResourceException ex) {
}
exception = new DataConnectException(message, e);
}
}
}
if (exception != null) {
throw exception;
}
}
/**
* 愙懕愭偺儕僜乕僗傪捛壛偟傑偡丅
*
* @param resource 儕僜乕僗
* @param params 儕僜乕僗偺僷儔儊乕僞
* @throws DataConnectException 愙懕偵幐攕
* @return 儕僜乕僗
*/
protected Connection putResource(String resource, ResourceParam[] params)
throws DataConnectException {
String key;
String value;
String driver = null;
String url = null;
String username = null;
String password = null;
Connection connection = null;
// 僷儔儊乕僞偺庢摼
if (params != null) {
for (int i = 0; i < params.length; i++) {
key = params[i].getName();
value = params[i].getValue();
if (key.equals(KEY_DRIVER)) {
// 僪儔僀僶偺応崌
driver = value;
} else if (key.equals(KEY_URL)) {
// URL偺応崌
url = value;
} else if (key.equals(KEY_USERNAME)) {
// 儐乕僓柤偺応崌
username = value;
} else if (key.equals(KEY_PASSWORD)) {
// 僷僗儚乕僪偺応崌
password = value;
}
}
}
// 僐僱僋僔儑儞偺捛壛
try {
Class.forName(driver);
} catch (Exception e) {
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n").getString(
"JDBCConnector.FailedToLoadJDBCDriver");
} catch (MissingResourceException ex) {
}
throw new DataConnectException(
message + " : driver class = " + driver,
e);
}
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n").getString(
"Common.FailedToGetConnection");
} catch (MissingResourceException ex) {
}
throw new DataConnectException(message + " : url = " + url, e);
}
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n").getString(
"JDBCConnector.FailedToSetAutoCommit");
} catch (MissingResourceException ex) {
}
throw new DataConnectException(message + " : url = " + url, e);
}
try {
this.resources.put(resource, connection);
} catch (Exception e) {
String message = null;
try {
message =
ResourceBundle.getBundle(
"jp.co.intra_mart.framework.base.data.i18n").getString(
"Common.FailedToAddConnection");
} catch (MissingResourceException ex) {
}
throw new DataConnectException(
message + " : url = " + url + ", resource = " + resource,
e);
}
return connection;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -