This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
tutorials:simple_flatfile_java_jdbc [2009/12/05 18:46] tdo created |
tutorials:simple_flatfile_java_jdbc [2012/10/26 14:46] (current) |
||
|---|---|---|---|
| Line 2: | Line 2: | ||
| <html><div align="center"><span style="color:red">DRAFT</span></div></html> | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
| ====== Simple Java program using JDBC to access an Omnidex Environment ====== | ====== Simple Java program using JDBC to access an Omnidex Environment ====== | ||
| + | ===== Step 1 - Create the JAVA program to test JDBC Connect ===== | ||
| + | |||
| + | <code JAVA> | ||
| + | /** Java Program to test a connection to the Omnidex | ||
| + | * JDBC driver. | ||
| + | * Make sure OdxNet is running | ||
| + | * Create a JDBC Datasource file with DSEDIT. | ||
| + | * Make sure there is a path setting to the Omnidex bin directory. | ||
| + | */ | ||
| + | import java.io.*; | ||
| + | import java.sql.*; | ||
| + | public class OdxTestConnection | ||
| + | { | ||
| + | static public void main(String args[]) | ||
| + | { | ||
| + | Connection odxconnection = null; | ||
| + | try | ||
| + | { | ||
| + | Class.forName("omnidex.jdbc.OdxJDBCDriver"); | ||
| + | } | ||
| + | catch ( ClassNotFoundException ce ) | ||
| + | { | ||
| + | ce.printStackTrace(); | ||
| + | return; | ||
| + | } | ||
| + | try | ||
| + | { | ||
| + | odxconnection = DriverManager.getConnection | ||
| + | ("jdbc:omnidex:c:\\dev\\odx\\tiny\\tiny.dsn"); | ||
| + | System.out.println("Omnidex JDBC Connection successful!"); | ||
| + | // Do SELECTS here | ||
| + | } | ||
| + | catch (SQLException e) | ||
| + | { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | if (odxconnection != null ) | ||
| + | { | ||
| + | try { odxconnection.close(); } | ||
| + | catch( SQLException e ) | ||
| + | { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } // end main | ||
| + | } //end class | ||
| + | </code> | ||
| + | ===== Step 2 Run OdxNet ===== | ||
| + | |||
| + | * Under Windows, open a console window | ||
| + | * Run OdxNet | ||
| + | * OdxNet will default to listening on PORT 7555. | ||
| + | |||
| + | ===== Step 3 Create an Omnidex Datasource File ===== | ||
| + | ===== Step 4 Compile the JAVA application ===== | ||
| + | Before compiling the java source program, make sure the following: | ||
| + | * A path exists to the Omnidex.jar file containing the Omnidex JDBC driver. | ||
| + | |||
| + | C:\dev\java>javac OdxTestConnection.java | ||
| + | |||
| + | ===== Step 5 Run and test the JAVA applications ===== | ||
| + | ===== Step 6 Add Test SQL SELECT Statement ===== | ||
| + | <code java> | ||
| + | /** Omnidex JDBC Example: Simple Select | ||
| + | */ | ||
| + | import java.io.*; | ||
| + | import java.sql.*; | ||
| + | |||
| + | public class OdxSimpleSelect { | ||
| + | |||
| + | public OdxSimpleSelect() { } | ||
| + | |||
| + | // Main | ||
| + | public static void main(String[] args){ | ||
| + | |||
| + | // Declare JDBC objects | ||
| + | Connection odxconn = null; | ||
| + | Statement odxstmt = null; | ||
| + | ResultSet odxrs = null; | ||
| + | |||
| + | // Register the Omnidex JDBC driver | ||
| + | try{Class.forName("omnidex.jdbc.OdxJDBCDriver");} | ||
| + | catch(ClassNotFoundException ce) | ||
| + | { | ||
| + | System.out.println("Driver error: " + ce); | ||
| + | System.exit(0); | ||
| + | } | ||
| + | // Connect to the Omnidex Environment Catalog via a File DataSource | ||
| + | // DISC recommends using a connection string instead of a data source. | ||
| + | try | ||
| + | { | ||
| + | odxconn = DriverManager.getConnection | ||
| + | ("jdbc:omnidex:c:\\dev\\odx\\tiny\\tiny.dsn"); | ||
| + | } | ||
| + | catch(SQLException se) | ||
| + | { | ||
| + | System.out.println("Connect error: " + se); | ||
| + | System.exit(0); | ||
| + | } | ||
| + | |||
| + | // The SQL statement to be executed | ||
| + | String sql = "select * from table1 where mytext = 'bears'"; | ||
| + | try | ||
| + | { | ||
| + | odxstmt = odxconn.createStatement(); | ||
| + | odxstmt.execute(sql); | ||
| + | odxrs = odxstmt.getResultSet(); | ||
| + | |||
| + | // Print the results from the Result Set returned after the Select | ||
| + | while(odxrs.next()) | ||
| + | { | ||
| + | System.out.print(odxrs.getString(1) + "\n"); | ||
| + | System.out.println(odxrs.getString(2)); | ||
| + | } | ||
| + | } | ||
| + | catch(SQLException se) | ||
| + | { | ||
| + | System.out.println("Execute query error: " + se); | ||
| + | } | ||
| + | |||
| + | // Close the Result Set, Statement and Connection objects */ | ||
| + | try | ||
| + | { | ||
| + | if(odxrs != null) odxrs.close(); | ||
| + | if(odxstmt != null) odxstmt.close(); | ||
| + | if(odxconn != null) odxconn.close(); | ||
| + | } | ||
| + | catch(SQLException se) | ||
| + | { | ||
| + | System.out.println("Close error: " + se); | ||
| + | } | ||
| + | |||
| + | System.out.println("End of program."); | ||
| + | |||
| + | } // End Main | ||
| + | } // End Class | ||
| + | </code> | ||
| + | |||
| + | ===== Compile and test the SELECT Statement ===== | ||
| + | |||
| + | {{:cmd:cmd_java_jdbc_simple.png|}} | ||
| {{page>:bottom_add&nofooter&noeditbtn}} | {{page>:bottom_add&nofooter&noeditbtn}} | ||