Connection to Microsoft SQL Server 2005 with JDBC
This has taken me a while to figure out, from reading various blogs, so heres a quick post to hopefully make the set up easy.
And your done!
I found the following links really helpful:
Connect with Integrated Authentication:
- Download and Install MS SQL Server 2005 express
- During install check the box to enable integrated authentication
- Run 'SQL Server Surface Area Configuration'
- Click 'Surface Area Configuration for Services and Connections'
- In the left hand pane click 'Remote Connections'
- Click 'Local and Remote Connections' Radio Button and then 'Using TCP/IP only'
- Right click 'My Computer'->Manage
- Click 'Services and Applications'->'Services'
- Right Click on 'SQL Server Browser'->Properties
- In the Startup Type drop down select 'Automatic' then click the 'Apply' then click 'Start'
- Install and run 'Microsoft SQL Server Manager Studio Express'
- Note the server name for example "MY-PC\SQLEXPRESS"
- Download the JDBC driver from Microsoft
- Unzip the driver
- In the unzip folder find the file 'sqljdbc_auth.dll' for your computer architecture and JVM, in the folder 'sqljdbc_
\enu\auth\' - Place the file 'sqljdbc_auth.dll' on your windows system path (this is required for integrated authentication)
- Open your favourite java code editor with the jdbc jar in the class path write the following code:
import java.sql.*;
public class test{
public static void main(String[] args) {
try{
java.lang.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection c = java.sql.DriverManager.getConnection("jdbc:sqlserver://MY-PC\\SQLEXPRESS;IntegratedSecurity=True");
System.out.println("Connected!");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Connect with SQL Server Authentication:
- Download and Install MS SQL Server 2005 express
- During install check the box 'SQL Server and Windows Authentication Mode'
- Run 'SQL Server Surface Area Configuration'
- Click 'Surface Area Configuration for Services and Connections'
- In the left hand pane click 'Remote Connections'
- Click 'Local and Remote Connections' Radio Button and then 'Using TCP/IP only'
- Right click 'My Computer'->Manage
- Click 'Services and Applications'->'Services'
- Right Click on 'SQL Server Browser'->Properties
- In the Startup Type drop down select 'Automatic' then click the 'Apply' then click 'Start'
- Install and run 'Microsoft SQL Server Manager Studio Express'
- Note the server name for example "MY-PC\SQLEXPRESS"
- In the object Explorer Open Security->Logins folder
- Right Click Logins folder-> New Login
- Enter a Login name, check 'SQL server Authentication', enter a password
- Select 'User Mapping' page, check a tick box next to the database name the user is to be connected to
- In the database role membership check: db_datawriter, db_datareader, public
- Click OK
- Download the JDBC driver from Microsoft
- Open your favourite java code editor with the jdbc jar in the class path write the following code:
;user=MyUserName;password=MyPassword;
import java.sql.*;
public class test{
public static void main(String[] args) {
try{
java.lang.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection c = java.sql.DriverManager.getConnection("jdbc:sqlserver://MY-PC\\SQLEXPRESS");
System.out.println("Connected!");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
And your done!
I found the following links really helpful:
- Angel Saenz-Badillos on MSDN
- http://jena.sourceforge.net/DB/mssql-howto.html
- MSDN building a connection URL
Labels: mssql 2005


0 Comments:
Post a Comment
<< Home