// A simple program to demonstrate database access in Java. // This uses the JDBC-ODBC bridge to talk to a Microsoft Access database. // It uses the example database that comes with MS Access version 7.0. // // This SQL query retrieves ALL records in the customer table. // // Peter van der Linden August 1996. import java.sql.*; import Pausa; class simple2 { public static void main (String args[]) { String url = "jdbc:odbc:myDSN"; String query = "SELECT * FROM Customers " + "WHERE CustomerID = 'QUICK'"; try { // Load the jdbc-odbc bridge driver Class.forName ("jdbc.odbc.JdbcOdbcDriver"); // Attempt to connect to a driver. Each // registered driver will be loaded until // one is found that can process this URL Connection con = DriverManager.getConnection ( url, "", "" ); // Create a Statement object so we can submit // SQL statements to the driver Statement stmt = con.createStatement (); // Submit a query, creating a ResultSet object ResultSet rs = stmt.executeQuery (query); // Display all columns and rows from the result set printResultSet (rs); rs.close(); stmt.close(); con.close(); } catch (SQLException ex) { while (ex != null) { System.out.println ("SQL Exception: " + ex.getMessage ()); ex = ex.getNextException (); } } catch (java.lang.Exception ex) { ex.printStackTrace (); } new Pausa(); } private static void printResultSet (ResultSet rs) throws SQLException { int numCols = rs.getMetaData().getColumnCount (); while ( rs.next() ) { for (int i=1; i<=numCols; i++) { System.out.print(rs.getString(i) + " | " ); } System.out.println(); } } }