初学JSP,遇到连接sql数据问题,请各位大哥帮帮忙,小弟先谢谢了!
小弟是学JSP的新手,现在遇到一个JSP连接SQL Server 2000的问题。请各位大哥帮帮忙!
Windows的操作系统,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目录下,
之后,修改环境变量中的CLASSPATH,
把SQL Server 2000 For JDBC 驱动程序安装目录下的
C:\Program Files\SQLDriverForJDBC\lib\msbase.jar;
C:\Program Files\SQLDriverForJDBC\lib\mssqlserver.jar;
C:\Program Files\SQLDriverForJDBC\lib\msutil.jar;
追加到CLASSPATH中去
要连接的SQL Server 2000中数据库是testDatabase,数据库中的表是userTable
连接数据库的JSP页面如下:
---------------------selectuserTable.jsp---------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% Connection con;
Statement sql;
ResultSet rs;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDatabase","sa","12345");
sql=con.createStatement();
rs=sql.executeQuery("select * from userTable");
out.print("<Table Border>");
out.print("<TR><td colspan=8 align=center>用户数据</td></tr>");
out.print("<TR>");
out.print("<Td width=100 >"+"用户ID号");
out.print("<Td width=50 >"+"用户名");
out.print("<Td width=100>"+"用户真实姓名");
out.print("<Td width=50>"+"年龄");
out.print("<Td width=50>"+"性别");
out.print("<Td width=100>"+"联系地址");
out.print("<Td width=100>"+"联系电话");
out.print("<Td width=100>"+"添加时间");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
out.print("<TD >"+rs.getLong(1)+"</TD>");
out.print("<TD >"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getString(4)+"</TD>");
out.print("<TD >"+rs.getInt("user_age")+"</TD>");
out.print("<TD >"+rs.getString("user_sex")+"</TD>");
out.print("<TD >"+rs.getString("user_address")+"</TD>");
out.print("<TD >"+rs.getString("user_telephone")+"</TD>");
out.print("<TD >"+rs.getString("add_time")+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
%>
</BODY>
</HTML>
-----------------------------
在地址栏中输入:
http://localhost:8080/jsp/selectuserTable.jsp
出错页面显示为:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.selectuserTable_jsp._jspService(selectuserTable_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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)
root cause
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(DriverManager.java:525)
java.sql.DriverManager.getConnection(DriverManager.java:171)
org.apache.jsp.selectuserTable_jsp._jspService(selectuserTable_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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.
请各位大哥帮帮小弟,先谢谢了!
[解决办法]
如果你的JSP 代码写的没有问题的话,我怀疑上你的SQLSever 有问题 ,你到网上下载一个 SQLServer 的sp3 和 sp4 升级包,并安装,再看下出现了此类异常没!!!
因为网上出现这种异常,用这种方法解决满好的!
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html