JDBC connection with MsSql (Microsoft) database

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

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
                 // getting connection
                 con = DriverManager.getConnection(
                                                               "ankit", "MsSql123");
                 // Test connection is null or not
                 if (con != null)
                          .println("Connection established successfully with MsSql(Microsoft) database!");
                       System.out.println("No Connection!");
          } catch (ClassNotFoundException e) {
          } catch (SQLException e) {
          } finally {
                 try {
                       if (con != null)
                              con.close(); // close connection
                 } catch (SQLException e) {
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.


JDBC connection with PostgreSQL database

JDBC tutorial connection with Oracle 11g

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
//start paginationJs //end paginationJs