日期:2014-05-18  浏览次数:20681 次

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 错误