JSP连接SQL的问题,虽然是老生常谈,但是以前的相关解决方法都试了,还是没成功,不知如何是好!
程序:test.jsp
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<%@ page import= "com.microsoft.jdbc.sqlserver.SQLServerDriver " %>
<html>
<body>
<%Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:microsoft:sqlserver://localhost:1305;DatabaseName=test ";
//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 test ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为: <%=rs.getString(1)%>
您的第二个字段内容为: <%=rs.getString(2)%>
<%}%>
<%out.print( "数据库操作成功,恭喜你 ");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
浏览后的出错提示:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 11
8: //pubs为你的数据库的
9: String user= "sa ";
10: String password= " ";
11: Connection conn= DriverManager.getConnection(url,user,password);
12: Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
13: String sql= "select * from test ";
14: ResultSet rs=stmt.executeQuery(sql);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:408)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:855)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:784)
org.apache.jsp.test_jsp._jspService(test_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
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.test_jsp._jspService(test_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
JDK目录:C:\Java\jdk1.5
Tomcat目录:D:\Tomcat 6.0
环境变量设置:
CLASSPATH:
.;%CLASSPATH%;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\jre\javaws\i18n.jar;%JAVA_HOME%\jre\javaws\jaws.jar;%JAVA_HOME%\jre\lib\rt.jar;%JAVA_HOME%\jre\lib\sunsasign.jar
JAVA_HOME:
C:\Java\jdk1.5
path:
.;%JAVA_HOME%\jre\bin;%JAVA_HOME%\bin;
我已把C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib目录下的三个jar文件拷到了D:\Tomcat 6.0\lib目录下,真不知是哪里出了问题,向各位请教了!
[解决办法]
又不是包找不到....关键是这个
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
tomcat 6以上的好象要用JDK1.6版本的.把tomcat换为5.X.X的吧
[解决办法]
不好意思,Tomcat 6.0 可以用 JRE 5.0的.
http://apache.mirrors.tds.net/tomcat/tomcat-6/v6.0.10/README.html
1.数据库url对吗
2.数据库打开了吗?
3.帐号密码对吗?
4。是SQLServer 2000吧?
[解决办法]
Error establishing socket错误
首先你先检查你数据库是否使用 你的1305端口(默认是1433的) 防火墙是否屏蔽1305端口
如果都没有问题 进CMD 使用NETSTAT -AN检查1305端口是否开启 没有的话需要打MS SQL SERVER的SP3 OR SP4 补丁。。。。。。。。。。
我的异常网推荐解决方案:An exception occurred processing JSP page,http://www.myexception.cn/j2se/33144.html
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html