回 帖 发 新 帖 刷新版面

主题:求救阿!jdbc 连接 SQL Server2000 的方法!

小弟的代码如下:
   <%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>

<%
  String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspdev";
  String user = "sa";
  String password = "";
  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  Connection conn = DriverManager.getConnection(url, user, password);
%>
   为什么会出现如下的错误提示呢?
   type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:59)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


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:512)
    java.sql.DriverManager.getConnection(DriverManager.java:171)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:50)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


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

    怎样才可以连接到SQL Server 的数据库呢?

回复列表 (共2个回复)

沙发

看一下这里的文章:http://www.tot.name/blog/index.php?itemid=63

板凳

<%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*"%>
    <html>
    <body>
    <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
    //pubs为你的数据库的
    String user="sa";
    String password="sa";
    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>

我来回复

您尚未登录,请登录后再回复。点此登录或注册