// This program assumes that there is a database // with a table called PHONENUM with two fields: // NAME and MOBILENUM // its registered ODBC data source name is phonebook package servlets; import java.sql.*; public class Select { public static String getNum( String name ) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:phonebook"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT MOBILENUM FROM PHONENUM WHERE NAME = \'"+name+"\'"); boolean recordExists = rs.next(); String mobilenum; if ( recordExists ) { mobilenum = rs.getString(1); } else { mobilenum = "no mobile number"; } stmt.close(); con.close(); return mobilenum; } public static void main(String args[]) throws Exception { String number = getNum( "MARY" ); System.out.println( "MARY's number is " + number ); } }