In this tutorial we will learn JDBC connection with MsSql database(Microsoft database) using sqljdbc.jar. How to register MsSql driver in java- How to getConnection in java - Writing your jdbc(Java database connectivity) program to connect with MsSql. In previous tutorial we configures java build path in eclipse and connected with Oracle and MySql databases.
Hostname = localHost
Port =1433 (default port)
SID= mydb
Username =ankit
Password=MsSql123
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433/DatabaseName=mydb",
"ankit", "MsSql123");
|
Example/ Full program to connect to MsSql database using Type 4 driver - java JDBC program >
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MsSql_JdbcConnectionSetupExample {
public static void main(String... arg) {
Connection con = null;
try {
// registering MsSql driver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// getting connection
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433/DatabaseName=mydb",
"ankit", "MsSql123");
// Test connection is null or not
if (con != null)
System.out
.println("Connection established successfully with MsSql(Microsoft) database!");
else
System.out.println("No Connection!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (con != null)
con.close(); // close connection
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/*OUTPUT
Connection established successfully with MsSql(Microsoft) database!
*/
|
So, above JDBC java program will connect you to MsSql (Microsoft) database using Type 4 java JDBC driver.
In this tutorial we learned JDBC connection with MsSql database using sqljdbc.jar. And registering MsSql driver in java and getting MsSql connection in java.
RELATED LINKS>