Home arrow Java arrow Page 3 - Using CSV Files as Databases and Interacting with Them Using Java

Using CSV Files as Databases and Interacting with Them Using Java

In this article, we’ll show the reader how to use a simple CSV (comma-separated values) file as a simplistic database setup.  We will leverage the JDBC-ODBC to interact with a CSV file, reading and writing to it with SQL.  Such a technique can be particularly useful for someone trying to write simple database code, but not able to because a local database instance is not pragmatic. 

Author Info:
By: Kulvir S. Bhogal & Kwang Sik Kang
Rating: 4 stars4 stars4 stars4 stars4 stars / 85
January 28, 2004
  1. · Using CSV Files as Databases and Interacting with Them Using Java
  2. · Creating an ODBC Data Source
  3. · Let's Interact

print this article

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
import java
import java

public class 
CSVInteract {
 public static 
void main(String[] args) {
String DBURL "jdbc:odbc:StateBirdDS";
"Interacting with CSV file as if it were a true 
try {
   Connection conn 


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


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.


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.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2015 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials