Monday, October 1, 2012

JDBC Connection with MSSQL Server

Hello there, its a while I returned to blog. Here I will enlist the steps needed to perform for JDBC Connection with MSSQL Server 2008.

Steps:
  1. Download the MSSQL Server JDBC Driver from here http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
  2. Unzip the folder and you will get three important files - sqljdbc4.jar, sqljdbc.jar and sqljdbc_auth.dll in the \sqljdbc_4.0\enu\auth\x86 folder
  3. Now put  sqljdbc4.jar file in D:\Program Files\Java\jre7\lib\ext whereas my JDK is installed in D drive
  4. Then put sqljdbc_auth.dll file the D:\Program Files\Java\jre7\bin
  5. Also add those two paths into the PATH system variables
  6. Also get the TCP/IP port number of the SQL Srever instance from SQL Server Management Studio
  7. Now you can check the following code modifying the connection string for you
try

String DbName="stackoverflow_db";
        Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ).newInstance();
                Connection con = DriverManager.getConnection( "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName="+DbName+";integratedSecurity=true");
System.out.println("Connected to database successfully");
}catch(Exception exc){
System.err.println(exc.getMessage());
}


No comments:

Post a Comment