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

java connect连接问题
以下代码中当执行conn   =   DriverManager.getConnection(connurl,   user,   password);   语句时总是不能正确执行。具体表现为在网页中显示 "No   success   connect   database ",不知何缘故?如何解决。

import   javax.servlet.*;
import   java.io.*;
import   javax.servlet.http.*;
import   java.sql.*;

public   class   SqlConn   extends   HttpServlet
{
private   String   connurl;
private   String   user;
private   String   password;
private   String   sql;

public   void   init()   throws   ServletException
{
connurl   =   "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs ";  
user   =   "sa ";  
password   =   "1234 ";

try
{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
}
        catch(ClassNotFoundException   ce)
        {
                        throw   new   UnavailableException( "加载数据库驱动失败! ");
        }

}
public   void   doGet(HttpServletRequest   req,HttpServletResponse   resp)
  throws   ServletException,IOException
{
Connection   conn=null;  
Statement   stmt=null;  
ResultSet   rs=null;  

        PrintWriter   out=resp.getWriter();
        sql   =   "select   *   from   testTable ";
        try   {  
conn   =   DriverManager.getConnection(connurl,   user,   password);   //获得连接  
stmt   =   conn.createStatement();   //获得执行SQL语言的容器    
rs   =   stmt.executeQuery(sql);   //获得结果集  
while   (rs.next())   {  
out.println(rs.getString(1));}
}
catch(SQLException   e)
        {
            out.println( "No   success   connect   database ");
           
        }
       
}
}

------解决方案--------------------
sql server需要打sp4补丁
------解决方案--------------------
最大可能是sqlserver没打补丁,然后是驱动包匹配问题
------解决方案--------------------
打补丁~