* Created by: Gyuri Lajos @ Easysoft
*
* This is a demonstration JDBC applet connecting to a selected ODBC
* data source via the Easysoft JDBC-ODBC Bridge and displaying the
* result of a query.
*
* The method doTask() can be overwritten in subclasses to cary out
* other JDBC tasks given an open connection to the selected database.
*
*
* A APPLET tag shown below can be used to launch the applet
*
*
* <APPLET CODE = "OutputApplet"
* ARCHIVE = "../jars/EJOB.jar"
* WIDTH = "440"
* HEIGHT = "300">
* <PARAM NAME = "jdbcUrl" VALUE = "jdbc:easysoft:"></PARAM>
* <PARAM NAME = "query" VALUE = "select * from coffees"></PARAM>
* </APPLET>
*
*/
import java.applet.Applet;
import java.sql.*;
public class QueryApplet extends Applet implements Runnable {
private Thread worker;
private java.awt.TextArea textA=null;
private java.awt.TextArea textR=null;
private String message = "Initializing";
/*
* JDBC url to use to connect to. This can be overridden in the
* applet parameter: jdbcUrl
*/
public String url = "jdbc:easysoft:";
/*
* query to run: JOB runs DatabaseMetaData.getTables() when query="tables"
* This can be overridden in the applet parameter: query
*/
public String query = "tables";
public synchronized void start() {
// Every time "start" is called we create a worker thread to
// re-evaluate the database query.
if (worker == null) {
message = "Connecting to database";
worker = new Thread(this);
worker.start();
}
}
public void init () {
// Get query parameter
String queryParameter = "";
try {
queryParameter = getParameter("query");
}
catch (Exception e)
{ // quietly ignore this if not running in browser
}
if (queryParameter!=null && !queryParameter.equals(""))
query=queryParameter;
// Getting the parameter for jdbcUrl from the calling html file
// This enables retargeting of the applet
String jdbcUrl = "";
try {
jdbcUrl = getParameter("jdbcUrl");
}
catch (Exception e)
{ // quetly ignore this if not running in browser
}
if (jdbcUrl!=null && !jdbcUrl.equals("")) url = jdbcUrl;
if (textA==null) {
textA = new java.awt.TextArea(message,2,50);
add(textA);
}
if (textR==null) {
textR = new java.awt.TextArea(message,12,50);
add(textR);
}
}
/**
* The "run" method is called from the worker thread. Notice that
* because this method is doing potentially slow databases accesses
* we avoid making it a synchronized method.
*/
public void run() {
try {
Connection con = getJOBConnection();
doTask(con);
} catch(Exception ex) {
setError( ex.getMessage());
}
}
public Connection getJOBConnection() throws SQLException {
// Load the driver
try {
Class.forName("easysoft.sql.jobDriver").newInstance();
} catch(Exception ex) {
throw new SQLException("Can't find Database driver class: " + ex);
}
// Now establish connection with via the Driver manager
Connection con = DriverManager.getConnection(url);
return con;
}
/**
* In subclasses this method can be overwritten to execute different
* db tasks then this given an open connection to the JDBC data source
* specified in the applet tag
*/
public void doTask(Connection con) throws SQLException {
// Create a statement Object
Statement stmt = con.createStatement();
// Execute a query returning a result set
ResultSet rs = stmt.executeQuery(query);
// Get the metadata for the result set
ResultSetMetaData rsmd = rs.getMetaData();
// Find out the number of columns in the resultset
int columnCount = rsmd.getColumnCount();
//StringBuffer data = new StringBuffer();
String data = "";
String rowData = "";
// While there are rows in the result set
while (rs.next()) {
rowData = "";
// get the data for each column in a row and
// construct a string of comma separated column values
for (int i = 1; i<= columnCount; i++) {
rowData = rowData + rs.getString(i) + ", ";
}
data = data + rowData + "\n";
}
// Update result set text area
textR.setText(data);
// Close result set to release database resources
rs.close();
// Close Statement to release database resources
stmt.close();
//Close Connection to release database resources
con.close();
textA.setText("Run complete");
}
/**
* This private method is used to record an error message for
* later display.
*/
synchronized void setError(String mess) {
textA.setText(mess);
worker = null;
// And ask AWT to repaint this applet.
repaint();
}
}
|