首先连接没有问题
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%> <%
DataSource ds = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//从Context中lookup数据源。
ds = (DataSource)envCtx.lookup("jdbc/pcd");
if(ds!=null)
{
out.println("已经获得DataSource!");
out.println("<br>");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select top 10 w3 from g_wlmc");
out.println("以下是从数据库中读取出来的数据");
while(rst.next())
{
out.println("bookName:"+rst.getString("w3"));
out.println("<br>");
}
}
else
out.println("连接失败!");
}
catch(Exception ne)
{
out.println(ne);
}
%>
上面运行没有问题而下面的用法不行
类文件
package admin;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource; public class DatabaseConnection {
public static Connection getConnection() throws SQLException,NamingException
{
try
{
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/pcd");
return ds.getConnection();
}
catch(SQLException e)
{
throw e;
}
catch(NamingException e)
{
throw e;
}
}
}
调用文件
<%@page contentType="text/html; charset=gb2312" %>
<%@page import="java.sql.*"%>
<%@page import="admin.DatabaseConnection;"%>
<html>
<head>
<title>login</title>
</head>
<body bgcolor="#FFFFFF">
<% Connection db= DatabaseConnection.getConnection();
Statement stmt=db.createStatement();
ResultSet rst=stmt.executeQuery("select top 10 w3 from g_wlmc");
while (rst.next()) {
out.println(rst.getString("w3"));
}
db.close();
%>
</label>
</form>
</body>
</html>
返回错误
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 9 in the jsp file: /gzmk/aa.jsp
DatabaseConnection cannot be resolved
6: <title>login</title>
7: </head>
8: <body bgcolor="#FFFFFF">
9: <% Connection db= DatabaseConnection.getConnection();
10: Statement stmt=db.createStatement();
11: ResultSet rst=stmt.executeQuery("select top 10 w3 from g_wlmc");
12: while (rst.next()) {
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%> <%
DataSource ds = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//从Context中lookup数据源。
ds = (DataSource)envCtx.lookup("jdbc/pcd");
if(ds!=null)
{
out.println("已经获得DataSource!");
out.println("<br>");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery("select top 10 w3 from g_wlmc");
out.println("以下是从数据库中读取出来的数据");
while(rst.next())
{
out.println("bookName:"+rst.getString("w3"));
out.println("<br>");
}
}
else
out.println("连接失败!");
}
catch(Exception ne)
{
out.println(ne);
}
%>
上面运行没有问题而下面的用法不行
类文件
package admin;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource; public class DatabaseConnection {
public static Connection getConnection() throws SQLException,NamingException
{
try
{
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/pcd");
return ds.getConnection();
}
catch(SQLException e)
{
throw e;
}
catch(NamingException e)
{
throw e;
}
}
}
调用文件
<%@page contentType="text/html; charset=gb2312" %>
<%@page import="java.sql.*"%>
<%@page import="admin.DatabaseConnection;"%>
<html>
<head>
<title>login</title>
</head>
<body bgcolor="#FFFFFF">
<% Connection db= DatabaseConnection.getConnection();
Statement stmt=db.createStatement();
ResultSet rst=stmt.executeQuery("select top 10 w3 from g_wlmc");
while (rst.next()) {
out.println(rst.getString("w3"));
}
db.close();
%>
</label>
</form>
</body>
</html>
返回错误
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 9 in the jsp file: /gzmk/aa.jsp
DatabaseConnection cannot be resolved
6: <title>login</title>
7: </head>
8: <body bgcolor="#FFFFFF">
9: <% Connection db= DatabaseConnection.getConnection();
10: Statement stmt=db.createStatement();
11: ResultSet rst=stmt.executeQuery("select top 10 w3 from g_wlmc");
12: while (rst.next()) {
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
是不是这个没有倒入啊,我也不清楚
==================
无语
deploy到tomcat上而不是要把workspace放在下面