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