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://www.microsoft.com/downloads/en/details.aspx?FamilyID=a737000d-68d0-4531-b65d-da0f2a735707&displaylang=en
For help with the jdbc, you can go to http://msdn.microsoft.com/en-us/library/ff928324(v=SQL.10).aspx
- download 1033\sqljdbc_3.0.1301.101_enu.exe by clicking on
the dowload button
- the file will be 3.7 Mbytes in size.
- Run the exe file, it will ask for a location to unzip the files
into, use the browser button to select a folder, then press the top
"Unzip" button
- This will unzip the file into the selected folder. with the
following folder structure:
- sqljdbc_3.0
- enu
- auth
- help
- xa
- install.txt
- license.txt
- release.txt
- sqljdbc4.jar
- sqljdbc.jar
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.
3) To include this in the project do the following:
- first create your project
- then copy sqljdbc4.jar into your project folder using "My
Computer"
- In Netbeans, from the Projects window, right click on your
project and open its properties.
- In the categories window select "Libraries"
- Click on the "Compile" tab
- Select Add JAR/Folder
- Browse to the location of sqljdbc4.jar and select it
- Finally press the ok button.
4) Your project should now be able to access the SQLServer files it
needs to connect to a SQL database.
5) You need to change your database access code to use
SQLServer instead of Access, to do this you will be using a different
driver and and different connection URL. 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.