-1

I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.

The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.

When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:

try{
           String 
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
       Connection conn = DriverManager.getConnection(URL,"","");

   System.out.println("connected");
    }catch(Exception e){
        System.out.println("Oops\n"+e);
    }

What do I need to change to fix this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Adam Boyher
  • 36
  • 1
  • 4
  • You should see [this](http://stackoverflow.com/questions/18981279/the-tcp-ip-connection-to-the-host-localhost-port-1433-has-failed), also [this](http://stackoverflow.com/questions/18841744/jdbc-connection-failed-error-tcp-ip-connection-to-host-failed) – TuyenNTA May 09 '17 at 22:49
  • Is this database on the local machine? Do you have a node on your network **named** `sqlexpress`? – Elliott Frisch May 09 '17 at 22:49

1 Answers1

0

You might need to reconfigure your connection string into this format. jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE

HOST in this case is most likely to be "localhost" since you are connecting on a local machine. DATABASE will be the name of your database

Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver

J. Knight
  • 131
  • 9