当前位置: 代码迷 >> Java Web开发 >> JSP里连接数据库解决办法
  详细解决方案

JSP里连接数据库解决办法

热度:120   发布时间:2016-04-17 12:17:31.0
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)
  相关解决方案