Java DataBase Connectivity Problem with MS SQL Server 2005 from a Remote Server-Collection of common programming errors
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
I am not getting where is the problem in the above code or do i need to do some setting to connect to remote server.
Please give me your valuable suggestion which can help me to overcome with this problem.
-
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
-
Make sure that your SQL Server is configured to use TCP/IP. Enable it from SQL Server’s Network Utility app. Also check there that the SQL Server is using port 1433 (IP Addresses – IPAll – TCP Port).
Try to use “telnet 1433”. If it doesn’t connect you will not be able to establish a connection.
-
ALLOW THE CONNECTION FIRST IN WHICH PC YOUR SQL SERVER IS RUNNING… GO TO CONTROL PANEL–>ADMIN. TOOLS—>Windows Firewall with Advanced Security–>Inbounded rules–>new rule–>select port radio button –>next–>enter port 3306–>click next –>finally give the rule name like conn any…click finish
Originally posted 2013-12-02 20:57:23.