Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting MS SQL Server with Java

Status
Not open for further replies.

hbutt

Programmer
Apr 15, 2003
35
GB
hi, there ive been having problems in connecting to MS SQL Server through java, could someone help me, as im struggling to do so. thanks in advance.
kind regards
hbutt.
 
Hi,

Download the Driver for MS SQLServer and put the jar file in your class path before tesing the below code

Links to download Drivers for SQL Server

NetDirect: DataDirect: FreeTDS:
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
}

Cheers,
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top