1

Ok so I have this code:

package com.andrewxd.banksystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Interface 
    {
        public static void main(String[] args)
        {
            try
            {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
                Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem");
                System.out.println("test");
                Statement sta = conn.createStatement();
                String Sql = "select * from Clients";
                ResultSet rs = sta.executeQuery(Sql);

                System.out.println(rs.next());
            } catch (Exception e){
                e.printStackTrace();
            }
    }


}

but it gives me this error can somebody help me?:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

I've tried searching but I didn't really understand much.

from what i understand the port is incorrect but how do i find the correct ip/port?

7
  • from what i understand the port is incorrect but how do i find the correct ip/port? Commented Feb 17, 2014 at 15:48
  • If you have multiple network interfaces try those IP addresses too. Commented Feb 17, 2014 at 15:50
  • This should be in your question, otherwise it can be missed. Commented Feb 17, 2014 at 15:50
  • i don't have multiple network interfaces Commented Feb 17, 2014 at 15:51
  • Use SSMS to see the listening port at the DB properties Commented Feb 17, 2014 at 15:51

5 Answers 5

1

1) Open SQL Server configuration manager and check to see if the TCP/IP under Network configuration protocols is enabled.

2) Under properties of the SQL serve under Connections check to see if Allow remote connections to this server is allowed.

3) Check to see if you can connect via SSMS and query the database.

4) In SQL Server Configuration manager check to see if the SQL Server Browser service is running. (this is not enabled by default nor is it set up to start up automatically by default).

5) If all those are set up then I would check the firewall.

(For anyone that might come across this the solution was to allow SQL Server and windows authentication)

Sign up to request clarification or add additional context in comments.

7 Comments

tcp/ip status enabled
Do you have any firewalls on? Also can you connect via SSMS using that username/password?
i can't connect to SSMS with Andrew pass andrei23... it gives me error 18456
Do you have an account to connect to the administrator SQL server to check on the status of the user or can you connect using windows authentication?
i just needed to unlock access for windows authentication and sql authentication
|
0

Make sure your sql server configuration -has TCP configured, and configured at that IP address.

You can test a couple things:

  1. turn off Windows Firewall, from services.msc.
  2. try connecting to SQL Server through the IP address,username,pwd the application is trying to use. (open enterprise manager, connect, enter IP address etc)

Comments

0

Try using jtds driver for SQLServer.

Comments

0

Installing Oracle JDBC driver from Here

And with your code looking like this:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleJDBC {

    public static void main(String[] argv) {

        System.out.println("-------- Oracle JDBC Connection Testing ------");

        try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:BankSystem", "Andrew","andrei23");

            } 
        catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

}

It should probably work, if not, what does the output look like now ? Still equal to what it was before ? Did you check firewalls ? Connectivity to server allowed ?

11 Comments

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver and i installed ojdbc6.jar to buildpaths
could it be that your jdbc driver is not in the classpath of your project? did you check preferences of jdbc, like correct path ?
oh so i need sqljdbc4.jar and ojdbc6.jar now i get it
but still -------- Oracle JDBC Connection Testing ------ Oracle JDBC Driver Registered! Connection Failed! Check output console IO Error: The Network Adapter could not establish the connection
which operating system and IDE do you use ?
|
0

The standard port for sqlserver is 1433 and not 1443 as far as I recall it right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.