Re: JDBC und SQL Server

From: Tim (triggerNOSPAM_at_get2net.dk)
Date: 08/15/04


Date: Sun, 15 Aug 2004 22:12:41 +0200

Hi Patrick,

Here's some sample code:

Connection connection = null;
try {
    String driverName = "jdbc:microsoft:sqlserver://";
    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String myDatabaseUrl = serverName + ":" + portNumber;
    String myDatabaseName = "DatabaseName=Northwind";
    String url = "jdbc:microsoft:sqlserver://" + myDatabaseUrl + ";" +
myDatabaseName;
    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);
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT CustomerId, CompanyName FROM
Customers");
    while (rs.next()) {
        System.out.println(rs.getString("CustomerId"));
        System.out.println(rs.getString("CompanyName"));
    }
    rs.close();
} catch (ClassNotFoundException e) {
    // Could not find the database driver
    e.printStackTrace();
} catch (SQLException e) {
    // Could not connect to the database
    e.printStackTrace();
}
finally {
    try {
        connection.close();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}

A great place to find all kinds of Java sample code is here:
http://javaalmanac.com/cgi-bin/search/find.pl?words=sql

- Tim

"Patrick Hennig" <patrick_hennig@licht-concept.de> wrote in message
news:%23mXDLUsgEHA.3944@tk2msftngp13.phx.gbl...
> Hi,
>
> Iīm very new in this topic.
>
> And now I want to read some data from a SQL Server with Java.
> But I donīt know how.
>
> I have now downloaded the new Driver form Microsoft and now I want to read
> data from the SQL Server with Java.
>
> Can someone give me a little example.
>
> Sincerly Patrick Hennig
>
>



Relevant Pages

  • one problem with SimplePlayerApplet.java (JMF sample code)
    ... Java Media Framework (JMF) 2.1 - Sample Code. ... This applet runs well in IE or Firefox. ...
    (comp.lang.java.programmer)
  • Re: Question about sample Java application
    ... > I'm working with some sample code out from a vendor's SDK, ... > kind of new to working with Java applications, and I want to try to ... > instance of the RuntimeExample class, then in line, they are calling ... mainand construction are two separate beasts. ...
    (comp.lang.java.programmer)
  • New to programming and mac in general
    ... great GUI and the safety-net of comercial apps not present in linux). ... seem to be able to create a Swing/SWT interface for Java Desktop ... real aplications for the mac enviroment, ... i have peeked a bit of sample code generated by XCode and ...
    (comp.sys.mac.programmer.help)
  • Re: Using POI to create XLS from a C app
    ... I'm trying to just run some sample code (the ... CreateCells.java sample from the poi source distro), ... $ java CreateCells ...
    (comp.lang.java.programmer)
  • Re: File browser
    ... the actual Java API documentation, and that's good enough for me. ... But as far as initialization goes, there's ample ambiguity here, in the ... documentation, in the tutorials, and in sample code I've seen all over the ...
    (comp.lang.java.programmer)

Loading