try{
         String url="jdbc:microsoft:sqlserver://tjj:12345;DataBaseName=myDataBase"; 
         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
         Connection con=DriverManager.getConnection(url,"Administrator","aaaa");
         Statement stmt=con.createStatement();
         ResultSet result=stmt.executeQuery("select * from CUSTOMER");
         
         int i=10;
         while(result.next()){
            g.drawString(result.getString(1),40,60+i);
            g.drawString(result.getString(3),100,60+i);
            g.drawString(result.getString(6),220,60+i);
            i+=20;
          }
       }catch(SQLException ex){
          System.out.println("SQLException:" +ex.getMessage());
       }catch(java.lang.Exception ex1){
          System.out.println("LangException:" +ex1.getMessage());
       }
提示:
[microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket. 
1。是不是url里面的格式有问题?我的SQL服务器名称是TJJ.2。设置ODBC数据源跟这个有什么关系?
3。是不是用windows认证不能正常访问数据库而一定要用混合认证?
4。我用telnet <host> 12345提示连接失败,跟这个有关系吗?怎么样才能连接成功??

解决方案 »

  1.   

    默认的端口不是12345,你改过吗?
    url里面应该带有user password,安装的microsoft jdbc driver目录下有文档参考一下
      

  2.   

    用jdbc不用配置odbc数据源吧,我记得oracle不用配置odbc,
    端口号一定要准确,不是随便填的,
    还有你确定你的sqlserver的jdbc驱动包没有问题么
      

  3.   

    路过,顺便问一下,数据库默认是不是JDBC安装的目录??我也碰到了数据库连接的问题.
    哪位前辈可以详细地讲讲有关数据库连接的问题?
    真的很郁闷...
      

  4.   

    to Acylas(Acylas):
    我改过的,原来默认的是1433,连接不起来,所以才改的,
    方法getConnection(String url,String user,String password)格式不会错的,我查了联机文档to yangbc(yangbc)
    端口不能随便填,那应该怎么填呀??to tangjiehua(只想好好学习。) 
    数据库默认是不是JDBC安装的目录??---这句话的意思我不懂呀,能不能解释一下
      

  5.   

    版主们,能不能抽空来看一下还有jFresH_MaN大哥,帮忙看看吧!顶。顶。。顶
      

  6.   

    [microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.这是最让人头疼的异常啊几点你确定一下:
    你使用的端口是12345,默认的应该是1433,你到企业管理器里面去确认一下
    sqlserver 是windows和sql复合身份验证模式的
    sqlserver 打补丁到sp3我一直认为ms的jdbc驱动不太好,至少是对jdbc的支持不太好,经常会有莫名其妙的问题,比如说这个
      

  7.   

    我已经把企业管理器里面的端口改成12345了sqlserver 是windows和sql复合身份验证模式的,按照这种说法,那我现在的认证方式就不能连接到数据库了,那岂不是要重装SQL server??还是打补丁就可以解决??
      

  8.   

    还有一个细节:我安装的SQL server是中文版,JDBC driver是English版的,有没有影响??
      

  9.   

    en还是中文版没有关系你如果按照我上面说的都做了还是连不上,我建议你就不要用sqlserver了
    真的是很郁闷的。
      

  10.   

    sqlserver的默认端口是1433,劝你改回来用,连不上是不是开了防火墙了?
      

  11.   

    主要问题出在String url="jdbc:microsoft:sqlserver://tjj:12345;DataBaseName=myDataBase"; 
    首先:如果你使用本机的话,你可以把tjj改成localhost或者127.0.0.1
    还有端口12345,是不是正确!至于会出现Error establishing socket.那就说明你要给你的sql打上sp3补丁!
      

  12.   

    安装MS SQL Server 2000的sp3补丁包( http://www.microsoft.com/downloads/details.aspx?FamilyId=90DCD52C-0488-4E46-AFBF-ACACE5369FA3&displaylang=zh-cn#filelist )。注意,不升级的话是无法正确连接到MS SQL Server 2000上的,会出现Error establishing socket错误。
      

  13.   

    我也出个这个问题,不过没解决。
    我在jdk\lib下,tomcat\share\lib下,在项目中都放了mssqlserver.jar文件。都提示这个错误。我以前连过oracle,没象这么麻烦。
      

  14.   

    折腾了几天,还是没有什么效果,SP3已经上了,现在出了这么一个问题:[microsoft][SQLServer 2000 Driver for JDBC]Error opening/loading com.microsoft.util.transliteration.properties.
    这又是什么意思呀?
      

  15.   

    我现在也是这个问题,郁闷啊!是不是跟jdk版本有关系啊?我的是jdk1.4.2的
      

  16.   

    getConnect里面有url,user,password, 
    不要以为有user和password了,url里面就不再需要.事实是url里面一样还要带user,password属性的,前面已经给你提过了.你看看安装目录下的帮助吧.下面我贴其中一点给你:表明了那些是必须要的(带optional就可要可不要的属性)
    Table 3-1. SQL Server Connection String Properties  Property Description 
    DatabaseName
    OPTIONAL The name of the SQL Server database to which you want to connect. 
    上面这个不写的话就是指向对应数据库服务器上master数据库HostProcess
    OPTIONAL The process ID of the application connecting to SQL Server 2000. The supplied value appears in the "hostprocess" column of the sysprocesses table. NetAddress
    OPTIONAL The MAC address of the network interface card of the application connecting to SQL Server 2000. The supplied value appears in the "net_address" column of the sysprocesses table. Password The case-insensitive password used to connect to your SQL Server database. PortNumber
    OPTIONAL The TCP port (use for DataSource connections only). The default is 1433. ProgramName
    OPTIONAL The name of the application connecting to SQL Server 2000. The supplied value appears in the "program_name" column of the sysprocesses table. SelectMethod SelectMethod={cursor | direct}. Determines whether database cursors are used for Select statements. Performance and behavior of the driver are affected by the SelectMethod setting.
    Direct-The direct method sends the complete result set in one request to the driver. It is useful for queries that only produce a small amount of data that you fetch completely. You should avoid using direct when executing queries that produce a large amount of data, as the result set is cached completely on the client and constrains memory. In this mode, each statement requires its own connection to the database. This is accomplished by "cloning" connections. Cloned connections use the same connection properties as the original connection; however, because transactions must occur on a single connection, auto commit mode is required. Due to this, JTA is not supported in direct mode. In addition, some operations, such as updating an insensitive result set, are not supported in direct mode because the driver must create a second statement internally. Exceptions generated due to the creation of cloned statements usually return an error message similar to "Cannot start a cloned connection while in manual transaction mode."
    Cursor-When the SelectMethod is set to cursor, a server-side cursor is generated. The rows are fetched from the server in blocks. The JDBC Statement method setFetchSize can be used to control the number of rows that are fetched per request. The cursor method is useful for queries that produce a large amount of data, data that is too large to cache on the client. Performance tests show that the value of setFetchSize has a serious impact on performance when SelectMethod is set to cursor. There is no simple rule for determining the value that you should use. You should experiment with different setFetchSize values to find out which value gives the best performance for your application. 
    The default is direct. SendStringParameters
    AsUnicode SendStringParametersAsUnicode={true | false}. Determines whether string parameters are sent to the SQL Server database in Unicode or in the default character encoding of the database. True means that string parameters are sent to SQL Server in Unicode. False means that they are sent in the default encoding, which can improve performance because the server does not need to convert Unicode characters to the default encoding. You should, however, use default encoding only if the parameter string data that you specify is consistent with the default encoding of the database.
    The default is true. ServerName The IP address (use for DataSource connections only). User The case-insensitive user name used to connect to your SQL Server database. 
      

  17.   

    MS SQLSERVER有3个jar。另外2个也要放在相应位置:msutil.jar,msbase.jar