Java Development Kit (JDK) 21 is the latest version of the Java SE platform, which was released in September 2022. It includes a number of new features, improvements, and bug fixes. In this article, we will discuss how to use JDK 21 with Microsoft SQL Server, and provide a comprehensive guide for entry-level users.
Prerequisites
Before you start using JDK 21 with SQL Server, you need to make sure that you have the following:
- JDK 21 installed on your machine
- Microsoft SQL Server installed on your machine or accessible from your machine
- Microsoft SQL Server JDBC Driver (JDBC 4.3 or later) installed on your machine
If you don't have any of these prerequisites, you can download and install them from the following links:
Connecting to SQL Server using JDBC
Once you have installed all the prerequisites, you can connect to SQL Server using JDBC by following these steps:
- Create a new Java project in your favorite IDE (e.g., IntelliJ IDEA, Eclipse, etc.)
- Create a new Java class with a main method (e.g.,
SQLServerTest.java) - Add the following code to your main method:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class SQLServerTest { public static void main(String[] args) { String url = "jdbc:sqlserver://localhost:1433;databaseName=myDB"; String user = "myUsername"; String password = "myPassword"; try { Connection conn = DriverManager.getConnection(url, user, password); System.out.println("Connection established."); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }Make sure to replace the
url,user, andpasswordvariables with your own SQL Server connection details. - Add the SQL Server JDBC Driver JAR file to your project's classpath
- Run the Java class
- If everything is set up correctly, you should see the following output:
Connection established.This means that you have successfully connected to SQL Server using JDBC in your Java application.
Using JDBC with SQL Server
Now that you have connected to SQL Server using JDBC, you can start using it to execute SQL queries, update data, and perform other database operations. Here are some examples:
Executing a SQL Query
To execute a SQL query, you can use the
Statementinterface in JDBC. Here's an example:import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SQLServerTest { public static void main(String[] args) { String url = "jdbc:sqlserver://localhost:1433;databaseName=myDB"; String user = "myUsername"; String password = "myPassword"; try { Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM myTable"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }This example executes a SELECT query on the
myTabletable in themyDBdatabase, and prints the ID and name of each row in the table.Updating Data
To update data in SQL Server using JDBC, you can use the
PreparedStatementinterface. Here's an example:import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class SQLServerTest { public static void main(String[] args) { String url = "jdbc:sqlserver://localhost:1433;databaseName=myDB"; String user = "myUsername"; String password = "myPassword"; String sql = "UPDATE myTable SET name = ? WHERE id = ?"; try { Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "New Name"); pstmt.setInt(2, 1); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }This example updates the name of the row with ID 1 in the
myTabletable in themyDBdatabase to "New Name".Creating a Table
To create a table in SQL Server using JDBC, you can use the
Statementinterface. Here's an example:import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class SQLServerTest { public static void main(String[] args) { String url = "jdbc:sqlserver://localhost:1433;databaseName=myDB"; String user = "myUsername"; String password = "myPassword"; String sql = "CREATE TABLE myTable (id INT PRIMARY KEY, name VARCHAR(50))"; try { Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }This example creates a new table called
myTablein themyDBdatabase with two columns:idandname.In this article, we have discussed how to use JDK 21 with SQL Server, and provided a comprehensive guide for entry-level users. We have covered the prerequisites, how to connect to SQL Server using JDBC, and how to use JDBC with SQL Server to execute SQL queries, update data, and perform other database operations. We hope that you have found this article helpful, and that you are now able to use JDK 21 with SQL Server in your Java applications.
References
Title Link JDK 21 download page https://www.oracle.com/java/technologies/javase-jdk-downloads.html SQL Server download page https://www.microsoft.com/en-us/sql-server/sql-server-downloads SQL Server JDBC Driver download page https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver16