SQL Server JDBC Connection Guide
The SQL Server JDBC driver can be downloaded and installed from the official Microsoft website.
Import the SQL Server JDBC driver jar file into a Java project.
Use the following code to connect to the SQL Server database:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLServerConnection {
public static void main(String[] args) {
String url = "jdbc:sqlserver://localhost:1433;databaseName=YourDatabaseName";
String user = "YourUsername";
String password = "YourPassword";
try {
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to SQL Server database successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
In the code above, you need to replace YourDatabaseName, YourUsername, and YourPassword with the actual database name, username, and password.
Running the code will result in either “Connected to SQL Server database successfully!” being displayed if the connection is successful or printing the exception information if the connection fails.