// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 9 - Data Access // Program to demonstrate one dimensional array access // Program to demonstrate SQL Database Access // DataBaseDemo.java 1 JAN 05 import java.sql.*; public class DataBaseDemo { public static void main(String args[]) { // declare variables for the data String name, ssNo, address, phoneNumber; // declare url for the database String url = "jdbc:odbc:Customers"; // The dB name is "Customers" // define the SQL query statement String sqlQuery = "SELECT Name, SSNo, Address, Phone FROM Customer"; try { // load the jdbc - odbc bridge driver for Windows-95 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // create connection instance "JavaDemo" is ID & "JavaIsFun" is password Connection aConnection = DriverManager.getConnection(url, "JavaDemo", "JavaIsFun"); // create statement object instance Statement aStatement = aConnection.createStatement(); // execute the SQL query statement ResultSet rs = aStatement.executeQuery(sqlQuery); // set the cursor & get first row boolean more = rs.next(); while (more) // loop while there are rows of data { // extract the data & display name = rs.getString(1); ssNo = rs.getString(2); address = rs.getString(3); phoneNumber = rs.getString(4); System.out.println("Name: " + name); System.out.println("SS No: " + ssNo); System.out.println("Address: " + address); System.out.println("Phone: " + phoneNumber); // get next row more = rs.next(); } // end while loop rs.close(); // close everything aStatement.close(); aConnection.close(); } // end try catch (ClassNotFoundException e) {System.out.println("Exception caught "+ e);} catch (SQLException e) { while (e != null) // we can have multiple exceptions here - print all { System.out.println("SQLException caught "+ e); e = e.getNextException(); } // end while loop } // end catch } // end main } // end DataBaseDemo.java