0

I want to connect Java class file with SQL server 2008. I have logged in with SQL server authentication use: sa, pass:123456. But i am receiving error in connectivity.

static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dburl = "jdbc:sqlserver://localhost\\SQL2008:1433;Database=Java";
static String user = "sa";
static String password = "123456";


public static void update() throws Exception{
    String sql = "UPDATE Categories SET Id='COM' WHERE Id='LAP'";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(dburl, user, password);
    Statement stmt = conn.createStatement();
    stmt.executeUpdate(sql);
    conn.close();
}



public static void main(String[] args) throws Exception {
    Basic.update();
}


  Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1048)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:829)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:712)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Tutorials.jdbc.Basic.update(Basic.java:48)
at Tutorials.jdbc.Basic.main(Basic.java:72)
Teppei14
  • 83
  • 1
  • 3
  • 8
  • 1
    No, you haven't logged in. It's failing on the `getConnection` line and it's telling you that it can't connect. Make sure you have a route to that port on localhost (i.e. nothing blocking it) and that the server is actually running. – Michael Todd Oct 22 '14 at 16:48
  • Also, is this Sql Server or SQLExpress? Sql Server defaults to port 1433. SQLExpress uses a dynamically assigned port which can be determined from the Configuration Manager applet. – Michael Todd Oct 22 '14 at 16:52

5 Answers5

2

Probably a little late to answer this question, but doing it for my own benefit.

The actual TCP port to connect to through JDBC can be found in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp

Use this value to connect to SQL Server.

For example "jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"

Also ensure that TCP connections are enabled in the server configuration tools.

Yogesh Sajanikar
  • 1,086
  • 7
  • 19
0

Make sure you have Driver jar file and change your dburl to this as mentioned below

try this

  String dburl ="jdbc:sqlserver://SQL2008:1433;DatabaseName=Java;user=sa;Password=123456";
Lokesh
  • 313
  • 2
  • 12
0

I think that your dburl may be malformed. Try this url instead.

String dburl= "jdbc:sqlserver://localhost\\SQL2008:1433;" +
     "databaseName=Java;user=sa;password=123456";
Aaron C
  • 343
  • 2
  • 9
0

TCP/IP Connection Refused.

  1. Run NETSTAT -A to see a list of ports that are open (they'll say LISTENING) If 1433 isn't among them, then SQL server isn't running or isn't listening on that port. Follow Michael Todd's advice from there.

  2. If 1433 is on the list, check your local firewall to see if it allows connections to/from localhost and/or allows connections to 1433.

  3. You can try TELNET 1433 and see if it connects (you should get a blank screen with a cursor blinking at the top left.) (You may have to enable the telnet client in Programs and Features.) If so then you have a problem with your code.

gofr1
  • 15,741
  • 11
  • 42
  • 52
Duston
  • 1,601
  • 5
  • 13
  • 24
0

Thanks you for your help, i'm fix my problem, it's not error on String dburl= "jdbc:sqlserver://localhost\SQL2008:1433;Database:Java"

it's error port 1433 no connect in SQL server 2008 Where i find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, i'm find its properties.

In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port and restart your SQL Server Services > SQL Server

And Its Done...

Thanks all for your help!

Teppei14
  • 83
  • 1
  • 3
  • 8