项目中的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>DataSourceTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>testing DataSource in Tomcat with mysql</description>
<display-name>DataSourceTest</display-name>
<servlet-name>DataSourceTest</servlet-name>
<servlet-class>com.gdret.intern.servlet.DataSourceTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataSourceTest</servlet-name>
<url-pattern>/DataSourceTest</url-pattern>
</servlet-mapping>
</web-app>
项目下的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/DataSourceTest" docBase="DataSourceTest" debug="0">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
maxActive="20"
maxIdle="2"
maxWait="3000"
url="jdbc:mysql://localhost:3306/test"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="" />
</Context>
测试用servlet
package com.gdret.intern.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
/**
* Servlet implementation class DataSourceTest
*/
public class DataSourceTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DataSourceTest() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Context ctx;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from user");
ResultSet rs = ps.executeQuery();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
while(rs.next()){
System.out.println(rs.getInt("id")+" "+
rs.getString("username")+ " " +
rs.getString("password")+ " " +
rs.getString("email"));
out.println(rs.getInt("id")+" "+
rs.getString("username")+ " " +
rs.getString("password")+ " " +
rs.getString("email"));
}
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
主要参考:
http://blog.csdn.net/xiazdong/article/details/6898774
http://blog.csdn.net/xiazdong/article/details/6904506
http://blog.csdn.net/cyxlzzs/article/details/7352837
http://blog.sina.com.cn/s/blog_4be5d1c10100wx4q.html
http://blog.sina.com.cn/s/blog_4be5d1c10100wx4v.html
http://annegu.iteye.com/blog/409176