1. 将jdk,tomcat的环境配置好,不要忘记tomcat的环境变量配置
2. 使用myeclipse开发,配置好服务器
3. 新建一个web工程,首先将mysql-connector-java-5.0.8-bin.jar拷贝到Web-inf/lib/,并将这个jar包拷贝到Tomcat_Home/lib/,否则提示找不到驱动
4. 写xml文件
l 在工程下META-INF/新建一个context .xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/fck"
username="root"
password="root"
type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/fcktest?characterEncoding=UTF-8" />
</Context>
l Web.xml
<resource-ref>
<res-ref-name>jdbc/fck</res-ref-name>
res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5. index.jsp进行测试
<%@ page language="java" import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*" pageEncoding="UTF-8"%>
<%
Context ctx = new InitialContext();
String strLookup = "java:comp/env/jdbc/fck";
DataSource ds =(DataSource) ctx.lookup(strLookup);
Connection con = ds.getConnection();
if (con != null){
out.print("success");
}else{
out.print("failure");
}
%>