// 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 ONE record in the customer table. // // Peter van der Linden August 1996. import java.sql.*; import Pausa; class simple { public static void main (String args[]) { String url = "jdbc:odbc:myDSN"; String query = "SELECT * FROM " + // "Clientes "; "Clientes WHERE CódigoDoCliente = 'QUICK'"; // "Customers WHERE CustomerID = 'QUICK'"; try { // Load the jdbc-odbc bridge driver Class.forName ("com.ms.jdbc.odbc.JdbcOdbcDriver"); // Attempt to connect to a driver. 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(); } } }