|
楼主 |
发表于 2005-8-3 14:01:42
|
显示全部楼层
书写格式没有问题
后来修改jsp测试文件重新测试时
说can't load driver class,
后来就把server.xml改了
改着改着问题就解决了
修改地方有:
1,server.xml
<Logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="localhost_log." suffix=".txt" timestamp="true" />
<!--因为我换了工作目录,因此 docBase,workDir也改了-->
<Context path="/DataSourceTest" reloadable="true"
docBase="C:\Documents and Settings\ginge\workspace\DataSourceTest"
workDir="C:\Documents and Settings\ginge\workspace\DataSourceTest\work">
<!--将resource 的子元素name,driver class这类全部换成属性,
这是根据tomcat自带的关于JDBC DataSource的Documentation
修改的
-->
<Resource name="jdbc/TestDataSource" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="root" password="16357336"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/student?autoReconnect=true" />
2,修改了测试用的JSP文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page
import="javax.naming.*,java.io.*,javax.sql.*,java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dbtest.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page.
<br>
<h1>test Tomcat</h1>
<%Context initCtx = null;
DataSource ds = null;
Context envCtx = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
} catch (Exception x) {
x.printStackTrace();
if (envCtx == null)
System.out.println("envCtx is null");
}
try {
ds = (DataSource) envCtx.lookup("jdbc/TestDataSource");
} catch (Exception p) {
p.printStackTrace();
if (ds == null) {
PrintWriter pw = response.getWriter();
pw.println("<h2>Can't not create Connection</h2>");
}
}
System.out.println("before getting connection!");
try {
conn = ds.getConnection();
if (conn == null) {
PrintWriter pw = response.getWriter();
pw.println("<h2>Can't not create Connection</h2>");
}
out.println("data from database:<br>");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from courses");
} catch (Exception e) {
e.printStackTrace();
System.out.println("before while loop");
}
%>
<TABLE>
<%try {
while (rs.next()) {
out.print("<tr><td>" + rs.getString(1) + "</td>");
out.print("<td>" + rs.getString(2) + "</td>");
out.print("<td>" + rs.getString(3) + "</td>");
out.print("<td>" + rs.getString(4) + "</td></tr>");
}
} catch (Exception b) {
b.printStackTrace();
System.out.println("in while loop");
}
%>
<%try {
rs.close();
stmt.close();
} catch (Exception c) {
c.printStackTrace();
System.out.println("try to close statement");
rs.close();
stmt.close();
} finally {
}
%>
<
</body>
</html> |
|