Results 1 to 3 of 3
  1. #1
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default Connect to SQL Server

    Hi,

    I have the following code to connect to SQL server and below it, I have given the error I am getting when trying to run it. Is it the port number or does it look like an id/password problem? Please help.
    Java Code:
    package testconnection;
    import java.sql.*;
    
    public class Main {
    
        public static void main(String[] args) {
            DB db = new DB();
            db.dbConnect("jdbc:jtds:sqlserver://localhost:1433/DB_us","us","");
    
        }
    }
    
    class DB
    {
        public DB() {}
    
        public void dbConnect(String db_connect_string, String db_userid, String db_password)
        {
            try
            {
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
                System.out.println("connected");
    
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    java.sql.SQLException: Network error IOException: Connection refused: connect
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(C onnectionJDBC2.java:372)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(C onnectionJDBC3.java:50)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.ja va:178)
    at java.sql.DriverManager.getConnection(DriverManager .java:582)
    at java.sql.DriverManager.getConnection(DriverManager .java:185)
    at testconnection.DB.dbConnect(Main.java:27)
    at testconnection.Main.main(Main.java:13)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    at java.net.Socket.connect(Socket.java:519)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at net.sourceforge.jtds.jdbc.SharedSocket.createSocke tForJDBC3(SharedSocket.java:289)
    at net.sourceforge.jtds.jdbc.SharedSocket.<init>(Shar edSocket.java:250)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(C onnectionJDBC2.java:297)
    ... 6 more
    BUILD SUCCESSFUL (total time: 1 second)
    Last edited by ddatta8; 03-04-2010 at 01:22 AM. Reason: none

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    i believe connection revused means it could not physically connect to the database service socket on that machine. For example, if the database is running, and has been configured to allow TCP/IP connections from external hosts at the IP address you are coming from, perhaps the box itself is running a windows firewall that is preventing any traffic external to the machine where the database software is running from connecting to it.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Connection refused means that the server saw the connection request and didn't like it. Check your JDBC URL...

Similar Threads

  1. Connect to SQL server 2005
    By comp in forum JDBC
    Replies: 1
    Last Post: 03-25-2009, 11:25 PM
  2. multi clients connect to udp server
    By bachma7 in forum Networking
    Replies: 5
    Last Post: 12-12-2008, 08:56 PM
  3. Problem connect to SQL Server
    By Crisanto in forum Eclipse
    Replies: 1
    Last Post: 12-02-2008, 12:13 AM
  4. Server not able to connect client
    By Kapil Gupta in forum New To Java
    Replies: 15
    Last Post: 07-22-2008, 04:09 AM
  5. connect to web server from mobile using java app.
    By jankrishnan in forum Advanced Java
    Replies: 1
    Last Post: 07-15-2008, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •