DB2 html java jdbc example: html <code>
APPLET CODE=JdbcTesta.class WIDTH=475 HEIGHT=400> /APPLET
</code>
Applet <code>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class JdbcTesta extends Applet {
Label lb1 = new Label( "Userid" );
TextField tf1 = new TextField(15);
Label lb2 = new Label( "Password" );
TextField tf2 = new TextField(15);
Button bt1 = new Button("Execute Sql");
Button bt2 = new Button("Reset Sql");
Button bt3 = new Button("Reset Result Set");
Button bt4 = new Button("Reset All");
Label lb3 = new Label( "Sql Code" );
String sql = "select col_name " +
"from dbname.tbname " +
"where partition_nbr = '09' " +
"and col_name = 'ED5197782';";
TextArea ta1 = new TextArea( sql, 05, 40, TextArea.SCROLLBARS_BOTH );
Label lb4 = new Label( "Result Set" );
TextArea ta2 = new TextArea( "", 05, 40, TextArea.SCROLLBARS_BOTH );
public void init() {
Panel p0 = new Panel();
p0.setLayout( new BorderLayout() );
Panel p1 = new Panel();
p1.setLayout( new GridLayout( 2, 4) ) ;
p1.add( lb1 );
p1.add( tf1 );
p1.add( lb2 );
p1.add( tf2 ); tf2.setEchoChar('*');
bt1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean editTF;
editTF = false;
editTF = editTFInput();
if ( editTF ) {
executeSql();
} else {
showStatus("Invalid input - Try Again ...");
}// end of if
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt1 );
bt2.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta1.setText("");
showStatus("Reset Sql ...");
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt2 );
bt3.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta2.setText("");
showStatus("Reset Result Set ...");
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt3 );
bt4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta1.setText("");
ta2.setText("");
showStatus("Reset All ...");
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt4 );
p0.add( p1, BorderLayout.NORTH );
Panel p2 = new Panel();
p2.setLayout( new GridLayout( 4, 4 ) ) ;
p2.add( lb3 );
p2.add( ta1 );
p2.add( lb4 );
p2.add( ta2 );
p0.add ( p2, BorderLayout.CENTER );
this.add( p0 );
} // end of init
public boolean editTFInput() {
boolean editTF;
editTF = true;
if ( tf1.getText().length() != 7 ) { editTF = false; }
if ( tf2.getText().length() < 6 | | tf2.getText().length() > 8 ) {
editTF = false; }
return editTF;
}// end if method editTFInput
public void executeSql() {
try {
String userid, password;
Vector queryResults = new Vector();
int accum1 = 0;
int accum2 = 0;
showStatus("Loading JDBC Driver ..." );
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
showStatus("Connecting To jdbc:db2

SNP ..." );
userid = tf1.getText().trim().toUpperCase();
password = tf2.getText().trim().toUpperCase();
Connection con =
DriverManager.getConnection("jdbc:db2

SNT", userid, password );
showStatus("Creating SQL Statement ..." );
String sqlsel1 = ta1.getText();
showStatus("Creating Java Statement Class ..." );
Statement stmt = con.createStatement();
showStatus("Executing SQL VIA JAVA Statement ..." );
ResultSet rs = stmt.executeQuery( sqlsel1 );
showStatus("Processing Result Set VIA JAVA Statement ..." );
// result set meta data
ResultSetMetaData meta = rs.getMetaData();
// add java result set to a vector
while ( rs.next() ) {
StringBuffer strbuf = new StringBuffer();
String col1 = rs.getString( "col_name" );
strbuf.append( col1 ) ;
int col2 = 1;
accum1 = accum1 + col2;
accum2 = accum2 + col2;
String rows = strbuf.toString();
queryResults.addElement( rows );
strbuf.setLength( 0 );
// if ( accum1 >= 0 ) {
// strbuf.append( col1 + " " + col2 + " " + accum1 + " " + accum2 +"\n" );
// String rows = strbuf.toString();
// strbuf.setLength( 0 );
// queryResults.addElement( rows );
// accum1 = 0;
// } // end of if
} // end of while
// add vector to a text area component
Enumeration enum = queryResults.elements();
while (enum.hasMoreElements()) {
String rows = (String)enum.nextElement();
ta2.append( rows );
} // end of while
showStatus("Processing Completed ..." );
queryResults.removeAllElements();// empty vector
rs.close();// close ResultSet
stmt.close();// close Statement
con.close();// close Connection
}// end try block
catch(ClassNotFoundException e) {
showStatus("ClassNotFoundException!!!: " );
ta2.append("ClassNotFoundException!!!: " + e ) ;
}
catch(SQLWarning e) {
showStatus("SQL_CODE !!!: " );
ta2.append("SQL_CODE !!!: " + e ) ;
}
catch(SQLException e) {
showStatus("SQL_CLASS !!: " );
ta2.append("SQL_CLASS !!: " + e ) ;
}// end of catch block(s)
} // end of executeSql
}// end of class JdbcTesta
</code>
Hope this helps,
Monty6
[This message has been edited by monty6 (edited September 06, 2000).]
[This message has been edited by monty6 (edited September 06, 2000).]