HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Browsing the JDBC API"
Vol. 3, Issue 4, p. 44

	

Listing 1: Connect to Database using Protocol Operands
  
// Connect to database  
public void connectDB ( String []  operand )   
                   throws Exception {  
    if (operand[DBP_DRIVER_ARG] != null ) {  
      class.forName( operand[DBP_DRIVER_ARG]);  
    }  
    if (operand[DBP_USERID_ARG] ==  null) {  
       session =    
       DriverManager.getConnection(  
         operand[DBP_URL_ARG]);  
    }  
    else {  
        session =   
            DriverManager.getConnection(  
      operand[DBP_URL_ARG],  
      operand[DBP_USERID_ARG],  
      operand[DBP_PASSWORD_ARG]  );   
    }  
    query =  session.createStatement();  
}  

Listing 2: The User Interface is notified of Observable change with a ProtocolData Object.
  
class DataBrowser    
         extends Frame   
         implements DBProtocol,  
                            DBConstants,  
                            DBHandle,  
                            ItemListener,  
                            Observer {  
  

        public void update(  Observable o,   
                                         Object p) {  
               if !(p instanceof ProtocolData)){  
                    System.out.println("Error in                
                                   Observers.");  
                    System.exit(1);  
               }  
              // do something as directed by   
              // observable object with detail   
              // in the protocol, e.g. requery  
      }}  

Listing 3: The Protocol Client creates a Protocol Instruction.
  
public static void  tellProtocolClient( byte opcode )   
                                   throws Exception {  
    ProtocolData protocolData =     
            ProtocolData.getProtocolData();  
    protocolData.setOpcode(opcode);  
    try {  
        switch (opcode) {  
            case DBP_EXECUTE_QUERY: {   
                   // the TextArea is the operand value  
                  protocolData.setOperand(  
                              ui.queryWindow.getText(),  
                              DBP_SQLTEXT_ARG);  
                  protocolData.setOperand(  
                             ui.rowCount.getText(),  
                             DBP_ROWCOUNT_ARG);  
                  ProtocolServer.serviceDBRequest(  
                              protocolData);  
                  break;  
 }  
  
}  

Listing 4: Protocol Server Instruction Handling.
  
// protocol handler  
public static Object serviceDBRequest( ProtocolData p )  
                                throws Exception {  
    switch (p.getOpcode()) {  
        case DBP_EXECUTE_QUERY: {      
            db.execSQL(  
                 p.getOperand(DBP_SQLTEXT_ARG),     
                 p.getOperand(DBP_ROWCOUNT_ARG));  
 return db.getResults();  
        }  
  
}    

Listing 5: ProtocolData Class Operand Capture
  
public void setOpcode( int opcode ) {  
    this.opcode = opcode;  
}  
public void setOperand( String operand, int index  ) {  
    this.operand[index] = operand;  
}  

Listing 6: Anonymous Class Example One
  
tableList.addItemListener(   
    new ItemListener() {  
        public void itemStateChanged(     
                              ItemEvent e ) {  
            Integer index =  (Integer)e.getItem();  
            tableName = tableList.getItem(index.intValue());  
            try {  
                ProtocolClient.tellProtocolClient(   
                                         DBP_COLUMNS );  
            } catch (Exception ea) {  
                 ea.printStackTrace();  
            }  
       } });  

Listing 7: Anonymous Class Example Two.
  
queryButton.addActionListener(   
        new ActionListener() {  
            public void actionPerformed(   
                               ActionEvent e ){  
                tabOptions.show(   
                          panel4,  
                          queryButton.getLabel());  
          } });
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.