1: /* 2: * This is a demonstration JDBC applet connecting to a selected ODBC 3: * data source via the Easysoft JDBC-ODBC Bridge and displaying the 4: * result of a query. 5: * 6: * The method doTask() can be overwritten in subclasses to cary out 7: * other JDBC tasks given an open connection to the selected database. 8: * 9: * A APPLET tag shown below can be used to launch the applet 10: * 11: * <APPLET CODE = "OutputApplet" ARCHIVE ="../jars/EJOB.jar,../jars/qdemo.jar"> 12: * <PARAM NAME = "jdbcUrl" VALUE = "jdbc:easysoft:" /> 13: * <PARAM NAME = "query" VALUE = "tables" /> 14: * </APPLET> 15: * 16: */ 17: 18: import javax.swing.JApplet; 19: import javax.swing.JButton; 20: 21: import java.sql.*; 22: import java.net.*; 23: import java.util.*; 24: import javax.swing.*; 25: import java.awt.*; 26: import java.awt.event.*; 27: 28: public class QueryApplet extends JApplet implements Runnable 29: { 30: private Thread worker; 31: private JTextArea textA=null; 32: private JTextArea textR=null; 33: private String message = "Initializing"; 34: private JTextArea displayText; 35: private JLabel urlLabel; 36: private JTextField urlText; 37: private JLabel userLabel; 38: private JTextField userText; 39: private JLabel pwdLabel; 40: private JPasswordField pwdText; 41: private JButton connectButton; 42: private JFrame frame; 43: private String jdbcUrl = ""; 44: private JScrollPane scrollableTextA; 45: private JScrollPane scrollableTextR; 46: 47: /* 48: * JDBC url to use to connect to. This can be overridden in the 49: * applet parameter: jdbcUrl 50: */ 51: 52: public String url = "jdbc:easysoft:"; 53: 54: /* 55: * query to run: 56: * JOB runs DatabaseMetaData.getTables() when query="tables" 57: * This can be overridden in the applet parameter: query 58: */ 59: 60: public String query = "tables"; 61: 62: public synchronized void start() 63: { 64: // Every time "start" is called we create a worker thread to 65: // re-evaluate the database query. 66: if (worker == null) 67: { 68: message = "Connecting to database"; 69: worker = new Thread(this); 70: worker.start(); 71: } 72: } 73: 74: public void init () 75: { 76: // Get query parameter 77: String queryParameter = ""; 78: try 79: { 80: queryParameter = getParameter("query"); 81: } 82: catch (Exception e) 83: { // quietly ignore this if not running in browser 84: } 85: 86: if (queryParameter!=null && !queryParameter.equals("")) 87: { 88: query=queryParameter; 89: } 90: 91: // Getting the parameter for jdbcUrl from the calling html file 92: // This enables retargeting of the applet 93: 94: try 95: { 96: jdbcUrl = getParameter("jdbcUrl"); 97: } 98: catch (Exception e) 99: { // quetly ignore this if not running in browser 100: } 101: 102: if (jdbcUrl!=null && !jdbcUrl.equals("")) 103: { 104: url = jdbcUrl; 105: } 106: 107: JPanel app = new JPanel(); // holds the prompt labels 108: app.setLayout(new BorderLayout(5,5)); // overall layout is a NSEWC one 109: 110: if (textA==null) 111: { 112: textA = new JTextArea(message); 113: textA.setRows( 2 ); 114: textA.setEditable( false ); 115: scrollableTextA = new JScrollPane( textA ); 116: app.add ( scrollableTextA, BorderLayout.NORTH ); 117: } 118: 119: if (textR==null) 120: { 121: textR = new JTextArea(""); 122: textR.setEditable( false ); 123: scrollableTextR = new JScrollPane( textR ); 124: app.add ( scrollableTextR, BorderLayout.CENTER ); 125: } 126: getContentPane().add(app); 127: } 128: 129: /* 130: * The "run" method is called from the worker thread. Notice that 131: * because this method is doing potentially slow databases accesses 132: * we avoid making it a synchronized method. 133: */ 134: 135: public void run() 136: { 137: textA.setText( "Getting logon information" ); 138: 139: try 140: { 141: displayText = new JTextArea(6,40); 142: 143: frame = new JFrame( "Result Set" ); 144: frame.setSize( 450, 125 ); 145: 146: // create the components for the applet 147: urlLabel = new JLabel ("URL:"); 148: 149: urlText = new JTextField(jdbcUrl); 150: urlText.setFont(new java.awt.Font("dialog", 1, 12)); 151: urlText.setEditable( false ); 152: 153: userLabel = new JLabel("Logon User:"); 154: 155: userText = new JTextField( "" ); 156: userText.setFont(new java.awt.Font("dialog", 1, 12)); 157: 158: pwdLabel = new JLabel("Login PWD:"); 159: 160: pwdText = new JPasswordField( "" ); 161: pwdText.setFont(new java.awt.Font("dialog", 1, 12)); 162: 163: connectButton = new JButton("Connect"); 164: 165: connectButton.addActionListener(new ActionListener() { 166: public void actionPerformed(ActionEvent e) 167: { 168: buttonConnect(); 169: }}); 170: 171: // overall layout is 3 panels - top, center and bottom 172: 173: JPanel center = new JPanel(); // center part of applet 174: 175: getContentPane().setLayout(new BorderLayout(5,5)); // overall layout is a NSEWC one 176: 177: // set the top panel - consists of three more horizontally arranged panels 178: 179: center.setLayout(new BorderLayout(5, 5)); // give it distinct center 180: 181: JPanel topLeft = new JPanel(); // holds the prompt labels 182: JPanel topCenter = new JPanel(); // holds the text fields 183: JPanel topRight = new JPanel(); // holds the buttons 184: 185: center.add ( topLeft, BorderLayout.WEST ); 186: center.add ( topCenter, BorderLayout.CENTER ); 187: center.add ( topRight, BorderLayout.EAST ); 188: 189: // Each of the top panels has a 3 rows x 1 col grid (2 for right panel) 190: 191: topLeft.setLayout(new GridLayout (3,1,5,5)); 192: topCenter.setLayout(new GridLayout (3,1,5,5)); 193: topRight.setLayout(new GridLayout (3,1,5,5)); 194: 195: // finally - add all the top components 196: topLeft.add(urlLabel); 197: topLeft.add(userLabel); 198: topLeft.add(pwdLabel); 199: 200: topCenter.add(urlText); 201: topCenter.add(userText); 202: topCenter.add(pwdText); 203: 204: topRight.add(connectButton); 205: 206: frame.getContentPane().add( center ); 207: frame.setVisible( true ); 208: } 209: catch (Exception ex) 210: { 211: ex.printStackTrace(); 212: } 213: } 214: 215: public Connection getJOBConnection() throws SQLException 216: { 217: // Load the driver 218: 219: try 220: { 221: Class.forName("easysoft.sql.jobDriver").newInstance(); 222: } 223: catch(Exception ex) 224: { 225: throw new SQLException("Can't find Database driver class: " + ex); 226: } 227: 228: // Now establish connection with via the Driver manager 229: java.util.Properties p = new java.util.Properties(); 230: 231: String u = userText.getText(); 232: if ( u != null ) 233: { 234: p.put( "logonuser", u ); 235: } 236: 237: String s = new String( pwdText.getPassword()); 238: if ( s != null ) 239: { 240: p.put( "logonpassword", s ); 241: } 242: 243: textA.setText( "Connecting" ); 244: 245: Connection con = DriverManager.getConnection(url, p); 246: return con; 247: } 248: 249: /* 250: * In subclasses this method can be overwritten to execute different 251: * db tasks then this given an open connection to the JDBC data source 252: * specified in the applet tag 253: */ 254: 255: public void doTask(Connection con) throws SQLException 256: { 257: // Create a statement Object 258: Statement stmt = con.createStatement(); 259: // Execute a query returning a result set 260: ResultSet rs = stmt.executeQuery(query); 261: // Get the metadata for the result set 262: ResultSetMetaData rsmd = rs.getMetaData(); 263: // Find out the number of columns in the resultset 264: int columnCount = rsmd.getColumnCount(); 265: //StringBuffer data = new StringBuffer(); 266: String data = ""; 267: String rowData =""; 268: // While there are rows in the result set 269: while (rs.next()) 270: { 271: rowData = ""; 272: // get the data for each column in a row and 273: // construct a string of comma separated column values 274: for (int i = 1; i<= columnCount; i++) 275: { 276: rowData = rowData + rs.getString(i) + ", "; 277: } 278: data = data + rowData + "\n"; 279: } 280: 281: // Update result set text area 282: textR.setText(data); 283: // Close result set to release database resources 284: rs.close(); 285: // Close Statement to release database resources 286: stmt.close(); 287: //Close Connection to release database resources 288: con.close(); 289: textA.setText("Run complete"); 290: } 291: 292: /* 293: * This private method is used to record an error message for 294: * later display. 295: */ 296: 297: synchronized void setError(String mess) 298: { 299: textA.setText(mess); 300: worker = null; 301: // And ask AWT to repaint this applet. 302: repaint(); 303: } 304: 305: private void buttonConnect() 306: { 307: frame.setVisible( false ); 308: try 309: { 310: Connection con = getJOBConnection(); 311: doTask(con); 312: } 313: catch(Exception ex) 314: { 315: setError( ex.getMessage()); 316: } 317: } 318: }