home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / Select.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  6.3 KB  |  291 lines

  1.  
  2. package sunw.demo.select;
  3.  
  4. /**
  5.  * This class is a simple demo database viewer.
  6.  * Given a URL it will go talk to a database and fill all its tables.
  7.  * The user can then chose a table and the tool can either display metadata
  8.  * informtion about the table or display all its rows.
  9.  *
  10.  * @author kgh
  11.  */
  12.  
  13. import java.sql.*;
  14. import java.awt.*;
  15. import java.util.*;
  16.  
  17. public class Select extends Panel {
  18.  
  19.     public Select() {
  20.     setLayout(null);
  21.     setSize(200, 300);
  22.  
  23.     initialize();
  24.     }
  25.  
  26.     private void initialize() {
  27.     if (text != null) {
  28.         return;
  29.     }
  30.     removeAll();
  31.  
  32.         text = new java.awt.List();
  33.         text.setFont(new Font("Courier", Font.PLAIN, 12));
  34.     add(text);
  35.  
  36.         status = new Label("", Label.LEFT);
  37.     add(status);
  38.  
  39.     doLayout();
  40.  
  41.     new WorkerThread(this);
  42.     }
  43.  
  44.     // This is called from the worker thread.  This is where we actually
  45.     // do any real work, like talking to the database.
  46.     // Note that we have to be careful about concurrency control here so
  47.     // thav we don't wedge the GU while we're off chatting to the database.
  48.  
  49.     void doWork() {
  50.     for (;;) {
  51.         int work = getWork();    
  52.         if (work == DO_SELECT) {
  53.         select();
  54.         }
  55.         }
  56.     }
  57.  
  58.     synchronized int getWork() {
  59.     work = NO_WORK;
  60.     while (work == NO_WORK) {
  61.         try {
  62.         wait();
  63.         } catch (Exception ex) {
  64.         }
  65.     }
  66.     int result = work;
  67.     return (result);
  68.     }
  69.  
  70.     void select() {
  71.     // System.err.println("Doing select with: " + selectString);
  72.  
  73.     text.removeAll();
  74.  
  75.     Connection con = Util.connect(url, user, password, status);
  76.     try {
  77.         Statement s = con.createStatement();
  78.         ResultSet rs = s.executeQuery(selectString);
  79.  
  80.         ResultSetMetaData md = rs.getMetaData();
  81.         int columns = md.getColumnCount();
  82.         int widths[] = new int[columns+1];
  83.         Vector contents[] = new Vector[columns+1];
  84.         for (int col = 1; col <= columns; col++) {
  85.         widths[col] = md.getColumnName(col).length();
  86.         contents[col] = new Vector();
  87.         }
  88.  
  89.         int rows = 0;
  90.         while (rs.next() && rows < maxRows) {
  91.         for (int col = 1; col <= columns; col++) {
  92.             String field = rs.getString(col);
  93.             if (field == null) {
  94.             field = "";
  95.             }
  96.             if (field.length() > 80) {
  97.             field = field.substring(0,80);
  98.             }
  99.             if (field.length() > widths[col]) {
  100.             widths[col] = field.length();
  101.             }
  102.             contents[col].addElement(field);
  103.         }
  104.         rows++;
  105.         }
  106.  
  107.         // Create the header string.
  108.         String header = "";
  109.         for (int col = 1; col <= columns; col++) {
  110.             String field = md.getColumnName(col);
  111.         int pad = widths[col] - field.length();
  112.         if (col != 1) {
  113.             pad++;
  114.         }
  115.         if (pad > 0) {
  116.             header += spaces.substring(0, pad);
  117.         }
  118.         header += field;
  119.         }
  120.         text.addItem(header);
  121.  
  122.         // Add the indivbidual database rows
  123.         for (int row = 0; row < rows; row++) {
  124.         String line = "";
  125.             for (int col = 1; col <= columns; col++) {
  126.             String field = (String)contents[col].elementAt(row);
  127.             int pad = widths[col] - field.length();
  128.             if (col != 1) {
  129.             pad++;
  130.             }
  131.             if (pad > 0) {
  132.             line += spaces.substring(0, pad);
  133.             }
  134.             line += field;
  135.         }
  136.                text.addItem(line);
  137.         }
  138.  
  139.         if (text.getItemCount() >= maxRows) {
  140.             status.setText("Only read first " + maxRows + " rows");
  141.         } else {
  142.               status.setText("Done.");
  143.         }
  144.  
  145.     } catch (SQLException sx)  {
  146.         System.err.println("Caught " + sx);
  147.         status.setText("Caught " + sx);    
  148.     }
  149.  
  150.     Util.disconnect(con);
  151.  
  152.     doLayout();
  153.  
  154.     }
  155.  
  156.     public synchronized void doLayout() {
  157.     initialize();
  158.     int width = getSize().width;
  159.     int height = getSize().height;
  160.     text.setBounds(0, 0, width, height-40);
  161.     status.setBounds(15, height-30, 2*width, 25);
  162.     }
  163.  
  164.     /**
  165.      * @deprecated provided for backward compatibility with old layout managers.
  166.      */
  167.  
  168.     public void layout() {
  169.     doLayout();
  170.     }
  171.  
  172.     //------------------------------------------------------------
  173.     // property accessors.
  174.  
  175.     public String getURL() {
  176.     return url;
  177.     }
  178.  
  179.     public void setURL(String url) {
  180.     this.url = url;
  181.     }
  182.  
  183.     public String getUser() {
  184.     return user;
  185.     }
  186.  
  187.     public void setUser(String user) {
  188.     this.user = user;
  189.     }
  190.  
  191.     public String getPassword() {
  192.     return password;
  193.     }
  194.  
  195.     public void setPassword(String password) {
  196.     this.password = password;
  197.     }
  198.  
  199.     public String getSQL() {
  200.     return selectString;
  201.     }
  202.  
  203.     public void setSQL(String sql) {
  204.     selectString = sql;
  205.     }
  206.  
  207.     public int getMaxRows() {
  208.     return maxRows;
  209.     }
  210.  
  211.     public void setMaxRows(int maxRows) {
  212.     this.maxRows = maxRows;
  213.     }
  214.  
  215.  
  216.     //------------------------------------------------------------
  217.  
  218.     // Serialization support.
  219.  
  220.     private void writeObject(java.io.ObjectOutputStream s)
  221.                       throws java.io.IOException 
  222.     {
  223.     // Clear the current display so we don't write all
  224.     // the current field values.
  225.     text.removeAll();
  226.         s.defaultWriteObject();
  227.     }
  228.  
  229.     private void readObject(java.io.ObjectInputStream s)
  230.           throws java.lang.ClassNotFoundException,
  231.              java.io.IOException 
  232.     {
  233.         s.defaultReadObject();
  234.     initialize();
  235.     }
  236.  
  237.     //------------------------------------------------------------
  238.  
  239.     // Event handler to re-read database state:
  240.     public void update(java.awt.event.ActionEvent evt) {
  241.     update();
  242.     }
  243.  
  244.     // Public method to re-read database state.
  245.     public synchronized void update() {
  246.     work = DO_SELECT;
  247.     notify();
  248.     }
  249.  
  250.     //------------------------------------------------------------
  251.     // GUI state:
  252.  
  253.     private transient Label status;
  254.     private transient java.awt.List text;
  255.  
  256.     // Keep track of pending work for the worker thread:
  257.     private final static int NO_WORK = 0;
  258.     private final static int DO_SELECT = 3;
  259.     private transient int work = NO_WORK;
  260.  
  261.     private final static String spaces = 
  262.         "                                           " +
  263.         "                                           " +
  264.         "                                           ";
  265.  
  266.     //------------------------------------------------------------
  267.     // Database related state:
  268.     private String url = "jdbc:odbc:SQLSERVER";
  269.     private String user = "guest";
  270.     private String password = "guest";
  271.     private String selectString = "";
  272.     private int maxRows = 200;
  273. }
  274.  
  275.  
  276. // The worker thread just calls into the given Select()
  277.  
  278. class WorkerThread extends Thread {
  279.     WorkerThread(Select tvarg) {
  280.         tv = tvarg;
  281.         this.start();
  282.     }
  283.  
  284.     public void run() {
  285.     tv.doWork();
  286.     }
  287.  
  288.     private Select tv;
  289. }
  290.  
  291.