This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
dev:jdbc:sample [2011/01/12 20:10] els created |
dev:jdbc:sample [2016/06/28 22:38] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | |||
{{page>:top_add&nofooter&noeditbtn}} | {{page>:top_add&nofooter&noeditbtn}} | ||
====== Development: JDBC Interface ====== | ====== Development: JDBC Interface ====== | ||
- | [[dev:jdbc:home|Overview]] | [[dev:jdbc:datasources|JDBC Datasources]] | **[[dev:jdbc:sample|Sample Program]]** | + | [[dev:jdbc:home|Overview]] | **[[dev:jdbc:sample|Sample Java Program]]** |
---- | ---- | ||
- | ===== Sample JDBC Program ===== | + | ===== Sample Java Program ===== |
+ | |||
+ | The following is a very simple Java program that shows a basic connection to an Omnidex Environment and simple query processing. | ||
+ | |||
+ | <code java> | ||
+ | /* Omnidex JDBC Example: Simple Select */ | ||
+ | |||
+ | import java.io.*; | ||
+ | import java.sql.*; | ||
+ | |||
+ | public class OdxJDBCSample | ||
+ | { | ||
+ | public OdxJDBCSample() | ||
+ | { | ||
+ | } | ||
+ | |||
+ | // Main | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | // Declare JDBC objects | ||
+ | Connection conn = null; | ||
+ | Statement stmt = null; | ||
+ | ResultSet rs = null; | ||
+ | |||
+ | // The SQL statement to be executed | ||
+ | String sql = "select NAME, PHONE from INDIVIDUALS"; | ||
+ | |||
+ | try | ||
+ | { | ||
+ | // Load and initialize the Omnidex JDBC driver | ||
+ | Class.forName("omnidex.jdbc.OdxJDBCDriver"); | ||
+ | |||
+ | // Connect to the Omnidex Environment File | ||
+ | conn = DriverManager.getConnection("jdbc:omnidex:[server1:7555]c:\\class\\simple.xml"); | ||
+ | |||
+ | // create the Statement object | ||
+ | stmt = conn.createStatement(); | ||
+ | |||
+ | // execute the sql | ||
+ | rs = stmt.executeQuery(sql); | ||
- | The following is a very simple program that shows a basic connection to an Omnidex Environment and simple query processing using the standard JDBC API. | + | // Print the results from the Result Set returned after the Select |
+ | while(rs.next()) | ||
+ | { | ||
+ | System.out.print(rs.getString(1) + "\t"); | ||
+ | System.out.println(rs.getString(2)); | ||
+ | } | ||
+ | } | ||
+ | catch(ClassNotFoundException ce) | ||
+ | { | ||
+ | System.out.println("Driver error: " + ce); | ||
+ | } | ||
+ | catch(SQLException se) | ||
+ | { | ||
+ | System.out.println("SQL error: " + se); | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | // Close the Result Set, Statement and Connection 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); | ||
+ | } | ||
+ | } | ||
- | <code> | + | System.out.println("End of program."); |
- | Put sample program here ... | + | } // End Main |
- | + | } // End Class | |
</code> | </code> | ||
===== ===== | ===== ===== | ||
- | **[[dev:jdbc:datasources|Prev]]** | + | **[[dev:jdbc:home|Prev]]** |
====== Additional Resources ====== | ====== Additional Resources ====== |