File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC to MS SQL Server

 
Chris Soong
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic