日期:2014-05-16 浏览次数:20740 次
如果你使用的是myeclipse的话,很简单,只需要三步
第一:在你安装TomCat的目录下找到context.xml配置文件,我的路径是【D:\Program Files\ApacheSoftware Foundation\Tomcat 6.0\conf\context.xml】
修改文件,添加如下
<Resource
name="jdbc/mldn"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="690811"
driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/mldn"
/> <resource-ref>
<description>DBConnection</description>
<res-ref-name>jdbc/mldn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref><Context path="/mars"docBase="E:\Workspace_myeclipse\DemoMLDN"reloadable="true">
<Resource
name="jdbc/mldn"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="690811"
driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/mldn"
/>
</Context><%@ page import="java.sql.Connection"%> <%@ page import="javax.sql.DataSource"%> <%@ page import="javax.naming.InitialContext"%> <%@ page import="javax.naming.Context"%> <%@ page contentType="text/html" pageEncoding="GBK"%> <html> <head> <title>练习</title> </head> <body> <% String DSNAME = "java:comp/env/jdbc/mldn";//jdbc/mldn是自己配置的,前面是java中默认的 Context context = new InitialContext(); DataSource ds = (DataSource) context.lookup(DSNAME); Connection conn = ds.getConnection(); %> <%=conn%> <% conn.close(); %> </body> </html>
运行结果是:
jdbc:mysql://localhost:3306/mldn, UserName=root@localhost, MySQL-AB JDBC Driver
对于Oracle也可以这么做。网上有很多例子,可以找找看。