1 Reply Latest reply: Feb 26, 2013 2:41 AM by 964289 RSS

    Java DataBase Connectivity Problem

    970110

      I am writing a java code to connect with MS SQL Server 2005. MS SQL Server is on Remote server windows server 2003. I am trying the following code but i am unable to establish a connection:
      import java.*;

      public class Connect {
      private java.sql.Connection con = null;
      private final String url = "jdbc:sqlserver://";
      private final String serverName="xxx.xxx.xxx.xxx";
      private final String portNumber = "1433";
      private final String databaseName="myDb";
      private final String userName ="user1";
      private final String password = "xxxx";
      private final String selectMethod = "cursor";

      // Constructor
      public Connect() {}

      private String getConnectionUrl() {
      return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
      }

      private java.sql.Connection getConnection() {
      try {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
      if(con!=null) System.out.println("Connection Successful!");
      } catch(Exception e) {
      e.printStackTrace();
      System.out.println("Error Trace in getConnection() : " + e.getMessage());
      }
      return con;
      }

      /*
      Display the driver properties, database details
      */

      public void displayDbProperties() {
      System.out.println("Perform Operations ");

      }

      private void closeConnection() {
      try{
      if(con!=null)
      con.close();
      con=null;
      }catch(Exception e){
      e.printStackTrace();
      }
      }
      public static void main(String[] args) throws Exception {
      Connect myDbTest = new Connect();
      // myDbTest.displayDbProperties();
      }
      }
      But I am getting following exceptions:
      com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
      Error Trace in getConnection() : The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
      Error: No active Connection

        • 1. Re: Java DataBase Connectivity Problem
          964289
          IMHO "Connection refused" means your database server is not visible from your application server.

          Check IP address and port.
          Check database connectivity directly from your database server (to avoid firewalls).
          Check database connectivity from your application server.

          Hope this will help you