package quickstart; import java.io.*; import java.sql.*; import java.util.*; import de.mathema.pride.*; /** * Simple command line client to interactively run a few database operations. * It demonstrates the most common operations of record creation, update, * retrieval and deletion. */ public class CustomerClient implements SQLExpression.Operator { private String prompt; private BufferedReader input; public CustomerClient( String prompt ) { this.prompt = prompt; try { input = new BufferedReader(new InputStreamReader( System.in ) ); } catch ( Exception uee ) { System.out.println( "error: trying to open System.in ..." ); System.exit(0); } } /** Read-eval-print loop of this interactive client */ protected void work() throws Exception { usage(); String cmd_ln = null; while ( true ) { System.out.print( prompt ); System.out.flush(); cmd_ln = input.readLine(); StringTokenizer st = new StringTokenizer( cmd_ln, " ", false ); int count = st.countTokens(); String[] str = new String[count]; for ( int i = 0; i < count; i++ ) { str[i] = st.nextToken(); } executeCmd (str); } } /** Databaes record creation */ private void create(int id, String name, String firstName) throws SQLException { Customer c = new Customer(id, name, firstName); c.create(); } /** Database record update */ private void update(int id, String name, String firstName) throws SQLException { Customer c = new Customer(id, name, firstName); c.update(); } /** * Query by example, using a finder method, * which returns only the first matching record */ private void findByID(int id) throws SQLException { Customer c = new Customer(id, null, null); c.find(); System.out.println(c.getId() + ": " + c.getName() + "," + c.getFirstname()); } /** Database record deletion */ private void delete(int id) throws SQLException { Customer c = new Customer(id, null, null); c.delete(); } /** * Query by example, using a query method, * which returns all matching records in a record iterator */ private void queryByName( String name ) throws SQLException { Customer c = new Customer(0, name, null); ResultIterator ri = c.query(new String[] { "name" }); do { System.out.println(c.getId() + ": " + c.getName() + "," + c.getFirstname()); } while(ri.next()); } /** * More complex query, making use of an SQLExpression */ private void queryByRange( int idLow, int idHigh ) throws SQLException { Customer c = new Customer(0, null, null); SQLExpression exp = new SQLExpression(DatabaseFactory.getDatabase()); exp = exp.and("id", GREATEREQUAL, idLow). and("id", LESSEQUAL, idHigh); ResultIterator ri = c.query(exp.toString()); do { System.out.println(c.getId() + ": " + c.getName() + "," + c.getFirstname()); } while(ri.next()); } /** Command dispatcher function */ protected void executeCmd( String[] cmd ) { try { if ( cmd.length < 1 ) { usage(); return; } if ( cmd[0].equals( "exit" ) || cmd[0].equals( "quit" )) { System.exit( 0 ); } else if ( cmd[0].equals( "create" ) ) { if (cmd.length < 4 ) { usage(); return; } create( Integer.parseInt(cmd[1]), cmd[2], cmd[3] ); } else if ( cmd[0].equals( "update" ) ) { if (cmd.length < 4 ) { usage(); return; } update( Integer.parseInt(cmd[1]), cmd[2], cmd[3] ); } else if ( cmd[0].equals( "findbyid" ) ) { if (cmd.length < 2 ) { usage(); return; } findByID( Integer.parseInt(cmd[1]) ); } else if ( cmd[0].equals( "delete" ) ) { if (cmd.length < 2 ) { usage(); return; } delete( Integer.parseInt(cmd[1]) ); } else if ( cmd[0].equals( "querybyname" ) ) { if (cmd.length < 2 ) { usage(); return; } queryByName( cmd[1] ); } else if ( cmd[0].equals( "querybyrange" ) ) { if (cmd.length < 3 ) { usage(); return; } queryByRange( Integer.parseInt(cmd[1]), Integer.parseInt(cmd[2]) ); } else usage(); } catch(Exception x) { x.printStackTrace(); } } public static void main(String[] arg) { try { Properties props = System.getProperties(); // Check the configuration properties if (props.getProperty(ResourceAccessor.Config.DRIVER) == null) throw new Exception("Driver class must be specified by -Dpride.driver="); if (props.getProperty("pride.db") == null) throw new Exception("Database url must be specified by -Dpride.db="); if (props.getProperty(ResourceAccessor.Config.USER) == null) System.out.println("Warning: no user name specified by -Dpride.user"); if (props.getProperty(ResourceAccessor.Config.PASSWORD) == null) System.out.println("Warning: no password specified by -Dpride.password"); if (props.getProperty(ResourceAccessor.Config.DBTYPE) == null) System.out.println("Warning: no database type specified by -Dpride.dbtype"); if (props.getProperty(ResourceAccessor.Config.LOGFILE) == null) props.setProperty(ResourceAccessor.Config.LOGFILE, "sql.log"); // PriDE initialization ResourceAccessorJ2SE accessor = new ResourceAccessorJ2SE(props); DatabaseFactory.setResourceAccessor(accessor); DatabaseFactory.setDatabaseName(props.getProperty("pride.db")); CustomerClient cc = new CustomerClient("customer>"); cc.work(); } catch ( Exception ex ) { System.out.println(ex.getMessage()); } } private void usage() { System.out.println ("------------------------------------------------------------------------\n" + "Usage: \n" + "[exit] shutdown the console\n" + "[create ID name firstname] create a customer\n" + "[update ID name firstname] update a customer\n" + "[findbyid ID] find a customer by its ID\n" + "[querybyname name] find customers by their name\n" + "[querybyrange ID ID] find all customers in the given ID range\n" + "[delete ID] delete a customer\n" + "------------------------------------------------------------------------\n" ); } }