This tutorial explains how to connect to a remote ODBC database from Android. Although the example ODBC database in the tutorial is an Excel spreadsheet on Windows, the method described here can be used to connect Android to ODBC databases on Linux and UNIX platforms as well.
We used the following software on a Windows machine:
Click Next to progress through the wizard, and then click Finish.
AndroidManifest.xml
, add the following line after your <manifest>
tag:
<uses-permission android:name="android.permission.INTERNET"/>
This allows your mobile device to open network sockets. (You need to be able to access the JDBC-ODBC Bridge server on TCP port 8831
from your mobile device.) Save the changes.
EJOB.jar
file.
This can be located on the machine running the JDBC-ODBC Bridge server.
The default location on a 32-bit machine for EJOB.jar
is:
drive:\Program Files\Easysoft\Easysoft JDBC-ODBC Bridge\Jars
The default location on a 64-bit machine for EJOB.jar
is:
drive:\Program Files (x86)\Easysoft\Easysoft JDBC-ODBC Bridge\Jars
Click to select EJOB.jar
in the JAR file list.
Ensure that the Into Folder value is your project's src
folder. For example, ODBCAndroid/src
.
EJOB.jar
to your Eclipse build path. To do this, right-click on your project, and chose Build Path > Add External Archives. Double-click the EJOB.jar
file.src
tree, and then double-click on the MainActivity.java
file.
In MainActivity.java
, replace the boilerplate code with this code:
package com.easysoft.ODBCAndroid; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.TextView; import easysoft.sql.*; import java.sql.*; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* To prove this works, we will save the output to a string and use setContentView to display the information */ TextView tv = new TextView(this); String HWOutput=""; try { /* Create a JDBC-ODBC Bridge connection. You need to replace myjobmachine with the host name or IP address of your JDBC-ODBC Bridge machine */ String jobUrl= "jdbc:easysoft://myjobmachine:8831/ExcelJDBC-ODBC Bridge"; HWOutput += jobUrl + "\n\n"; /* Connect to the target ODBC driver using the JDBC-ODBC Bridge */ easysoft.sql.jobDriver driver = (easysoft.sql.jobDriver)Class.forName("easysoft.sql.jobDriver").newInstance(); java.util.Properties p = new java.util.Properties(); Connection con = DriverManager.getConnection(jobUrl,p); DatabaseMetaData dbMD = null; /* Create and execute a query that gets the Excel data */ Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select Product, \"Stock Level\" from \"Sheet1$\""); String Prod=""; String Lev=""; /* For each row, place the data into the output string */ while (rs.next()) { Prod = rs.getString("Product"); HWOutput += "Product : " + Prod + " : "; Lev = rs.getString("Stock Level"); HWOutput += Lev + "\n"; } con.close(); HWOutput += "\n"; } catch(Exception e) { /* If there were any errors, store them to the output string */ HWOutput += e + "\n\n"; e.printStackTrace(); } /* Display the output */ tv.setText(HWOutput); setContentView(tv); } }
Now run the code.