本帖最后由 gan_yufei 于 2011-09-20 20:45:43 编辑

解决方案 »

  1.   

    《疯狂Java实战演义》这本书上有
    下载地址:http://download.csdn.net/detail/a627910865/3473542
      

  2.   

    这个按正常思路就可以了,没什么特别的
    首先,通过
    show databases   
    获得所有的数据库,保存在数据库名下拉框中在选种数据库下拉框事件中,通过
    use 选种的数据库名 
    show tables
    或者直接 show tables from 选种的数据库名
    获得该数据库的所有表保存在表明下拉框中在表明下来框选择事件中,通过 
    desc 选种的表名 
    或者直接 show fields from 选种的表名
    获取表的结构信息显示在JTable中在输入框输入SQL在执行操作按钮事件中,执行输入框输入的SQL,把结果显示在下面的TextArea
     
      

  3.   

    不知道你学没有学习j2ee,如果学过这个理解起来应该不难的!就是写一些sql语句,或者事务等。
    主要还是select语句。
      

  4.   

    java.sql.Connection
    java.sql.Statement
    java.sql.ResultSet
    java.sql.DatabaseMetaDatalz掌握这几个类就可以解决问题了。红色部分可以获得数据库名,表名,字段名。
      

  5.   

    刷新数据 按钮我觉得就没必要了吧。部分代码,仅供参考      String catalog = con.getCatalog();
          DatabaseMetaData dmd = con.getMetaData();
          ResultSet userTables = 
             dmd.getTables(catalog, null, null, new String[] {"TABLE"});
          List<Table> list = new ArrayList<Table>();      try {
             while(userTables.next()) {
                String tableName = userTables.getString(Table.TABLE_NAME);
                Table table = new Table();
                table.setTableName(tableName);            if(!table.isBackupTable()) {
                   continue;
                }            table.setCatalog(catalog);
                table.setSchema(userTables.getString(Table.SCHEMA));
                ResultSet primaryKeys = dmd.getPrimaryKeys(
                    catalog, table.getSchema(), tableName);
                ResultSet tableColumns = dmd.getColumns(
                    catalog, table.getSchema(), tableName, null);
                String sql = "SELECT * FROM " + tableName;
      

  6.   

    多谢阿宝 和 softroad及各位朋友,花了一点时间只做出了textArea的输出效果, 关于JTable的关联, 我还得琢磨琢磨, 下面是效果图:
      

  7.   

    楼主要用数据库元数据,就是楼上的说的DatabaseMetaData,这个可以获得数据库的缘数据信息,比如说表、字段、主键等等,这些方法可以在API找到,不知道现在说楼主还能不能给点分,谢谢了啊,呵呵!!!
      

  8.   

    还是不怎么清楚用JTable关联数据库, 已知的数据库我还清楚,用String数组设置表头名,
    不怎么会用vector添加
      

  9.   

    DefaultTableModel这个可以动态修改表结构和数据。JTable table = new JTable();table.getModel();好像是这么个方法,没查api,拿到这个以后就可以进行表操作了
      

  10.   

    建于ResultSet上的tablemodel
    楼主可以修改 isCellEditable
    并在数据修改的时候及时修改resultset,可以实现数据同步
    import java.sql.*;
    import javax.swing.table.*;
    import javax.swing.event.*;/**
     * This class takes a JDBC ResultSet object and implements the TableModel
     * interface in terms of it so that a Swing JTable component can display the
     * contents of the ResultSet.  Note that it requires a scrollable JDBC 2.0 
     * ResultSet.  Also note that it provides read-only access to the results
     **/
    public class ResultSetTableModel implements TableModel {
        ResultSet results;             // The ResultSet to interpret
        ResultSetMetaData metadata;    // Additional information about the results
        int numcols, numrows;          // How many rows and columns in the table    /**
         * This constructor creates a TableModel from a ResultSet.  It is package
         * private because it is only intended to be used by 
         * ResultSetTableModelFactory, which is what you should use to obtain a
         * ResultSetTableModel
         **/
        ResultSetTableModel(ResultSet results) throws SQLException {
    this.results = results;                 // Save the results
    metadata = results.getMetaData();       // Get metadata on them
    numcols = metadata.getColumnCount();    // How many columns?
    results.last();                         // Move to last row
    numrows = results.getRow();             // How many rows?
        }
        
        /** 
         * Call this when done with the table model.  It closes the ResultSet and
         * the Statement object used to create it.
         **/
        public void close() {
    try { results.getStatement().close(); }
    catch(SQLException e) {};
        }    /** Automatically close when we're garbage collected */
        protected void finalize() { close(); }    // These two TableModel methods return the size of the table
        public int getColumnCount() { return numcols; }
        public int getRowCount() { return numrows; }    // This TableModel method returns columns names from the ResultSetMetaData
        public String getColumnName(int column) {
    try {
        return metadata.getColumnLabel(column+1);
    } catch (SQLException e) { return e.toString(); }
        }    // This TableModel method specifies the data type for each column.  
        // We could map SQL types to Java types, but for this example, we'll just
        // convert all the returned data to strings.
        public Class getColumnClass(int column) { return String.class; }
        
        /**
         * This is the key method of TableModel: it returns the value at each cell
         * of the table.  We use strings in this case.  If anything goes wrong, we
         * return the exception as a string, so it will be displayed in the table.
         * Note that SQL row and column numbers start at 1, but TableModel column
         * numbers start at 0.
         **/
        public Object getValueAt(int row, int column) {
    try {
        results.absolute(row+1);                // Go to the specified row
        Object o = results.getObject(column+1); // Get value of the column
        if (o == null) return null;       
        else return o.toString();               // Convert it to a string
    } catch (SQLException e) { return e.toString(); }
        }    // Our table isn't editable
        public boolean isCellEditable(int row, int column) { return false; }     // Since its not editable, we don't need to implement these methods
        public void setValueAt(Object value, int row, int column) {}
        public void addTableModelListener(TableModelListener l) {}
        public void removeTableModelListener(TableModelListener l) {}
    }
      

  11.   

    捣鼓半天成这样了, swing真的很头疼啊