JDBC-ODBC Bridge Driver Enables Spreadsheet-as-database Interaction

JDBC-ODBC Bridge Driver Enables Spreadsheet-as-database Interaction

ava programmers who use JDBC may presume that the technology is restricted to interaction with database products such as IBM DB2 Universal Database, Informix, Oracle, or Microsoft SQL Server. This article illustrates how JDBC can be used in conjunction with ODBC to interact with Microsoft Excel spreadsheets as if they were databases.

Programming interfaces such as Apache Jakarta POI allow a Java application to programmatically modify Excel spreadsheets (see “Learn to Read and Write Microsoft Excel Documents with Jakarta’s POI“). Unfortunately, such APIs allow only cell-by-cell interaction with spreadsheets. They do not enable interacting with a spreadsheet as if it were a database, which utilizes the power of SQL. By tapping into the JDBC-ODBC bridge driver, you can work with spreadsheets as databases.

Setting Up Your Excel Spreadsheet as an ODBC Source
Start off by creating a simple Excel spreadsheet with which you will interact. Table 1 shows the structure you want to create. It contains population estimate data for the four most populous states in the US as estimated by the U.S. Census Bureau as of July 1, 2002.

Table 1. Population Estimates for Most Populous States
State Population
California 35,116,033
Texas 21,779,893
New York 19,157,532
Florida 16,713,149

Figure 1 shows a screenshot of the spreadsheet you will need to create. I named the spreadsheet “statepopulation.xls.” You can also download the spreadsheet.

Click to enlarge
Figure 1: The statepopulation.xls Spreadsheet

Next, you need to register your spreadsheet as an ODBC Data Source. These instructions assume that you are using Microsoft Windows 2000, but registering ODBC Data Sources in other Microsoft Windows operating systems should be quite similar.

Open the Windows Control Panel. Next, open up Administrative Tools. Double click on the Data Sources (ODBC) icon (see Figure 2).

Click to enlarge
Figure 2: The Data Sources Administrative Tool

In the User DSN tab, choose the Excel files option and click Add (see Figure 3).

Click to enlarge
Figure 3: Add an Excel File Data Source

In the subsequent driver selection page, choose the “Microsoft Excel Driver” option and click Finish (see Figure 4).

Click to enlarge
Figure 4: Choose the Microsoft Excel Driver

Next, you will be presented with a window in which you will select the Excel file that you want to setup as an ODBC source. Choose the Select Workbook button (see Figure 5) and choose the spreadsheet you created (statepopulation.xls).

Click to enlarge
Figure 5: Select the Workbook You Want to Setup as a Data Source

If you plan on using JDBC to write to the Excel file, you must deselect the Read Only option.

You should be returned to the ODBC Microsoft Excel Setup window. When you click the Options>> button, you see a Rows to Scan option. Since you have only a few records, you don’t need to increase the number.

Go ahead and name your Data Source Name as ExcelJDBCTest. You can include a description (see Figure 6).

Click to enlarge
Figure 6: Specifying a Name for the Data Source

In the ODBC Data Source Administrator screen shown in Figure 7, you should see the ODBC Data Source you just created.

Click to enlarge
Figure 7: Confirm the Data Source Was Established

Interact with Your Spreadsheet (with Java)
Now that you have created your ODBC source, it’s time to interact with it. The following class, InteractWithExcel.java, reads from your spreadsheet and displays the records to standard out:

import java.io.*;import java.net.*;import java.sql.*;import java.util.*;public class InteractWithExcel {	static final String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";	static final String DATABASE_URL = "jdbc:odbc:EXCELJDBCTest";	public static void main(String[] args) {		try {			Class.forName(DRIVER_NAME);			Connection conn = null;			conn = DriverManager.getConnection(DATABASE_URL);			Statement stmt = conn.createStatement();			ResultSet rs =				stmt.executeQuery("select State,Population from [Sheet1$]");			while (rs.next()) {				String state = rs.getString(1);				int population = rs.getInt(2);				System.out.println(state + " - " + population);			}			rs.close();			stmt.close();		} catch (ClassNotFoundException cnfe) {			System.err.println("ClassNotFoundException Was Thrown");			cnfe.printStackTrace();		} catch (SQLException sqle) {			System.err.println("SQLException Was Thrown");			sqle.printStackTrace();		}	}}

Note that the Driver you use is sun.jdbc.odbc.JdbcOdbcDriver, the JDBC-ODBC bridge driver. You don’t have to download anything to use it. The driver is built into the JDK.

Another important point is the EXCELJDBCTest portion of the string of the database URL you specify, jdbc:odbc:EXCELJDBCTest, is the name of the data source you specified earlier.

In order to perform a query, you have to have a database table with which to interact. But your spreadsheet contains sheets and not tables. However, the JDBC-ODBC bridge driver respects sheets as tables. This can be seen in the following statement:

stmt.executeQuery("select State,Population from [Sheet1$]");

You specify a table name of “[Sheet1$]” since your sheet name in Excel is (by default) “Sheet1” (see Figure 8). Note that the sheet name must be enclosed in brackets and end in a $. If, for example, your sheet name was MySheetName, your corresponding table would be [MySheetName$]. Your table grabs the column names from the first row of your spreadsheet (i.e., State and Population).

Click to enlarge
Figure 8: Map Spreadsheet to the SQL World

Figure 9 shows the execution of your application:

Click to enlarge
Figure 9: A Successful Run of the Program

You can change your query to: select sum(Population) from [MySheetName$] to realize the power of your setup. Go ahead and try this. You should see that executing the query in your application returns the expected result of 92766607, the sum of the four population numbers.

We can also write to your spreadsheet using a statement like the following:

int result = stmt.executeUpdate("insert into [Sheet1$] values('Illinois',12600620)");

This statement would add a new entry to your table, namely Illinois with a population of 12600620.

Spreadsheets as Databases
In this article, you learned how to interact with Excel spreadsheets as if they were database tables. You facilitated this ability by first registering a spreadsheet as an ODBC Data Source. You could then use the JDBC-ODBC bridge driver to interact with your spreadsheet using SQL.

With your newly learned technique, you can use the inherent power of SQL to read from and write to your spreadsheets. Leveraging SQL’s built-in functions adds to any Java programmer’s arsenal. However, you should not consider using Microsoft Excel for the database functionality of your enterprise. Use a much more scalable database solution (e.g., IBM DB2 UDB, Oracle Database, Microsoft SQL Server, etc) for housing your enterprise data.

devx-admin

devx-admin

Share the Post:
Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security.

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists

AI Tool

Unleashing AI Power with Microsoft 365 Copilot

Microsoft has recently unveiled the initial list of Australian clients who will benefit from Microsoft 365 (M365) Copilot through the exclusive invitation-only global Early Access Program. Prominent organizations participating in

Microsoft Egnyte Collaboration

Microsoft and Egnyte Collaboration

Microsoft has revealed a collaboration with Egnyte, a prominent platform for content cooperation and governance, with the goal of improving real-time collaboration features within Microsoft 365 and Microsoft Teams. This

Best Laptops

Top Programming Laptops of 2023

In 2023, many developers prioritize finding the best laptop for programming, whether at home, in the workplace, or on the go. A high-performing, portable, and user-friendly laptop could significantly influence

Renaissance Gaming Magic

AI Unleashes A Gaming Renaissance

In recent times, artificial intelligence has achieved remarkable progress, with resources like ChatGPT becoming more sophisticated and readily available. Pietro Schirano, the design lead at Brex, has explored the capabilities

New Apple Watch

The New Apple Watch Ultra 2 is Awesome

Apple is making waves in the smartwatch market with the introduction of the highly anticipated Apple Watch Ultra 2. This revolutionary device promises exceptional performance, robust design, and a myriad

Truth Unveiling

Unveiling Truths in Bowen’s SMR Controversy

Tony Wood from the Grattan Institute has voiced his concerns over Climate and Energy Minister Chris Bowen’s critique of the Coalition’s support for small modular nuclear reactors (SMRs). Wood points

Avoiding Crisis

Racing to Defy Looming Financial Crisis

Chinese property developer Country Garden is facing a liquidity challenge as it approaches a deadline to pay $15 million in interest associated with an offshore bond. With a 30-day grace

Open-Source Development

Open-Source Software Development is King

The increasingly digital world has led to the emergence of open-source software as a critical factor in modern software development, with more than 70% of the infrastructure, products, and services

Home Savings

Sensational Savings on Smart Home Security

For a limited time only, Amazon is offering massive discounts on a variety of intelligent home devices, including products from its Ring security range. Running until October 2 or while

Apple Unleashed

A Deep Dive into the iPhone 15 Pro Max

Apple recently unveiled its groundbreaking iPhone 15 Pro and iPhone 15 Pro Max models, featuring a revolutionary design, extraordinary display technology, and unrivaled performance. These new models are the first