81,094
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc//ROOT" type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000" validationQuery="select
1" username="数据库用户名" password="密码"
url="jdbc:microsoft:sqlserver://211.211.211.00;DatabaseName=数据库名" maxActive="4"/>
</Context>
<Resource name="jdbc//ROOT" type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000" validationQuery="select
1" username="数据库用户名" password="密码"
url="jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=数据库名" maxActive="4"/>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/ROOT</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
public static Connection getConnection() {
Connection con = null;
try {
Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup(
"java:comp/env/jdbc/ROOT");
if (ds != null) {
System.out.println("获取成功DataSource"+ds);
con = ds.getConnection();
System.out.println(con+"=con");
}
return con;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
<Context>
......
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="123456" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Book;SelectMethod=Cursor"/>
</Context>
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/TestDB</value>
</property>
</bean>