Tomcat5.5.17+MySQL构建数据源

来源:互联网 发布:c 程序员简历模板 编辑:程序博客网 时间:2024/06/01 19:33
C:/Tomcat 5.5/conf/server.xml中添加
<Context path="/DataTool" reloadable="true" docBase="E:/workspace/DataTool" workDir="E:/workspace/DataTool/work" >

<Resource name="jdbc/mysql/test"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=gb2312"
username="root"
password=""
maxActive="20"
maxIdle="10"
maxWait="-1"
/>
</Context>

</Host>

E:/workspace/DataTool/WEB-INF/web.xml
<?mxl version="1.0" ecnoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<description>datepool</description>

<resource-ref>
<description>datesource</description>

<res-ref-name>jdbc/mysql/test</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

E:/workspace/DataTool/show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>


<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>

<html>
<head>

<title>My JSP 'testPool.jsp' starting page</title>

</head>

<body>
my JSP page. <br>
<%
try {
Connection conn=null;

Context initCtx=new InitialContext();

Context ctx = new InitialContext();

javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup("java:comp/env/jdbc/mysql/test");

conn=ds.getConnection();

Statement stmt=conn.createStatement();

PreparedStatement ps=conn.prepareStatement("select * from student");

ResultSet rs=ps.executeQuery();

while(rs.next()){

out.println(rs.getString(3)+"<br>");

}

rs.close();
stmt.close();
out.println("ok");

}

catch(Exception e) {

System.out.println(e.toString());
}

%>
</body>
</html>