aspose file tools*
The moose likes JDBC and the fly likes JDBC to MS SQL Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC to MS SQL Server" Watch "JDBC to MS SQL Server" New topic
Author

JDBC to MS SQL Server

Chris Soong
Greenhorn

Joined: Sep 23, 2003
Posts: 6
Can somebody please post the code as to connect JDBC to MS SQL Server? I know how to connect JDBC to MS Access but I need to connect to MS SQL Server instead.
Any help would be greatly appreciated.
V.T. Eialarasu
Greenhorn

Joined: Jan 13, 2004
Posts: 3
Originally posted by Chris Soong:
Can somebody please post the code as to connect JDBC to MS SQL Server? I know how to connect JDBC to MS Access but I need to connect to MS SQL Server instead.
Any help would be greatly appreciated.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MSSqlServerJDBC
{
public static void main(String[] args) throws Exception
{
String db_host = "cgdc1", db_name = "metagrid-bns";
String db_url = "jdbc:microsoft:sqlserver://";
db_url += db_host + ":1433;DatabaseName=";
db_url += db_name + ";SelectMethod=cursor";
System.out.println("db_url = " + db_url);
/*
This is microsoft own type 4 driver.
Another open source driver 'jtds' is
available at JTDS.
*/
String driver_class = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String user_name = "mgbnsuser", password = "tiger";
Class.forName(driver_class);
Connection con =
DriverManager.getConnection(db_url, user_name, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next())
{
System.out.println("user_id = " + rs.getString(1));
}
rs.close();stmt.close(); con.close();
}
}
[ January 13, 2004: Message edited by: V.T. Eialarasu ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC to MS SQL Server
 
Similar Threads
SQL server connectivity
MS sql server + MS type 4 jdbc driver
connection to mssql server database
Connecting to SQL Server 2000
JDBC Drivers with WAS 7