tomcat 中使用jdbc数据库连接次
1 将jdbc驱动类拷贝到tomcat的lib目录下2 在tomcat 的server.xml文件中的<GlobalNamingResources>
标签中添加如下内容
<Resource
name="jdbc/test"
type="javax.sql.DataSource"
password=""
driverClassName="com.MySQL.jdbc.Driver"
maxIdle="40"
maxWait="50"
username=""
url=""
maxActive=""
/>
3 在tomcat的context.xml中<Context>标签中添加如下内容
<ResourceLink name="jdbc/test" global="jdbc/test" type="javax.sql.DataSource">
4 创建动态web 项目
5 修改web.xml文件内容 添加如下内容
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
6 编写连接的通用类
String strPoolName="jdbc/test";//定义的连接次名字
InitialContext ctx = new InitialContext()
DataSource ds = (DataSource)ctx.lookup("java:comp/env"+strPoolName);
Connection conn = ds.getConnection();
页:
[1]