Java Database Connectivity (JDBC) with Microsoft SQL Server

Java Database Connectivity (JDBC) with Microsoft SQL Server
Java Database Connectivity (JDBC) with Microsoft SQL Server

Hello everyone, Welcome to techxon. In this Java tutorial we discuss how to connect Java applications with Microsoft SQL Server Database. First of all need to download Microsoft JDBC Driver Jar file for SQL Server. You can download it from official Microsoft site. After download the Jar file include it in to your project class path.


Download Microsoft JDBC Driver for SQL Server

Now we need to know some important information about Microsoft SQL Server.

  1. Connection URL - URL for connect Microsoft SQL Server
  2. Driver class - com.microsoft.sqlserver.jdbc.SQLServerDriver
  3. Username - Your Microsoft SQL Server username
  4. Password - Your Microsoft SQL Server password
These information are mandatory. Let's discuss this tutorial step by step.

Connection URL

//Connection URL
static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=your_database_name";
  • localhost - Server name on where Microsoft SQL is running. You can also use IP address
  • 1433 - Default port number

Connection Method

I create simple method for connect Microsoft SQL Server.

    //Connection Method
    public Connection conn() throws Exception {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection connection = DriverManager.getConnection(url, "username", "password");
        return connection;
    }

Save, Update and Delete Method

I create method for Save data, Update data and Delete data.

    //Method for Save, Update and Delete data
    public void putData(String sqlQuery) throws Exception {
        Statement st = conn().createStatement();
        st.executeUpdate(sqlQuery);
    }

Search Method

I create method for search data.

    //Method for Search data
    public ResultSet getData(String sqlQuery) throws Exception {
        Statement st = conn().createStatement();
        ResultSet rs = st.executeQuery(sqlQuery);
        return rs;
    }

Complete Java Class

package yourpackagename;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Gayan Ruchiranga
 */
public class JDBC {

    //Connection URL
    static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=your_database_name";

    //Connection Method
    public Connection conn() throws Exception {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection connection = DriverManager.getConnection(url, "username", "password");
        return connection;
    }

    //Method for Save, Update and Delete data
    public void putData(String sqlQuery) throws Exception {
        Statement st = conn().createStatement();
        st.executeUpdate(sqlQuery);
    }

    //Method for Search data
    public ResultSet getData(String sqlQuery) throws Exception {
        Statement st = conn().createStatement();
        ResultSet rs = st.executeQuery(sqlQuery);
        return rs;
    }

}

How to use this Java Class

The following example will show you how to use this JDBC Java class. First create another Java package. Enter its name as "databaseConnection". Then create a JDBC class in that package.

package yourpackagename;

import databaseConnection.JDBC;
import java.sql.ResultSet;

/**
 *
 * @author Gayan Ruchiranga
 */
public class TestJDBC {

    public static void main(String[] args) {
        try {
            
            //Example for use putData() method
            new JDBC().putData("Insert, Update or Delete query");

            //Example for use getData() method
            ResultSet rs = new JDBC().getData("Select query");

            while (rs.next()) {
                rs.getString("Column Name");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Congratulations! Now you can connect Java applications with Microsoft SQL Server Database. Remember this help and leave a comment. Thank You! 

Comments