A Step-by-Step Guide to Integrating SQL Server with Java on Windows

Rehan ShafiqRehan Shafiq
4 min read

This guide walks you through integrating a Microsoft SQL Server database with a Java application on a Windows system using JDBC (Java Database Connectivity). Assuming you’ve already installed the necessary tools (JDK 8+, SQL Server Express, SSMS, JDBC Driver, and an IDE like IntelliJ IDEA), this tutorial starts with configuring SQL Server and progresses to connecting it with a Java program. Screenshots clarify each step, and troubleshooting tips help resolve common issues. Let’s get started!

Prerequisites

Before proceeding, ensure you have the following installed:

  • Java Development Kit (JDK): JDK 8 or higher. Download JDK.

  • SQL Server: Microsoft SQL Server (Express edition is free). Download SQL Server.

  • SQL Server Management Studio (SSMS): For database management. Download SSMS.

  • JDBC Driver for SQL Server: Official driver from Microsoft. Download JDBC Driver.

  • IDE: IntelliJ IDEA or another Java IDE.

If you’ve already installed these, proceed to the steps below.

Step 1: Set Up SQL Server

  1. Enable TCP/IP in SQL Server Configuration Manager:

    • Press the Windows key and search for SQL Server Configuration Manager.

    • Open it and navigate to SQL Server Network Configuration > Protocols for SQLEXPRESS.

      SQL Server Configuration Manager

    • Check if TCP/IP is disabled. If so, right-click and select Enable.

      Enable TCP/IP

    • Right-click TCP/IP, select Properties, and go to the IP Addresses tab.

    • Scroll to IPAll and ensure the TCP Port is set to 1433 (default port).

      Set TCP Port

    • Click Apply and OK.

    • Restart the SQL Server service: Navigate to SQL Server Services, right-click SQL Server (SQLEXPRESS), and select Restart.

  2. Configure SQL Server Management Studio (SSMS):

    • Open SSMS and connect to your SQL Server instance with these settings:

      • Server Name: localhost\SQLEXPRESS

      • Authentication: Windows Authentication

      • Trust Server Certificate: Check this box

      • Encrypt Connection: Optional

    • Click Connect.

      SSMS Connection

  3. Create a Database and Table:

    • In SSMS, create a database named TestDB and a table named Users using this SQL script:

        CREATE DATABASE TestDB;
        GO
        USE TestDB;
        CREATE TABLE Users (
            Id INT PRIMARY KEY IDENTITY(1,1),
            Name NVARCHAR(50),
            Email NVARCHAR(100)
        );
        INSERT INTO Users (Name, Email) VALUES ('Rehan Shafiq', 'rehan.codecraft@gmail.com');
      

Step 2: Configure the JDBC Driver

  1. Locate the JDBC Driver:

    • If not already downloaded, get the Microsoft JDBC Driver from Microsoft’s JDBC Driver page.

    • Extract the file and locate the .jar file (e.g., mssql-jdbc-12.8.1.jre8.jar for JDK 8).

  2. Add the Driver to Your Project:

    • In IntelliJ IDEA:

      • Go to File > Project Structure > Libraries.

      • Click +, select Java, and choose the .jar file.

      • Click Apply and OK.

Add JDBC Driver

Select JAR File

JDBC Driver Added

Step 3: Create a Java Project and Write Connection Code

  1. Set Up a New Java Project:

    • Open IntelliJ IDEA and create a new Java project.

    • Ensure the JDBC driver is included in the project’s libraries (from Step 2).

  2. Write the Connection Code:

    • Create a new Java class (e.g., SqlServerConnection.java) and add the following code to connect to the SQL Server database and retrieve data from the Users table:

        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.ResultSet;
        import java.sql.Statement;
      
        public class SqlServerConnection {
            public static void main(String[] args) {
                String url = "jdbc:sqlserver://localhost:1433;databaseName=TestDB;encrypt=true;trustServerCertificate=true";
      
                try {
                        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        Connection conn = DriverManager.getConnection(url, user, password);
                        System.out.println("Connected Successfully");
      
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
      
      • Replace TestDB in the url with your actual database name.

      • Replace YourPasswordHere with the sa account password set during SQL Server installation.

Step 4: Test the Connection

  1. Run the Java program in IntelliJ IDEA.

  2. If successful, the console should output:

     Connected Successfully
    

    Screenshot Placeholder: Include a screenshot of the console output showing the successful connection and retrieved data.

  3. If errors occur, refer to the troubleshooting section below.

Troubleshooting Common Issues

  1. Connection Refused:

    • Ensure SQL Server is running and TCP/IP is enabled in SQL Server Configuration Manager.

    • Verify the server name (localhost\SQLEXPRESS) and port (1433) in the connection URL.

    • Check if the Windows Firewall allows connections on port 1433.

  2. Driver Not Found:

    • Verify the JDBC driver .jar file is correctly added to your project’s library or classpath.

For detailed troubleshooting, refer to Microsoft’s JDBC documentation.

Additional Tips

  • Security: Avoid using the sa account in production. Create a dedicated SQL Server user with limited permissions.

  • Connection String Options: Use encrypt=true;trustServerCertificate=true for secure connections. Adjust based on your environment.

Feel free to share your experiences or ask questions in the comments below. Happy coding!


© 2025 Rehan Shafiq
Archive | Privacy Policy | Terms
Powered by Hashnode - Build your developer hub.

0
Subscribe to my newsletter

Read articles from Rehan Shafiq directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rehan Shafiq
Rehan Shafiq