Java
  Home arrow Java arrow Page 3 - Reading and Writing Data Using JDBC and XM...
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Reading and Writing Data Using JDBC and XML
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 4
    2006-06-08

    Table of Contents:
  • Reading and Writing Data Using JDBC and XML
  • Database Drivers
  • Connecting to an ODBC Data Source
  • Retrieving Data from a Database Using SQL
  • Writing Data to a Database Using SQL

  • 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


    Reading and Writing Data Using JDBC and XML - Connecting to an ODBC Data Source


    (Page 3 of 5 )

    Your first project today is a Java application that uses a JDBC-ODBC bridge to connect to a Microsoft Access file.

    The Access file for this project is world20.mdb, a database of world energy statistics published by the U.S. Energy Information Administration. The Coal table in this database includes three fields you will be using in the project:

    • Country

    • Year

    • Anthracite Production

    The database used in this project is included on this book's official Web site at http://www.java21days.com.

    To use this database, you must have an ODBC driver on your system that supports Microsoft Access files. Using the ODBC Data Source Administrator (or a similar program if you're on a non-Windows system), you must create a new ODBC data source associated with world20.mdb.

    Other setup work might be needed depending on the ODBC drivers present on your system, if any. Consult the documentation included with the ODBC driver.


    Caution - Though the process of using a Microsoft Access database on a Windows system via ODBC is fairly straightforward, with other database formats and systems you might need to install an ODBC driver and learn more about its use before you try to create a JDBC-ODBC application.


    After you have downloaded world20.mdb to your computer or found another database that's compatible with the ODBC drivers on your system, the final step in getting the file ready for JDBC-ODBC is to create a data source associated with it. Unlike other input-output classes in Java, JDBC doesn't use a filename to identify a data file and use its contents. Instead, a tool such as the ODBC Data Source Administrator is used to name the ODBC source and indicate the file folder where it can be found.

    In the ODBC Data Source Administrator, click the User DSN tab to see a list of data sources that are available. To add a new one associated with world20.mdb (or your own database), click the Add button, choose an ODBC driver, and then click the Finish button.

    A Setup window opens that you can use to provide a name, short description, and other information about the database. Click the Select button to find and choose the database file.

    Figure 20.3 shows the Setup window used to set up world20.mdb as a data source in the ODBC Data Sources Administrator.


    Figure 20.3  The driver Setup window.

    After a database has been associated with an ODBC data source, working with it in a Java program is relatively easy if you are conversant with SQL.

    The first task in a JDBC program is to load the driver (or drivers) that will be used to connect to a data source. A driver is loaded with the Class.forName(String) method. Class, part of the java.lang package, can be used to load classes into the Java interpreter. The forName(String) method loads the class named by the specified string. A ClassNotFoundException may be thrown by this method.

    All programs that use an ODBC data source use sun.jdbc.odbc.JdbcOdbcDriver, the JDBC-ODBC bridge driver included with Java 2. Loading this class into a Java interpreter requires the following statement:

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    After the driver has been loaded, you can establish a connection to the data source by using the DriverManager class in the java.sql package.

    The getConnection(String, String, String) method of DriverManager can be used to set up the connection. It returns a reference to a Connection object representing an active data connection.

    The three arguments of this method are as follows:

    • A name identifying the data source and the type of database connectivity used to reach it

    • A username

    • A password

    The last two items are needed only if the data source is secured with a username and a password. If not, these arguments can be null strings ("").

    The name of the data source is preceded by the text jdbc:odbc: when using the JDBC-ODBC bridge, which indicates the type of database connectivity in use.

    The following statement could be used to connect to a data source called Payroll with a username of Doc and a password of 1rover1:

    Connection payday = DriverManager.getConnection(
    "jdbc:odbc:Payroll", "Doc", "1rover1");

    After you have a connection, you can reuse it each time you want to retrieve or store information from that connection's data source.

    The getConnection() method and all others called on a data source throw SQLException errors if something goes wrong as the data source is being used. SQL has its own error messages, and they are passed along as part of SQLException objects.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek