Java
  Home arrow Java arrow Page 2 - Exploring JDBC and XML
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Exploring JDBC and XML
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 4
    2006-06-15

    Table of Contents:
  • Exploring JDBC and XML
  • JDBC Drivers
  • Using XML
  • Processing XML with Java and XOM

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Exploring JDBC and XML - JDBC Drivers


    (Page 2 of 4 )

    Creating a Java program that uses a JDBC driver is similar to creating one that uses the JDBC-ODBC bridge.

    The first step is to acquire and install a JDBC driver. Sun does not include a JDBC driver with Java 2, but more than a dozen companies, including Informix, Oracle, Symantec, IBM, and Sybase, sell drivers or package them with commercial products. A list of available JDBC drivers can be found on Sun's JDBC site at http://java.sun.com/ products/jdbc/jdbc.drivers.html.

    The developers of the MySQL database offer Connector/J, a free open source JDBC driver developed by Mark Matthews. Some of these drivers are available to download for evaluation.

    To download this driver or find out more about it, visit the Web page http://www.mysql.com/downloads/api-jdbc.html.

    NetDirect offers a JDBC driver as part of the JDataConnect Server, which is available for trial download from http://www.j-netdirect.com.

    After you have downloaded and installed a JDBC driver, the steps for setting up a data source for JDBC are the same as with the JDBC-ODBC bridge:

    • Create the database.

    • Associate the database with a JDBC driver.

    • Establish a data source, which may include selecting a database format, database server, username, and password.

    Listing 20.3 is a Java application that uses the JDataConnect JDBC driver to access a database file called People.mdb. This database is a Microsoft Access file with contact information for U.S. presidents.

    Listing 20.3 The Full Text of Presidents.java

     1: import java.sql.*;
    2: 
    3: public class Presidents {
    4:   public static void main(String[] arguments) {
    5:     String data = "jdbc:JDataConnect:
    //127.0.0.1/Presidents"; 6: try { 7: Class.forName("JData2_0.sql.$Driver"); 8: Connection conn =
    DriverManager.getConnection( 9: data, "", ""); 10: Statement st = conn.createStatement(); 11: ResultSet rec = st.executeQuery( 12: "SELECT * FROM Contacts ORDER BY NAME"); 13: while(rec.next()) { 14: System.out.println(rec.getString("NAME")
    + "\n" 15: + rec.getString("ADDRESS1") + "\n" 16: + rec.getString("ADDRESS2") + "\n" 17: + rec.getString("PHONE") + "\n" 18: + rec.getString("E-MAIL") + "\n"); 19: } 20: st.close(); 21: } catch (Exception e) { 22: System.out.println("Error -- " +
    e.toString()); 23: } 24: } 25: }

    Using this application with another database and driver would require changes to lines 5 and 7.

    The JDataConnect Server can be used to connect remotely to servers on the Internet, so 127.0.0.1 could be replaced with an Internet address, such as db.naviseek.com:1150, if a JDataConnect Server is running at that location and port.

    Line 5 creates the database address that will be used when creating a Connection object representing the connection to the Presidents data source. This address includes more information than the one used with the JDBC-ODBC bridge driver, as shown:

    jdbc:JDataConnect://127.0.0.1/Presidents

    Line 7 of the Presidents application loads the JDBC driver included with the JDataConnect Server:

    JData2_0.sql.$Driver

    Caution - NetDirect's JDataConnect Server uses the ODBC Data Source Administrator to create a new data source associated with a database.

    On Windows NT, 2000, and XP, you must set up the data source using the System DSN tab rather than the User DSN tab. This is required because the JDataConnect Server connects to the database outside your account, so it must be available to all users on your computer.

    Before this program will run successfully, the JDataConnect Server must be started, unless it is already running (on a Windows NT or Windows XP system, it is set up as a service during JDataConnect installation and does not need to be started manually). The reference to 127.0.0.1 in line 6 refers to this server; 127.0.0.1 is a substitute for the name of your own machine.


    Configuration information for the data source and driver is provided by the company that developed the JDBC driver. The database address can vary widely from one JDBC driver implementation to another, although there should always be a reference to a server, a database format, and the name of the data source.

    If the People.mdb database exists, the database has been associated with an ODBC data source, and the JDBC driver has been set up correctly, the output of the Presidents application should be similar to the following (depending on the records in the database):

    Gerald Ford
    Box 927
    Rancho Mirage, CA 92270
    (734) 741-2218
    library@fordlib.nara.gov
    Jimmy Carter
    Carter Presidential Center
    1 Copenhill, Atlanta, GA 30307
    (404) 727-7611
    carterweb@emory.edu 
    Ronald Reagan
    11000 Wilshire Blvd.
    Los Angeles, CA 90024
    library@reagan.nara.gov
    George Bush
    Box 79798
    Houston, TX 77279
    (409) 260-9552
    library@bush.nara.gov
    Bill Clinton
    15 Old House Lane
    Chappaqua, NY 10514
    (501) 370-8000
    info@clintonpresidentialcenter.com
    George W. Bush
    White House, 1600 Pennsylvania Ave.
    Washington, DC 20500
    (202) 456-1414
    president@whitehouse.gov

    More Java Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 Days, 4th...
     

    Buy this book now. This article is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT