首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JSP里连接数据库解决办法

2013-09-11 
JSP里连接数据库%@page contentTypetext/html charsetGBK%%@page importjava.sql.* %%Connec

JSP里连接数据库
<%@page contentType="text/html; charset=GBK"%>
<%@page import="java.sql.*" %>
<%
  Connection con = null;
  Statement stmt = null;
  String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  String URL="jdbc:sqlserver://localhost:1433;"+"databaseName=JSP";
  Class.forName(DRIVER);
  con = DriverManager.getConnection(URL,"","");
  stmt = con.createStatement();
  String name = request.getParameter("name");
  String password = request.getParameter("password");
  stmt.executeUpdate("insert into jsptable values('"+name+"','"+password+"')");
%>

我用的是SQLSERVER2005数据库 请问我直连问题出在哪里?报错如下:


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: Exception in JSP: /panduan.jsp:11

8: String name = request.getParameter("name");
9: String password = request.getParameter("password");
10: Class.forName(DRIVER);
11: con = DriverManager.getConnection(URL,"","");
12: stmt = con.createStatement();
13: stmt.executeUpdate("insert into jsptable values('"+name+"','"+password+"')");
14: 


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause 

javax.servlet.ServletException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.panduan_jsp._jspService(panduan_jsp.java:66)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause 

com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.panduan_jsp._jspService(panduan_jsp.java:53)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)


javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


--------------------------------------------

Apache Tomcat/5.5.17


[解决办法]
此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
楼主【zilimenhu】截止到2008-06-29 12:43:44的历史汇总数据(不包括此帖):
发帖数:3 发帖分:60
结贴数:0 结贴分:0
未结数:3 未结分:60
结贴率:0.00 % 结分率:0.00 %
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
[解决办法]
改成
con = DriverManager.getConnection(URL,"sa",""); 
试试
[解决办法]
你的数据库中的用户名和密码是不是空的,是不是sa,如果是写上
[解决办法]
你的如果是sqlServer 2000的话, 建议打上补丁 sp3, 否则经常碰到这问题
[解决办法]

探讨
改成
con = DriverManager.getConnection(URL,"sa","");
试试

[解决办法]
探讨
改成
con = DriverManager.getConnection(URL,"sa","");
试试

[解决办法]
有可能是1433端口的问题哦,打上SP4补丁看看
[解决办法]
LS们说的对
改成sa试试

[解决办法]
打补丁!
[解决办法]
你建一个数据源试一下,然后再用dbc桥接器试一下,看能连上不
[解决办法]
配制工具下的SQL Server Configuration Manager中将TCP/IP改为启用啊
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行