Minimum Retrieval Sample
The following example is a Java Main class that connects to and retrieves
rows from the Orders sample database using the Omnidex JDBC driver.
With a few minor changes, this code should compile and run to demonstrate
the Omnidex JDBC driver. The code requiring change is highlighted
with comments indicating what should replace it. Other important differences
will be displayed in red.
Basic Setup
Following are a few basic steps necessary to run this sample:
- Make sure the Java SDK is properly installed. Refer to the installation
instructions accompanying the Java SDK.
- Make sure the Omnidex server software is properly installed on the
server.
- Make sure the Orders sample database is properly installed with Omnidex.
- Start an Omnidex Network Server listener on the server.
- Make sure the Omnidex client software is properly installed on the
client and the omnidex.jar file is in the CLASSPATH.
- Create an Omnidex Data Source using Data Source Editor (DSEDIT) that
connects to the orders sample database on the server, through the running
Omnidex Network Server listener. This Data Source must be a File
data source.
- Modify the OdxJDBCTest.java file (shown below).
- Compile and run OdxJDBCTest java program.
Code
/**
* Notice that both java.sql.* and omnidex.jdbc.* are imported.
* This is because the Omnidex JDBC driver throws SQLException and
* the java.sql.DriverManager is used to register the Omnidex driver.
*/
import java.io.*;
import java.sql.*;
import omnidex.jdbc.*;
public class OdxJDBCTest {
/** Default constructor - Not used in this example. */
public TestJDBCcal() {
}
/**
* Main method. No command line arguments accepted.
*/
public static void main(String[] args){
/**
Instantiate JDBC objects */
OdxJDBCConnection
conn = null;
OdxJDBCStatement
stmt = null;
OdxJDBCResultSet
rs = null;
/**
Register the Omnidex JDBC driver */
try{
Class.forName("omnidex.jdbc.OdxJDBCDriver");
}catch(ClassNotFoundException
ce) {
pw.println("Driver
error: " + ce);
System.out.println("Driver
error: " + ce);
System.exit(0);
}
/**
Since DriverManager.getConnection returns a java.sql Connection object,
* you must cast
the Connection to an OdxJDBCConnection.
* The highlighted
information is the OMNIDEX FILE DSN that points to an Omnidex
* Environment
Catalog
*/
try{
conn
= (OdxJDBCConnection)DriverManager.getConnection(
"jdbc:omnidex:c:\\odxtesting\\dsn\\odx_file_ordersffl.dsn");
}catch(SQLException
se) {
pw.println("Connect
error: " + se);
System.out.println("Connect
error: " + se);
System.exit(0);
}
/**
The SQL statement to be executed */
String
sql = "select * from customers where state='co'";
/**
conn.createStatement java.sql Statement object,
* you must cast
the Statement to an OdxJDBCStatement.
* The same is
true of the ResultSet returned by stmt.getResultSet
*/
try{
stmt
= (OdxJDBCStatement)conn.createStatement();
stmt.execute(sql);
rs = (OdxJDBCResultSet)stmt.getResultSet();
while(rs.next()){
System.out.print(rs.getString(1)
+ "\n");
System.out.print(rs.getString(2)
+ "\n");
System.out.print(rs.getString(3)
+ "\n");
System.out.println(rs.getString(4));
}
}catch(SQLException
se) {
System.out.println("Execute
query error: " + se);
}
/** Close all of the objects */
try{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}catch(SQLException
se) {
System.out.println("Close
error: " + se);
}
System.out.println("End
of program.");
} // End Main
} // End Class
Top
|