<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'bookTable.jsp' starting page</title>
</head>
<body>
<center>
<font size="5">BOOK TABLE</font>
<hr>
<%
/****************JDBC:ODBC桥接法***********/
//String driver="sun.jdbc.odbc.JdbcOdbcDriver";
//String useWay="jdbc:odbc:bookShop";
//Class.forName(driver);
//Connection con = DriverManager.getConnection(useWay, "sa", "123");
//###########################测试通过!

/****************加载sqljdbc驱动法***********/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
   "databaseName=bookShop;user=sa;password=123;";
Connection con = DriverManager.getConnection(connectionUrl);
//###########################未测试通过!

/*****************加载jtds驱动法*************/
//Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
//DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
//String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/bookShop;user=sa;password=123";
//Connection con = DriverManager.getConnection(dbUrl);
//###########################未测试通过!

/****************加载JDBC驱动法*****************/
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bookShop"; 
//String user="sa"; 
//String password="123"; 
//Connection con= DriverManager.getConnection(url,user,password); 
//###########################未测试通过!
Statement stmt = con.createStatement();
try {
ResultSet rs;
rs = stmt.executeQuery("select * from book");
%>
<table border=3 width="300">
<tr bgcolor="silver">
<td>
id
</td>
<td>
name
</td>
<td>
publish
</td>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("publish")%></td>
</tr>
<%
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt.close();
con.close();
%>
</table>
</center>
</body>
</html>我用的数据库是sqlserver2005,开发工具是Eclipse,不足什么原因上面3中连接方式都出错。我在lib中加入了各自的jar包jtds-1.2.jar,msbase.jar,mssqlserver.jar,msutil.jar,sqljdbc.jar;其中jtds-1.2.jar按照其default.html说明配置了环境变量。
数据库名:bookShop,用户名sa,密码123 ;一张表:book有id,name,publish列

解决方案 »

  1.   

    ****************加载sqljdbc驱动法***********/ 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" + 
      "databaseName=bookShop;user=sa;password=123;"; 
    Connection con = DriverManager.getConnection(connectionUrl); 
    //###########################未测试通过! 
    Connection con = DriverManager.getConnection(connectionUrl,"sa","123");
      

  2.   

    ****************加载JDBC驱动法*****************/ 
    //Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    //String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bookShop"; 
    //String user="sa"; 
    //String password="123"; 
    //Connection con= DriverManager.getConnection(url,user,password); 
    //###########################未测试通过! 
    用JDBC时和你的系统有关 要安装SP4或者SP3补丁
      

  3.   

    用try 和catch看导入了驱动没有
      

  4.   

    你用的是SqlServer 2005应该不需要打什么补丁了,你用JTDS驱动连接报什么错误,你把错误贴出来?微软的JDBC驱动有几种,一种是连接SqlServer2000的,还有就是SqlServer2005驱动?不知道你选择是否对?
      

  5.   

    ****************加载sqljdbc驱动法***********/ 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" + 
      "databaseName=bookShop;user=sa;password=123;"; 
    Connection con = DriverManager.getConnection(connectionUrl); 
    //###########################未测试通过! 
    Connection con = DriverManager.getConnection(connectionUrl,"sa","123");
      

  6.   

    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage 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 /bookTable.jsp at line 2522:  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    23:  String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
    24:     "databaseName=bookShop;user=sa;password=123;";
    25:  Connection con = DriverManager.getConnection(connectionUrl);
    26:  //###########################鏈祴璇曢�杩囷紒
    27: 
    28:  /*****************鍔犺浇jtds椹卞姩娉�************/
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    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: com.microsoft.sqlserver.jdbc.SQLServerException: 到主机  的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.bookTable_jsp._jspService(bookTable_jsp.java:152)
    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:393)
    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 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.bookTable_jsp._jspService(bookTable_jsp.java:81)
    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:393)
    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.14 logs.加载sqljdbc驱动时
      

  7.   

    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage 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 /bookTable.jsp at line 2522:  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    23:  String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
    24:     "databaseName=bookShop;user=sa;password=123;";
    25:  Connection con = DriverManager.getConnection(connectionUrl);
    26:  //###########################鏈祴璇曢�杩囷紒
    27: 
    28:  /*****************鍔犺浇jtds椹卞姩娉�************/
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    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: com.microsoft.sqlserver.jdbc.SQLServerException: 到主机  的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.bookTable_jsp._jspService(bookTable_jsp.java:152)
    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:393)
    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 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.bookTable_jsp._jspService(bookTable_jsp.java:81)
    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:393)
    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.14 logs.加载sqljdbc驱动时
      

  8.   

    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage 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 /bookTable.jsp at line 3229:  Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
    30:  DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
    31:  String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/bookShop;user=sa;password=123";
    32:  Connection con = DriverManager.getConnection(dbUrl);
    33:  //###########################鏈祴璇曢�杩囷紒
    34: 
    35:  /****************鍔犺浇JDBC椹卞姩娉�****************/
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    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: Network error IOException: Connection refused: connect
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.bookTable_jsp._jspService(bookTable_jsp.java:152)
    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:393)
    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: Network error IOException: Connection refused: connect
    net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:372)
    net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
    net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:178)
    java.sql.DriverManager.getConnection(Unknown Source)
    java.sql.DriverManager.getConnection(Unknown Source)
    org.apache.jsp.bookTable_jsp._jspService(bookTable_jsp.java:88)
    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:393)
    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.net.ConnectException: Connection refused: connect
    java.net.PlainSocketImpl.socketConnect(Native Method)
    java.net.PlainSocketImpl.doConnect(Unknown Source)
    java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    java.net.PlainSocketImpl.connect(Unknown Source)
    java.net.SocksSocketImpl.connect(Unknown Source)
    java.net.Socket.connect(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:289)
    net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:250)
    net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:297)
    net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
    net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:178)
    java.sql.DriverManager.getConnection(Unknown Source)
    java.sql.DriverManager.getConnection(Unknown Source)
    org.apache.jsp.bookTable_jsp._jspService(bookTable_jsp.java:88)
    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:393)
    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.14 logs.加载jtds驱动时
      

  9.   

    javax.servlet.ServletException: com.microsoft.sqlserver.jdbc.SQLServerException: 到主机  的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect 这个错误的解决方法:配制工具下的SQL Server Configuration Manager中将TCP/IP改为启用
    试试
      

  10.   

    应该是你的TCP/IP 没有启用造成的,
    你先试试,有问题再说,关注ing!
      

  11.   

    在sqlserver2005的企业管理器里,你看一下,你的TCPIP的端口号启用的对不对,然后重新配置一下,但要记得重新启动服务器,我想很可能不是1433端口