Java 为连接在同一网络上的一组系统创建公共数据库连接
我希望使用此代码的系统连接到单个系统中存在的公共数据库(在本例中为@10.145.10.100)。我希望源文件位于本地系统中,并且仍然能够从系统@10.145.10.100中的单个SQL运行各种应用程序。到目前为止,系统运行在本地系统中的服务器上,因为正如我前面所说的,我希望使用此连接的项目在其自己的机器中拥有其源文件。如果可以通过在一台tomcat服务器上运行它来实现这一点,那么这些解决方案也是受欢迎的。当前显示的错误是ConnectionTimedOut CommonConnection.java 所引用的属性如下所示 dbinfo.properties 上下文文件Java 为连接在同一网络上的一组系统创建公共数据库连接,java,sql,datasource,connection-pooling,Java,Sql,Datasource,Connection Pooling,我希望使用此代码的系统连接到单个系统中存在的公共数据库(在本例中为@10.145.10.100)。我希望源文件位于本地系统中,并且仍然能够从系统@10.145.10.100中的单个SQL运行各种应用程序。到目前为止,系统运行在本地系统中的服务器上,因为正如我前面所说的,我希望使用此连接的项目在其自己的机器中拥有其源文件。如果可以通过在一台tomcat服务器上运行它来实现这一点,那么这些解决方案也是受欢迎的。当前显示的错误是ConnectionTimedOut CommonConnection.j
如果数据库服务器有防火墙,来自其他计算机的连接可能会被拒绝。如果您使用Eclipse,请尝试将您的库添加到WEB-INF/lib文件夹中。我知道您是用斜体字写的,但是您确定mysql连接器java-???-bin.jar位于Tomcat的lib/
文件夹中吗?自从安装Tomcat之后,您是否重新启动了它?您确定正确读取了属性文件吗?你打印出你试图加载的驱动程序的名称了吗?我已经更新了问题Jason。是的,没有添加jar文件,但是现在添加了jar文件,我得到一个异常,说“连接超时了”。这可能是因为访问限制吗?我使用的是MySQL 5.1.36-community,很可能是这个版本,也可能是对服务器的访问权限。无论哪种方式,我都无法判断,因为我没有权限更改sql server属性并对其进行测试。但是谢谢你的回复。
/**
* Builds the URL
*/
private void buildURL(){
ResourceBundle resourceBundle = ResourceBundle.getBundle("com.dennis.connection.dbinfo");
String urlNamespace = resourceBundle.getString("URL_NAMESPACE");
String domain = resourceBundle.getString("DOMAIN");
String port = resourceBundle.getString("PORT");
URL = urlNamespace+domain+":"+port+"/";
}
/**
* A poolable connection object is created using SUN API
* @param dataSourceName the datasource name is passed by the coder who uses this function
*/
public CommonConnection(String dataSourceName) {
ResourceBundle.clearCache();
ResourceBundle dbInfo = ResourceBundle.getBundle(DATABASE_INFO_PATH);
ResourceBundle dbSchema = ResourceBundle
.getBundle(DATABASE_SCHEMA_PATH);
this.dataSourceName = dataSourceName;
this.buildURL();
DRIVER = dbInfo.getString("DRIVER");
URL += dataSourceName;
USERNAME = dbInfo.getString("USERNAME");
PASSWORD = dbInfo.getString("PASSWORD");
try {
ds = setUp();
} catch (Exception e) {
log(e);
}
}
/**
* sets up the datasource
* @return the datasource
* @throws Exception exception that occurs has to be handled instansiation, illegal access, class not found
*/
public DataSource setUp() throws Exception{
Class.forName(CommonConnection.DRIVER).newInstance();
connectionPool = new GenericObjectPool();
connectionPool.setMaxActive(10);
ConnectionFactory cf = new DriverManagerConnectionFactory(
CommonConnection.URL, CommonConnection.USERNAME,
CommonConnection.PASSWORD);
PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf,
connectionPool, null, null, false, true);
return new PoolingDataSource(connectionPool);
}
public GenericObjectPool getConnectionPool() {
return connectionPool;
}
/**
* Sets up the dataSource for the connection
*/
/**
* returns the connection object from the datasource
*
* @return the connection object
*/
public Connection getJNDIConnection() {
Connection result = null;
try {
if (ds != null) {
result = this.ds.getConnection();
} else {
log("Failed to lookup datasource.");
}
} catch (SQLException ex) {
log("Cannot get connection: " + ex);
}
return result;
}
DRIVER =com.mysql.jdbc.Driver
DATASOURCE_CONTEXT =java:comp/env/jdbc/
URL_NAMESPACE =jdbc:mysql://
DOMAIN=10.145.10.100
PORT=3306
USERNAME =root
PASSWORD =admin
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/bank_admin_user"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="admin"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://10.145.10.100:3306/bank_admin_user;create=true" />
</Context>