{"id":1840,"date":"2022-08-30T15:19:53","date_gmt":"2022-08-30T15:19:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/java-database-connectivity-problem-with-ms-sql-server-2005-from-a-remote-server-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:53","modified_gmt":"2022-08-30T15:19:53","slug":"java-database-connectivity-problem-with-ms-sql-server-2005-from-a-remote-server-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/java-database-connectivity-problem-with-ms-sql-server-2005-from-a-remote-server-collection-of-common-programming-errors\/","title":{"rendered":"Java DataBase Connectivity Problem with MS SQL Server 2005 from a Remote Server-Collection of common programming errors"},"content":{"rendered":"<p>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:<\/p>\n<pre><code>import java.*;\n\npublic class Connect {\n     private java.sql.Connection con = null;\n     private final String url = \"jdbc:sqlserver:\/\/\";\n     private final String serverName=\"xxx.xxx.xxx.xxx\"; \n     private final String portNumber = \"1433\";\n     private final String databaseName=\"myDb\"; \n     private final String userName =\"user1\";\n     private final String password = \"xxxx\";     \n     private final String selectMethod = \"cursor\";\n\n     \/\/ Constructor\n     public Connect() {}\n\n     private String getConnectionUrl() {\n          return url+serverName+\":\"+portNumber+\";databaseName=\"+databaseName+\";selectMethod=\"+selectMethod+\";\";\n     }\n\n     private java.sql.Connection getConnection() {\n          try {\n              Class.forName(\"com.microsoft.sqlserver.jdbc.SQLServerDriver\");\n              con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);\n               if(con!=null) System.out.println(\"Connection Successful!\");\n          } catch(Exception e) {\n               e.printStackTrace();\n               System.out.println(\"Error Trace in getConnection() : \" + e.getMessage());\n         }\n          return con;\n      }\n\n     \/*\n          Display the driver properties, database details\n     *\/\n\n     public void displayDbProperties() {\n                    System.out.println(\"Perform Operations \");\n\n     }\n\n     private void closeConnection() {\n          try{\n               if(con!=null)\n                    con.close();\n               con=null;\n          }catch(Exception e){\n               e.printStackTrace();\n          }\n     }\n     public static void main(String[] args) throws Exception {\n          Connect myDbTest = new Connect();\n         \/\/ myDbTest.displayDbProperties();\n     }\n}\n<\/code><\/pre>\n<p>But I am getting following exceptions:<\/p>\n<pre>\ncom.microsoft.sqlserver.jdbc.SQLServerException: The TCP\/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect\n        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)\nError Trace in getConnection() : The TCP\/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect\nError: No active Connection\n<\/pre>\n<p>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.<\/p>\n<p>Please give me your valuable suggestion which can help me to overcome with this problem.<\/p>\n<ol>\n<li>\n<p>IMHO &#8220;Connection refused&#8221; means your database server is not visible from your application server.<\/p>\n<ul>\n<li>Check IP address and port.<\/li>\n<li>Check database connectivity directly from your database server (to avoid firewalls).<\/li>\n<li>Check database connectivity from your application server.<\/li>\n<\/ul>\n<p>Hope this will help you<\/p>\n<\/li>\n<li>\n<p>Make sure that your SQL Server is configured to use TCP\/IP. Enable it from SQL Server&#8217;s Network Utility app. Also check there that the SQL Server is using port 1433 (IP Addresses &#8211; IPAll &#8211; TCP Port).<\/p>\n<p>Try to use &#8220;telnet 1433&#8221;. If it doesn&#8217;t connect you will not be able to establish a connection.<\/p>\n<\/li>\n<li>\n<p>ALLOW THE CONNECTION FIRST IN WHICH PC YOUR SQL SERVER IS RUNNING&#8230; GO TO CONTROL PANEL&#8211;&gt;ADMIN. TOOLS&#8212;&gt;Windows Firewall with Advanced Security&#8211;&gt;Inbounded rules&#8211;&gt;new rule&#8211;&gt;select port radio button &#8211;&gt;next&#8211;&gt;enter port 3306&#8211;&gt;click next &#8211;&gt;finally give the rule name like conn any&#8230;click finish<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 20:57:23. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 = &#8220;jdbc:sqlserver:\/\/&#8221;; private [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1840","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1840"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1840\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}