0

I have created this simple test to see if I can connect to my local SQL Server 2014 Express Database:

import java.sql.*;

public class SQLServercheck{
    public static void main (String[] args){
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            Connection con = DriverManager.getConnection
                ("jdbc:sqlserver://mypc\\SQLEXPRESS:mydatabase");
            System.out.println("Connection Successful");
        } catch (SQLException sqle){
            System.out.println("SQL Error: " + sqle);
        } catch (ClassNotFoundException cnfe){
            System.out.println("Class not found exception: " + cnfe);
        }
    } }

When running it, I get the following message:

run:

Class not found exception: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver BUILD SUCCESSFUL (total time: 0 seconds)

I am a student so please answer with some explanation if possible. Much appreciated.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RuanSama
  • 11
  • 3
  • You didn't add the JDBC driver Jar file to the classpath. It's available at https://www.microsoft.com/en-us/download/details.aspx?id=11774 – Andreas May 22 '16 at 22:40

1 Answers1

0

The Driver is found here:

microsoft.com/en-us/download/details.aspx?id=11774

You only need the newest version exe. Don't bother with any of the other files, unless you really want to read more.

Download and install it and then in Netbeans: Once Netbeans is open and you are ready to make the magic:

Right click on your project that you need to access the SQL Server 2014 express database from.

Select properties and click on Libraries in the left pane.

Click on add JAR/Folder to the right of the window and select the .jar file that you installed earlier. Again, try te newest one first. In my case it was sqljdbc42.jar

Problem solved!

Then just remember to configure your SQL Server appropriately so as to avoid a SQLException.

(In my case I had to allow TCP/IP and manually set the port to 1433 in the IPAll group under the IP Adresses tab of the TCP/IP properties window with SQL Server Configuration Manager)

RuanSama
  • 11
  • 3