Listing 1: Database query sample using JDBC Statement stmt = null; Connection conn = null; ResultSet rs = null; String url = "jdbc:db2:sample"; String strStatement = "select * FROM employee"; String driver = "COM.ibm.db2.jdbc.app.DB2Driver"; try { Class.forName(driver); conn = DriverManager.getConnection(url); stmt = conn.createStatement(); rs = stmt.executeQuery(strStatement); while(rs.next()) { printRow(rs,""); } stmt.close(); conn.close(); } catch(ClassNotFoundException cnfe) { System.out.println("Class was not found!\n" + cnfe.getMessage()); } catch(SQLException sqle) { System.out.println("SQL Exception!\n" + sqle.getMessage()); } Listing 2: ADO.NET sample program listing OleDbConnection con = null; try { string connectionString = "Provider=IBMDADB2.1;User ID=spchang;Password=sp2501c;Data Source=SAMPLE"; con = new OleDbConnection(); con.ConnectionString = connectionString; OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; cmd.CommandText = "Select * FROM EMPLOYEE"; con.Open(); OleDbDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { string empno = reader.GetString(0); string firstname = reader.GetString(1); Console.Out.WriteLine(empno + "--->" + firstname); } }catch (Exception e) { Console.Out.WriteLine(e.Message); }finally { try { if (con != null) { con.Close(); } }catch(Exception ex) { Console.Out.WriteLine(ex.Message); } } Listing 3: Using the using statement string connectionString = "Provider=IBMDADB2.1;User ID=spchang;Password=sp2501c;Data Source=SAMPLE"; using (OleDbConnection con = new OleDbConnection()) { con.ConnectionString = connectionString; OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; cmd.CommandText = "Select * FROM EMPLOYEE"; con.Open(); using (OleDbDataReader reader = cmd.ExecuteReader()) { while(reader.Read()) { string empno = reader.GetString(0); string firstname = reader.GetString(1); Console.Out.WriteLine(empno + "--->" + firstname); } } } Listing 4: Using the disconnected model OleDbConnection con = null; Try { DataSet ds = new DataSet(); string connectionString = "Provider=IBMDADB2.1;User ID=spchang;Password=sp2501c;Data Source=SAMPLE"; con = new OleDbConnection(); con.ConnectionString = connectionString; OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; cmd.CommandText = "Select * FROM EMPLOYEE"; con.Open(); OleDbDataAdapter oledbadapter = new OleDbDataAdapter(cmd.CommandText,con); oledbadapter.Fill(ds); foreach (DataRow r in ds.Tables[0].Rows) { for (int i=0; i0) Console.Out.Write(", "); Console.Out.Write(r.ItemArray[i]); } // for Console.Out.WriteLine(); } // foreach }catch (Exception e) { Console.Out.WriteLine(e.Message); } Listing 5: Using the XML functionality of ADO.NET string connectionString = "Provider=IBMDADB2.1;User ID=spchang;Password=sp2501c;Data Source=SAMPLE"; con = new OleDbConnection(); con.ConnectionString = connectionString; OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; cmd.CommandText = "Select * FROM EMPLOYEE"; con.Open(); DataSet dataset = new DataSet("MyDataSet"); OleDbDataAdapter adapter = new OleDbDataAdapter(cmd.CommandText, con); adapter.SelectCommand = cmd; adapter.Fill(dataset); string xmlstr = dataset.GetXml(); Console.Out.WriteLine(xmlstr); Listing 6: Message explaining the exception try { // Code that could generate an exception goes here. // If an exception is generated, the catch block below // will print out information about it. } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } try { Class.forName("myDriverClassName"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } Listing 7: The catch block is complete try { // Code that could generate an exception goes here. // If an exception is generated, the catch block below // will print out information about it. } catch(SQLException ex) { System.out.println("\n--- SQLException caught ---\n"); System.out.println("Message: " + ex.getMessage ()); System.out.println("SQLState: " + ex.getSQLState ()); System.out.println("ErrorCode: "+ ex.getErrorCode ()); ex = ex.getNextException(); }