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.
// 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.