Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

JDBC to MS SQL Server

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
please buy this thing and then I get a fat cut of the action:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic