Steps to setup a SQL Server Java Connections

1) You need to download the sql server jar library from Microsoft.  You can do this at the following link:

http://go.microsoft.com/fwlink/?LinkId=245496

For help with the jdbc, you can go to http://msdn.microsoft.com/en-us/sqlserver/aa937724
2) Inside the help folder you can open default.htm and read information about using the drivers.  For our purposes we will include the sqljdbc4.jar in our projects.

For quick access to just sqljdbc4.jar, click here and download from the class website

3) To include this in the project do the following:
3a) If you are doing a web project, then you can just add the driver to your apache tomcat lib folder, that is, copy sqljdbc4.jar into C:\Program Files\apache-tomcat-7.0.22\lib,
the location on your computer may be different.

4) Your project should now be able to access the SQLServer files it needs to connect to a SQL database.

5)  To access SQLServer in your program you need to refer to the correct driver and connect to your database.  The example below assumes that SQLServer is installed on your machine,  replace localhost with the actual url if its in a different location. Also you'll need to replace the databaseName, user, and password to match your actual database.

 try{
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  // load the driver
     // line below needs to be modified to include the database name, user, and password (if any)
  con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=database;user=user;password=password;");
 
     System.out.println("Connected to database !");
 
   }
   catch(SQLException sqle) {
      System.out.println("Sql Exception :"+sqle.getMessage());
   }
   catch(ClassNotFoundException e) {
    System.out.println("Class Not Found Exception :" + e.getMessage());
   }

6) Now you can use the con variable as you have in the past to issue SQL operations.


Using Netbeans  Database Services

To connect Netbeans to a SQLServer database dio the following: