J2ee 初步
Tomcat 数据源配置
1. 找到 C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf 下面的 context 文件修改 配置如下
< Context >
< WatchedResource > WEB-INF/web.xml </ WatchedResource >
< Resource name = "jdbc/dggj"
auth = "Container"
type = "javax.sql.DataSource"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/dggj"
username = "root"
password = "123456"
maxActive = "100"
maxIdle = "30"
maxWait = "10000" />
</ Context >
2. 修改 Web 应用的 web.xml 如下
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< resource-ref >
< description > DB Connection </ description >
< res-ref-name > jdbc/dggj </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
</ web-app >
3. 写测试程序 vtest.jsp 如下
<%@ page language = "java" import = "java.util.*,javax.naming.*,javax.sql.*" pageEncoding = "ISO-8859-1" %>
< jsp:directive.page import = "java.sql.Connection" />
< jsp:directive.page import = "java.sql.Statement" />
<%
Context ctx= new InitialContext();
DataSource ds=(DataSource)ctx.lookup( "java:comp/env/jdbc/dggj" );
Connection con=ds.getConnection();
Statement st=con.createStatement();
java.sql.ResultSet rs=st.executeQuery( "select * from tbl_station" );
while (rs.next())
{
System.out.println(rs.getObject(2));
}
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title > My JSP 'judi.jsp' starting page </ title >
</ head >
< body >
This is my JSP page. < br >
</ body >
</ html >
参考地址: http://www.java2000.net/p1906
Tomcat6.0 的配置跟 v5.x 的配置稍微有差别