//======================================================
class Database{
    Connection con;
    resultSet results;
    ResultSetMetaData rsmd;
    DatabaseMetaData dma;
    String catalog;
    String types[];   public Database(String driver){
       types = new String[1];
       types[0] = "TABLE";
       try{
            Class.forName(driver);
       }catch (Exception e){
          System.out.println("driver load failed:"+ e.getMessage());
       }
   }
   //-----------------------------------
   public void Open(String url, String cat){
      catalog = cat;
      try{
          con   = DriverManager.getConnection(url);
          dma =con.getMetaData();
          results = new resultSet(dma.getCatalogs());
          String s[];
         while (results.hasMoreElements()){
             s = results.nextElement();
         }
      }catch (Exception e){
         System.out.println("open failed: "+ e.getMessage());
      }
   }
   //-----------------------------------
   public String[] getTableNames(){
        String[] tbnames = null;
        Vector tname = new Vector();        try{
             results = new resultSet(dma.getTables(null, null, null, types));            while (results.hasMoreElements())
                 tname.addElement(results.getColumnValue("TABLE_NAME"));         }catch (Exception e) {
             System.out.println(e);
        }
        tbnames = new String[tname.size()];
       for (int i=0; i< tname.size(); i++)
           tbnames[i] = (String)tname.elementAt(i);
      return tbnames;
   }
   // 返回表的元数据
   public String[] getTableMetaData()
   {
      results = null;
      try{
      results = new resultSet(dma.getTables(null, null, null, types));
      }
      catch (Exception e)
      {System.out.println(e.getMessage());}
      return results.getMetaData();
   }
   //返回一个字段的元数据
   public String[] getColumnMetaData(String tablename){
       results = null;
       try {
            results = new resultSet(dma.getColumns(catalog, null, tablename, null));
       }catch (Exception e){
            System.out.println(e.getMessage());}
       return results.getMetaData();
   }
   //返回一个字段名数组
   public String[] getColumnNames(String table){
      String[] tbnames = null;
      Vector tname = new Vector();      try {
            results = new resultSet(dma.getColumns(catalog, null, table, null));
           while (results.hasMoreElements() )
                 tname.addElement(results.getColumnValue("COLUMN_NAME"));
      }catch (Exception e) {
            System.out.println(e);
      }      tbnames = new String[tname.size()];
      for (int i=0; i< tname.size(); i++)
           tbnames[i] = (String)tname.elementAt(i);
      return tbnames;
   }
   //返回给定字段的值
   public String getColumnValue(String table, String columnName){
      String res = null;
      try{
          if (table.length()>0)
              results = Execute("Select distinct "+columnName+" from "+table+" order by "+columnName);
           res = results.getColumnValue(columnName);
      } catch (Exception e){
           System.out.println("Column value error" +columnName+ e.getMessage());
      }      return res;
   }   public String getNextValue(String columnName){
       String res = "";
       try{
            // 使用存储的 resultSet
            //返回该列的下一个值
           if (results.hasMoreElements())
            res = results.getColumnValue(columnName);
        } catch (Exception e){
            System.out.println("next value error"+columnName+ e.getMessage());
        }        return res;
   }
   //对此数据库执行一个 SQL 查询
   public resultSet Execute(String sql){
       results = null;
      try{
            Statement stmt = con.createStatement();
            results = new resultSet(stmt.executeQuery(sql));
       }catch (Exception e){
            System.out.println(e.getMessage());
       }
       return results;
   }
}
//==========================================
class resultSet{
   ResultSet rs;
   ResultSetMetaData rsmd;
   int numCols;   public resultSet(ResultSet rset){
      rs = rset;
      try{
           rsmd = rs.getMetaData();
           numCols = rsmd.getColumnCount();
      }catch (Exception e){
           System.out.println(e.getMessage());
      }
   }
   //-----------------------------------
   public String[] getMetaData(){
      String md[] = new String[numCols];
      try{
           for (int i=1; i<= numCols; i++)
              md[i-1] = rsmd.getColumnLabel(i);
      }catch (Exception e){
           System.out.println(e.getMessage());
      }
      return md;
   }
   //-----------------------------------
   public boolean hasMoreElements(){
      try{
          return rs.next();
      }catch(Exception e){
          return false;
      }
   }
   //-----------------------------------
   public String[] nextElement(){
      String[] row = new String[numCols];
      try{
          for (int i = 1; i <= numCols; i++)
              row[i-1] = rs.getString(i);
      }catch (Exception e){
          System.out.println(e.getMessage());
      }
     return row;
   }
   //-------------------------------------
   public String getColumnValue(String columnName){
      String res = "";
      try{
           res = rs.getString(columnName);
      }catch (Exception e){
           System.out.println(e.getMessage());
      }
      return res;
   }
   //----------------------------------------------
   public void finalize(){
      try{
           rs.close();}
      catch(Exception e){
           System.out.println(e.getMessage());
      }
   }
}

解决方案 »

  1.   

    import java.net.URL;
    import java.sql.*;
    import java.util.Vector;
    import java.awt.*;
    import java.awt.event.*;public class dbFrame extends Frame {
        Choice Tables, Columns;
        List Data;
        Button Search, Quit;
        public dbFrame(){
          super("数据库浏览");
          setGUI();
        }
       //------------------------------------
       private void setGUI(){
          setBackground(Color.lightGray);
          setLayout(new BorderLayout());
          Panel pn = new Panel();
          add("North",pn);
          pn.setLayout(new GridLayout(2,2));
          pn.add(new Label("Tables"));
          pn.add(new Label("Columns"));
          pn.add(Tables = new Choice());
          pn.add(Columns = new Choice());
          Panel pc = new Panel();
          add(pc);
          pc.setLayout(new BorderLayout());
          pc.add("North",new Label("Data"));
          pc.add("Center",Data = new List(15));
          Panel pQuit=new Panel();
          pQuit.add(Quit = new Button("Quit"));
          pc.add("South",pQuit);
          setBounds(100, 100, 500, 300);
          setVisible(true);
       }
       static public void main(String argv[]){
          new dbFrame();
       }
    }