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());
} });