1. Download Microsoft JDBC driver
Click here to download Microsoft JDBC Driver 4.0 for SQL Server which supports:- SQL Server versions: 2005, 2008, 2008 R2, and 2012.
- JDK version: 5.0 and 6.0.
2. JDBC database URL for SQL Server
The syntax of database URL for SQL Server is as follows:Where:
- serverName: host name or IP address of the machine on which SQL server is running.
- instanceName: name of the instance to connect to on serverName. The default instance is used if this parameter is not specified.
- portNumber: port number of SQL server, default is 1433. If this parameter is missing, the default port is used.
- property=value: specify one or more additional connection properties. To see the properties specific to SQL server, visit Setting the Connection Properties.
- Windows authentication: using current Windows user account to log on SQL Server. This mode is for the case both the client and the SQL server are running on the same machine. We specify this mode by adding the property integratedSecurity=true to the URL.
- SQL Server authentication: using a SQL Server account to authenticate. We have to specify username and password explicitly for this mode.
- Connect to default instance of SQL server running on the same machine as the JDBC client, using Windows authentication:
jdbc:sqlserver://localhost;integratedSecurity=true;
- Connect to an instance named sqlexpress on the host dbServer, using SQL Server authentication:
jdbc:sqlserver://dbHost\sqlexpress;user=sa;password=secret
- Connect to a named database testdb on localhost using Windows authentication:
jdbc:sqlserver://localhost:1433;databaseName=testdb;integratedSecurity=true;
3. Register JDBC driver for SQL Server and establish connection
The JDBC driver class of SQL Server is com.microsoft.sqlserver.jdbc.SQLServerDriver, so to register this driver, use the following statement:
1
| DriverManager.registerDriver( new com.microsoft.sqlserver.jdbc.SQLServerDriver()); |
1
| Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ); |
1
2
3
4
5
| String dbURL = "jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret" ; Connection conn = DriverManager.getConnection(dbURL); if (conn != null ) { System.out.println( "Connected" ); } |
1
2
3
4
| String dbURL = "jdbc:sqlserver://localhost\\sqlexpress" ; String user = "sa" ; String pass = "secret" ; conn = DriverManager.getConnection(dbURL, user, pass); |
1
2
3
4
5
| String dbURL = "jdbc:sqlserver://localhost\\sqlexpress" ; Properties properties = new Properties(); properties.put( "user" , "sa" ); properties.put( "password" , "secret" ); conn = DriverManager.getConnection(dbURL, properties); |
4. Java Code Example to connect to SQL Server
To demonstrate, we create a small program that connects to an SQL Server instance on localhost and print out some database information as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| package net.codejava.jdbc; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; /** * This program demonstrates how to establish database connection to Microsoft * SQL Server. * @author www.codejava.net * */ public class JdbcSQLServerConnection { public static void main(String[] args) { Connection conn = null ; try { String dbURL = "jdbc:sqlserver://localhost\\sqlexpress" ; String user = "sa" ; String pass = "secret" ; conn = DriverManager.getConnection(dbURL, user, pass); if (conn != null ) { DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData(); System.out.println( "Driver name: " + dm.getDriverName()); System.out.println( "Driver version: " + dm.getDriverVersion()); System.out.println( "Product name: " + dm.getDatabaseProductName()); System.out.println( "Product version: " + dm.getDatabaseProductVersion()); } } catch (SQLException ex) { ex.printStackTrace(); } finally { try { if (conn != null && !conn.isClosed()) { conn.close(); } } catch (SQLException ex) { ex.printStackTrace(); } } } } |
1
2
3
4
| Driver name: Microsoft JDBC Driver 4.0 for SQL Server Driver version: 4.0.2206.100 Product name: Microsoft SQL Server Product version: 11.00.2100 |
No comments:
Post a Comment