Fri, 17 May 2024


How to connect to a SQL Server database using JDBC?

By: Peter, NetArt Media
Fri, 11 June 2021

This example connects to a SQLServer database using the NetDirect JDBC driver.
Connection connection = null;
try
{
String driverName = "com.jnetdirect.jsql.JSQLDriver"; // NetDirect JDBC driver
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase; // a JDBC url
String username = "username";
String password = "password";

// Load the JDBC driver
Class.forName(driverName);

// Create a connection to the database
connection = DriverManager.getConnection(url, username, password);
}
catch (ClassNotFoundException e)
{
// Could not find the database driver
}
catch (SQLException e)
{
// Could not connect to the database
}


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox