TOMCAT6.0配置 JNDI DataSource
阿超 发表于 2009-08-23 17:42 | 来源: | 阅读 597 次
其实步骤很简单,下面就简单的说下步骤
一: 把
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="sa" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/employee_manager?autoReconnect=true"/>
粘贴到Tomcat 6.0/conf context.xml 文件中的<Context></Context>
二:把 <resource-ref>
<description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
粘贴到web.xml 文件中的<web-app></ web-app >
三: 把数据库驱动文件放在Tomcat 6.0/lib 中
四: 程序中应用:
public static Connection getConnection() {
DataSource ds;
InitialContext cxt ;
try{
cxt = new InitialContext();
ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/TestDB" );
con = ds.getConnection();
}catch(Exception e){
e.printStackTrace();
}
SqlServer2000和以上步骤一样:但注意
url=”jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=employee_manager”
?autoReconnect=true 应该删除!
上述内容在MyEclipse7.1.1 + MySql5.1/Sqlserver2000+Tomcat6.0.18上测试成功!

