JSP连接SQL数据库,怎么这么难啊? 请各位大哥帮忙啊!
各位大哥,小弟已经发很大力气了,可是还是没有解决JSP连接SQL Server 2000的问题。请各位大哥帮帮忙!
我已经重装了Windows Server 2003的系统了, SQL 数据库也换了标准版的了,打了sp4的补丁。
Tomcat、JDK和jdbc驱动安装路径如下:
D:\j2sdk
D:\Tomcat 5.0
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC
设置虚拟路径为D:\jsp 配置好环境变量后,测试一个JSP页面
http://localhost:8080/jsp/firstjsp.jsp (没有连接数据的JSP页面)
测试成功!
按照资料上的方法
将C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC中的lib目录下三个jar文件:
msbase.jar,mssqlserver.jar,msutil.jar
拷贝到Tomcat 5.0目录下common\lib目录下,
要连接的SQL Server 2000中数据库是ceshi,数据库中的表是userTable
连接数据库的JSP页面如下:
--------------------ceshi.jsp-----------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%!
class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ceshi";
String user="sa";
String password="12345";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from userTable";
ResultSet rs=stmt.executeQuery(sql);
while(rs.nest())
{
%>
第一个字段内容为:<%=rs.getString(1)%>
第er个字段内容为:<%=rs.getString(2)%>
<%
}
%>
<%out.print("数据库连接成功!");%>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
-------------------------------
在地址栏中输入:
http://localhost:8080/jsp/ceshi.jsp
出错页面显示为:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
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: 8 in the jsp file: /testsqlserver.jsp
Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\jsp\org\apache\jsp\testsqlserver_jsp.java:12: 需要 <标识符>
class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
^
An error occurred at line: 8 in the jsp file: /testsqlserver.jsp
Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\jsp\org\apache\jsp\testsqlserver_jsp.java:12: 需要 '{'
class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
^
An error occurred at line: 8 in the jsp file: /testsqlserver.jsp
Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\jsp\org\apache\jsp\testsqlserver_jsp.java:28: 非法的类型开始
while(rs.nest())
^
Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\jsp\org\apache\jsp\testsqlserver_jsp.java:107: 需要 <标识符>
}
^
Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\jsp\org\apache\jsp\testsqlserver_jsp.java:108: 需要 '}'
^
5 错误
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:437)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:497)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:476)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:464)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.30 logs.
--------------------------------------------
Apache Tomcat/5.0.30
请各位大哥再度帮帮小弟看看,非常感谢!万分感谢!
[解决办法]
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from jobs";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
%>
第一个字段内容为sdfsdfsd: <%=rs.getString(1)%>
第er个字段内容为sdfsdf: <%=rs.getString(2)%>
<%
}
out.print("数据库连接成功!");
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
以上代码,测试通过
[解决办法]
真是的.加我QQ274793328给你看看,就不相信搞不定这个东东
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html