2

I need help with connecting SQL server and Java. I've added the jdbc driver in eclipse but keep getting this error. Still pretty new to java and SQL server so any help, tips, suggestions would greatly be appreciated. Here is the code I'm using. Not sure if it matters but I'm using SQL Server 2014.

import java.sql.*;

public class DBTest {
    public static void main(String[] args) throws SQLException, ClassNotFoundException{
        String dbUrl = "jdbc:microsoft:sqlserver://MACBOOK\\SQLEXPRESS:1433;DatabaseName=ConnectionTest";
        String user = "testing";
        String pass = "test";

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

        Connection myConn = DriverManager.getConnection(dbUrl, user, pass);         
    }
}
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
CSharpDude
  • 15
  • 4

1 Answers1

0

try changing you dbUrlto:

jdbc:sqlserver://MACBOOK:1433;DatabaseName=ConnectionTest

The connection string should be in the format

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

according to microsoft

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • Thanks guys, that seem to fix 1 problem.. Now I'm getting – CSharpDude Apr 30 '15 at 11:06
  • The TCP/IP connection to the host MACBOOK, port 1433 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.". at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190) – CSharpDude Apr 30 '15 at 11:06
  • first of all make sure that the host of the database is MACBOOK. You could also use the ip address instead of the hostname. Then make sure the instance of your db is running. Then check in the options that it is actually accepting tcp connections. If all these is checked and you still have a problem than check the hosts pc firewall settings and unblock the 1433 port. – MaVRoSCy Apr 30 '15 at 11:16
  • I thought that I did all that. I'll check it over tonight when I get home. Thanks for the tips – CSharpDude Apr 30 '15 at 14:02
  • @Steve if you find an answer helpful you can upvote it and accept it – MaVRoSCy Apr 30 '15 at 22:21