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

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
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
.Check if
TCP/IP
is disabled. If so, right-click and selectEnable
.Right-click
TCP/IP
, selectProperties
, and go to theIP Addresses
tab.Scroll to
IPAll
and ensure theTCP Port
is set to1433
(default port).Click
Apply
andOK
.Restart the SQL Server service: Navigate to
SQL Server Services
, right-clickSQL Server (SQLEXPRESS)
, and selectRestart
.
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
.
Create a Database and Table:
In SSMS, create a database named
TestDB
and a table namedUsers
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
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).
Add the Driver to Your Project:
In IntelliJ IDEA:
Go to
File > Project Structure > Libraries
.Click
+
, selectJava
, and choose the.jar
file.Click
Apply
andOK
.
Step 3: Create a Java Project and Write Connection Code
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).
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 theUsers
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 theurl
with your actual database name.Replace
YourPasswordHere
with thesa
account password set during SQL Server installation.
Step 4: Test the Connection
Run the Java program in IntelliJ IDEA.
If successful, the console should output:
Connected Successfully
Screenshot Placeholder: Include a screenshot of the console output showing the successful connection and retrieved data.
If errors occur, refer to the troubleshooting section below.
Troubleshooting Common Issues
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
.
Driver Not Found:
- Verify the JDBC driver
.jar
file is correctly added to your project’s library or classpath.
- Verify the JDBC driver
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.
Subscribe to my newsletter
Read articles from Rehan Shafiq directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
