Feeds

Escape from Access DB

The relational database that polarises programmers

Combat fraud and increase customer satisfaction

Do you like Microsoft Access? Well, thanks to an intuitive and powerful user interface and being bundled as part of Office, it has established a niche for itself in many organisations, sometimes unofficially.

The combination of recordable macros, wizard-generated reports, query-by-example and, crucially, the provision of program support using Visual Basic for Applications has made MS Access the tool of choice for legions of developers of varying levels of ability and technical proficiency.

Not only has this led to a proliferation of applications, it has also produced a proliferation of "rescue" projects as those applications have grown in scope, size and complexity. For enterprise developers using Java this data need not remain out of reach thanks to the wonders of JDBC-ODBC bridges.

ODBC (Open Database Connectivity) is an API that provides programmatic access to data using SQL. ODBC adopts a layered approach; an application written to ODBC can talk to any underlying data source that has an ODBC driver. In the case of Access, ODBC support has been there almost from the beginning. In the Java world, a similar architecture exists called JDBC (Java Database Connectivity) in which Java applications written to the API can talk to any data source that has a JDBC driver. Put these together in the form of a JDBC-ODBC bridge and you have a platform-independent mechanism for Java to talk to any DBMS that has JDBC or ODBC drivers.

In the case of Access, the first step to opening the database to ODBC is to create an ODBC Data Source Name for your database. The simplest method is to load the Data Source Administrator from within the Windows Administrator tools (via the Control Panel if you're not running a server version of Windows).

A User DSN creates and registers a DSN for a single user, a System DSN is accessible to all users and a File DSN stores the DSN information to a file which can be shared among different users. In each case the DSN associates a user-defined label with a database driver (Microsoft Access Driver *.mdb) and a specific instance of the database. Once you create the DSN, any program that uses the ODBC API to talk to the underlying database can use it.

In the case of Java, the normal method for talking to a database is to use JDBC. First, the database driver is registered and then a specific database URL is used to establish a connection which can be used to create the statement object. Once this has been done, it's possible to interact with the database using SQL, stored procedures etc.

In the following code snippet, we are going to connect to an Access database which has a DSN of MyAccessDB using Sun's JDBC-ODBC bridge driver

import java.sql.*;

public class AccessDAO {

        private Connection con;
        private Statement st;
private static final String url="jdbc:odbc:MyAccessDB"; 
        private static final String className="sun.jdbc.odbc.JdbcOdbcDriver"; 
        private static final String user=""; 
        private static final String pass=""; 

        AccessDAO()throws Exception { 


        Class.forName(className); 
                con = DriverManager.getConnection(url, user, pass); 
                st = con.createStatement();  

                //do whatever database processing is required 
        } 
} 

It's not just the user data that can be queried using the JDBC-ODBC bridge, the database metadata can also be accessed. The following program will report all of the SQL data types supported by the version of Access used by the database contained in the MyAccessDB DSN:

import java.sql.*; 

public class AccessDAO { 

        private Connection con; 
        private Statement st; 
        private static final String url="jdbc:odbc:MyAccessDB"; 
        private static final String className="sun.jdbc.odbc.JdbcOdbcDriver"; 
        private static final String user=""; 
        private static final String pass=""; 
                
        public AccessDAO()throws Exception { 

                Class.forName(className); 
                con = DriverManager.getConnection(url, user, pass); 
                DatabaseMetaData dbMetaData  = con.getMetaData(); 
                ResultSet dbTypes = dbMetaData.getTypeInfo(); 
                String typeName; 
                System.out.println("Supported Database Types:"); 
                while (dbTypes.next()){ 
                        typeName = dbTypes.getString("TYPE_NAME"); 
                        System.out.println(typeName); 
                } 
                
        } 
        
        public static void main(String[] args) throws Exception{ 
                new AccessDAO(); 
        } 
        
} 

Of course, it's not just Access that supports ODBC. There are many other databases which support the ODBC API, and these aren't limited to the Windows operating system either. Furthermore, ODBC can be used with Excel and represents an alternative to the use of Apache POI or JExcelAPI as methods for interfacing Java and Excel. ®

Combat fraud and increase customer satisfaction

More from The Register

next story
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Oh no, Joe: WinPhone users already griping over 8.1 mega-update
Hang on. Which bit of Developer Preview don't you understand?
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
Ditch the sync, paddle in the Streem: Upstart offers syncless sharing
Upload, delete and carry on sharing afterwards?
New Facebook phone app allows you to stalk your mates
Nearby Friends feature goes live in a few weeks
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
prev story

Whitepapers

SANS - Survey on application security programs
In this whitepaper learn about the state of application security programs and practices of 488 surveyed respondents, and discover how mature and effective these programs are.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Top three mobile application threats
Learn about three of the top mobile application security threats facing businesses today and recommendations on how to mitigate the risk.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.