Java
  Home arrow Java arrow Page 3 - Using CSV Files as Databases and Interacti...
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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
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

Using CSV Files as Databases and Interacting with Them Using Java
By: Kulvir S. Bhogal & Kwang Sik Kang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 72
    2004-01-28

    Table of Contents:
  • Using CSV Files as Databases and Interacting with Them Using Java
  • Creating an ODBC Data Source
  • Let's Interact

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Using CSV Files as Databases and Interacting with Them Using Java - Let's Interact


    (Page 3 of 3 )

    Now it is time for us to interact with the new data source.  Take a look at the code below. 

     
    import java.sql.Connection
    import java
    .sql.DriverManager
    import java
    .sql.ResultSet
    import java
    .sql.Statement

     
    public class 
    CSVInteract {
     
     public static 
    void main(String[] args) {
     
      final 
    String DBURL "jdbc:odbc:StateBirdDS";
     
      
    System.out.println
       
    "Interacting with CSV file as if it were a true 
        database."
    ); 
      
    try {
     
       Class.
    forName
        
    "sun.jdbc.odbc.JdbcOdbcDriver"); 
       Connection conn 

       DriverManager
    .getConnection
         DBURL

         
    ""
         
    "");
     
       
    Statement st conn.createStatement();
     
       
    // Example of Select Query 
       ResultSet rec = st.executeQuery( 
         "SELECT StateBird FROM statebirds.txt 
          where STATE='Florida'"); 
       while (rec.next()) { 
        System.out.println( 
         "Florida's State Bird is: " 
          + rec.getString( 
           "STATEBIRD")); 
       } 
       // Example of Insert 
       System.out.println( 
        "Inserting Record"); 
       String insertStatement = 
        "INSERT INTO statebirds.txt 
         values('Wyoming','Western Meadowlark')"; 
       
    st.executeUpdate(insertStatement);
       
    // Confirm Insertion 
       rec = st.executeQuery( 
         "SELECT StateBird FROM statebirds.txt 
          where STATE='Wyoming'"); 
       while (rec.next()) { 
        System.out.println( 
         "Wyoming's State Bird is: " 
          + rec.getString("STATEBIRD")); 
       } 
      
    } catch (Exception e) {
       e
    .printStackTrace(); 
      

     



    The code first displays the state bird of Florida to the console.  Next, we insert a record for the state of Wyoming.  Then we confirm the insertion by displaying the added record.  The file can also be opened to view the update.
     
    Readers familiar with the JDBC™ Application Programming Interface (API) should find the code straightforward. To learn about JDBC, take the “JDBC Short Course” here.

    The driver we use is sun.jdbc.odbc.JdbcOdbcDriver
    This driver is the JDBC-ODBC bridge driver.  The good thing is that you don’t have to download anything to use it.  The driver comes built into the Java Development Kit™ (JDK). 

    Also, notice that the table we refer to in our code is the name of our file name: statebirds.txt

    Note that at the time of this article’s writing, the Microsoft ODBC Text Driver does not support the deletion of records.

    Conclusion

    This article showed how to setup a CSV file as a no-frills database that can be accessed with JDBC.  Such a technique is particularly useful when access to a database is not available, yet need to test your JDBC code. 

    The reader is cautioned not to use a CSV file in true production level code.  One should consider a more scalable solution designed to truly provide database functionality (e.g., IBM® DB2™ Universal Database).  However, the technique described in this article is useful for individuals who are testing their JDBC code, providing a database (albeit quite limited in functionality) to work against.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 5 hosted by Hostway