Install and configure the JDBC Driver
Download Microsoft JDBC Driver for SQL Server from here (I used V2.0 in XP and V3.0 in win7)
Unpack the driver to c:\Program Files\ Microsoft SQL Server JDBC Driver 2.0 (or 3.0 depending on your version)
Add the driver path to Windows PATH this procedure is the same in XP and in Win7, do this “rightclick” on “My Computer” and select “Properties” this opens the “System Properties” window and select the “Advanced” tab
Click on “Environment Variables” (bottom of the window)
Select the “PATH” variable and add this to the end of the line “;c:\Program Files\ Microsoft SQL Server JDBC Driver 2.0\sqljdbc_2.0\enu\sqljdbc.jar” without the quotation marks but with the semicolon at the beguinnig, once again if you use V3.0 be sure to replace it accordingly.
Configure MS SQL Server 2008
Have an instance of MSSQL Server 2008 up and running (if you don’t know how read this), next, make sure your SQL Server as TCP-IP turned on, to do this go to the “SQL Server Configuration Manager”
Expand “SQL Server Network Configuration” and click on “Protocols for MSSQLSERVER” then on the right panel rightclick on TCP-IP and then “Enable”, this will allow for eclipse to access your SQL server through the MS SQL Driver.
After this you need to restart your SQL Server service like so:
Now it’s time to configure the user login that will be used to access the database, i created a test user for this (if you don’t know how to create a user go here), to configure a login expand your server instance, then “Security” and then “Logins”, locate your user login rightclick it and select “Properties”
On the “Login Properties” window (on the upper left corner) select “User Mapping” then choose which Databases your user will need to have access to (mine was SI_TP1) then select the “Database role membership” and be sure to select “db_datareader” and “db_datawriter”.
Use Eclipse to access your Database
Create a project and a class file that will be used to access your DB (if you don’t know how go here), next rightclick on your project in the “Package Explorer” go to “Build Path” and chose “Add Libraries”
In the “Add Library” window select “User Library” and click next
Next select “User Libraries…”
Click on “New…”
Give your Library a name like “MS_JDBC_2”
Then click on “ADD JARs..”
Navigate to your “c:\Program Files\ Microsoft SQL Server JDBC Driver 2.0\sqljdbc_2.0\enu\” folder and chose sqljdbc.jar file if your using XP or sqljdbc4.jar if your using Win7.
Click OK and return to Eclipse.
To access your database from eclipse use the following code:
SERVER_NAME = your server name
DB_NAME = the database you want do access
LOGIN = the login of your user
*** = that user password
SOME_TABLE = name of table in database
SOME_METHOD = ResultSet methods
import java.sql.*;
public class JDBCTest
{
public static void main(String[] args)
{
try
{
//Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionURL = "jdbc:sqlserver://SERVER_NAME;" +
"databaseName=DB_NAME;user=LOGIN;password=***;";
Connection con = DriverManager.getConnection(connectionURL);
//ENTER SQL STATMENTS HERE
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SOME_TABLE");
while(rs.next())
{
System.out.print(rs.SOME_METHOD);
}
con.close();
}
catch(SQLException se)
{ System.out.println(se.getMessage()+"**"+se.getSQLState()+"**"+se.getErrorCode());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
If there is anything you found unclear or if you found an error please leave a comment.