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

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