ASP学习简单,使用方便,部署也比JSP简单
作JSP一般用语商务型,

解决方案 »

  1.   

    你说的tables是什么意思?
    是数据库里面的table?
        try {
            Statement stmt = connection.createStatement();
        
            // Create table called my_table
            String sql = "CREATE TABLE my_table(col_string VARCHAR(254))";
        
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
        }
      

  2.   

    Listing All Table Names in a Database
        try {
            // Gets the database metadata
            DatabaseMetaData dbmd = connection.getMetaData();
        
            // Specify the type of object; in this case we want tables
            String[] types = {"TABLE"};
            ResultSet resultSet = dbmd.getTables(null, null, "%", types);
        
            // Get the table names
            while (resultSet.next()) {
                // Get the table name
                String tableName = resultSet.getString(3);
        
                // Get the table's catalog and schema names (if any)
                String tableCatalog = resultSet.getString(1);
                String tableSchema = resultSet.getString(2);
            }
        } catch (SQLException e) {
        }
      

  3.   

    Creating a Hash Table
    A hash table, or map, holds key/value pairs. 
        // Create a hash table
        Map map = new HashMap();    // hash table
        map = new TreeMap();        // sorted map
        
        // Add key/value pairs to the map
        map.put("a", new Integer(1));
        map.put("b", new Integer(2));
        map.put("c", new Integer(3));
        
        // Get number of entries in map
        int size = map.size();        // 2
        
        // Adding an entry whose key exists in the map causes
        // the new value to replace the old value
        Object oldValue = map.put("a", new Integer(9));  // 1
        
        // Remove an entry from the map and return the value of the removed entry
        oldValue = map.remove("c");  // 3
        
        // Iterate over the keys in the map
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            // Get key
            Object key = it.next();
        }
        
        // Iterate over the values in the map
        it = map.values().iterator();
        while (it.hasNext()) {
            // Get value
            Object value = it.next();
        }
      

  4.   

    Here is the code that implements the table in SimpleTableDemo.java: 
    String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};Object[][] data = {
        {"Mary", "Campione",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"Alison", "Huml",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Kathy", "Walrath",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Sharon", "Zakhour",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Philip", "Milne",
         "Pool", new Integer(10), new Boolean(false)}
    };JTable table = new JTable(data, columnNames);The SimpleTableDemo example uses one of two JTable constructors that directly accept data: 
    JTable(Object[][] rowData, Object[] columnNames) 
    JTable(Vector rowData, Vector columnNames) 
    The advantage of these constructors is that they're easy to use. However, these constructors also have disadvantages: 
    They automatically make every cell editable. 
    They treat all data types the same (as strings). For example, if a table column has Boolean data, the table can display the data in a check box. However, if you use one of the two JTable constructors listed previously, your Boolean data will be displayed as a string. You can see this difference in the last column of the two previous pictures of tables. 
    They require that you put all of the table's data in an array or vector, which isn't appropriate for some data. For example, if you're instantiating a set of objects from a database, you might want to query the objects directly for their values, rather than copying all their values into an array or vector. 
    If you want to get around these restrictions, you need to implement your own table model, as described in Creating a Table Model. 
    Adding a Table to a Container 
    It's easy to put a table in a scroll pane. You need just one or two lines of code: 
    JScrollPane scrollPane = new JScrollPane(table);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));The scroll pane automatically gets the table's header, which displays the column names, and puts it on top of the table. Even when the user scrolls down, the column names remain visible at the top of the viewing area. The scroll pane also tries to make its viewing area the same as the table's preferred viewing size. The previous code snippet sets the table's preferred viewing size with the setPreferredScrollableViewportSize method. 
    If you're using a table without a scroll pane, then you must get the table header component and place it yourself. For example: container.setLayout(new BorderLayout());
    container.add(table.getTableHeader(), BorderLayout.PAGE_START);
    container.add(table, BorderLayout.CENTER);Setting and Changing Column Widths 
    By default, all columns in a table start out with equal width, and the columns automatically fill the entire width of the table. When the table becomes wider or narrower (which might happen when the user resizes the window containing the table), all the column widths change appropriately. 
    When the user resizes a column by dragging its right border, then either other columns must change size, or the table's size must change. By default, the table's size remains the same, and all columns to the right of the drag point resize to accommodate space added to or removed from the column to the left of the drag point. The following figures illustrate the default resizing behavior. [PENDING: These will be updated.] 
      

  5.   

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import java.awt.Dimension;
    import java.awt.GridLayout;/** 
     * TableDemo is just like SimpleTableDemo, except that it
     * uses a custom TableModel.
     */
    public class TableDemo extends JPanel {
        private boolean DEBUG = false;    public TableDemo() {
            super(new GridLayout(1,0));        JTable table = new JTable(new MyTableModel());
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));        //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);        //Add the scroll pane to this panel.
            add(scrollPane);
        }    class MyTableModel extends AbstractTableModel {
            private String[] columnNames = {"First Name",
                                            "Last Name",
                                            "Sport",
                                            "# of Years",
                                            "Vegetarian"};
            private Object[][] data = {
                {"Mary", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"Alison", "Huml",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Kathy", "Walrath",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Sharon", "Zakhour",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Philip", "Milne",
                 "Pool", new Integer(10), new Boolean(false)}
            };        public int getColumnCount() {
                return columnNames.length;
            }        public int getRowCount() {
                return data.length;
            }        public String getColumnName(int col) {
                return columnNames[col];
            }        public Object getValueAt(int row, int col) {
                return data[row][col];
            }        /*
             * JTable uses this method to determine the default renderer/
             * editor for each cell.  If we didn't implement this method,
             * then the last column would contain text ("true"/"false"),
             * rather than a check box.
             */
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }        /*
             * Don't need to implement this method unless your table's
             * editable.
             */
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }        /*
             * Don't need to implement this method unless your table's
             * data can change.
             */
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                                       + " to " + value
                                       + " (an instance of "
                                       + value.getClass() + ")");
                }            data[row][col] = value;
                fireTableCellUpdated(row, col);            if (DEBUG) {
                    System.out.println("New value of data:");
                    printDebugData();
                }
            }        private void printDebugData() {
                int numRows = getRowCount();
                int numCols = getColumnCount();            for (int i=0; i < numRows; i++) {
                    System.out.print("    row " + i + ":");
                    for (int j=0; j < numCols; j++) {
                        System.out.print("  " + data[i][j]);
                    }
                    System.out.println();
                }
                System.out.println("--------------------------");
            }
        }    /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);        //Create and set up the window.
            JFrame frame = new JFrame("TableDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Create and set up the content pane.
            TableDemo newContentPane = new TableDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);        //Display the window.
            frame.pack();
            frame.setVisible(true);
        }    public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
      

  6.   

    When the entire table is resized, all the columns are resized. To customize initial column widths, you can invoke setPreferredWidth on each of your table's columns. This sets both the preferred widths of the columns and their approximate relative widths. For example, adding the following code to SimpleTableDemo makes its third column bigger than the other columns: TableColumn column = null;
    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);
        if (i == 2) {
            column.setPreferredWidth(100); //sport column is bigger
        } else {
            column.setPreferredWidth(50);
        }
    }As the preceding code shows, each column in a table is represented by a TableColumn object. TableColumn supplies getter and setter methods for the minimum, preferred, and maximum widths of a column, as well as a method for getting the current width. For an example of setting cell widths based on the actual amount of space needed to draw the cells' contents, see the initColumnSizes method in TableRenderDemo.java. When the user explicitly resizes columns, the columns' preferred widths are set such that the user-specified sizes become the columns' new current widths. However, when table itself is resized — typically because the window has resized — the columns' preferred widths do not change. Instead, the existing preferred widths are used to calculate new column widths to fill the available space. You can change a table's resize behavior by invoking the setAutoResizeMode method. The method's argument should have one of these values (defined as constants in JTable): AUTO_RESIZE_SUBSEQUENT_COLUMNS 
    The default. In addition to resizing the column to the left of the drag point, adjusts the sizes of all columns to the right of the drag point. 
    AUTO_RESIZE_NEXT_COLUMN 
    Adjusts only the columns immediately to the left and right of the drag point. 
    AUTO_RESIZE_OFF 
    Adjusts the table size instead. Detecting User Selections 
    The following code snippet shows how to detect when the user selects a table row. By default, a table allows the user to select multiple rows — not columns or individual cells — and the selected rows need not be next to each other. Using the setSelectionMode method, the following code specifies that only one row at a time can be selected. 
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ...
    //Ask to be notified of selection changes.
    ListSelectionModel rowSM = table.getSelectionModel();
    rowSM.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            //Ignore extra messages.
            if (e.getValueIsAdjusting()) return;        ListSelectionModel lsm =
                (ListSelectionModel)e.getSource();
            if (lsm.isSelectionEmpty()) {
                ...//no rows are selected
            } else {
                int selectedRow = lsm.getMinSelectionIndex();
                ...//selectedRow is selected
            }
        }
    });The code is from SimpleTableSelectionDemo.java. SimpleTableSelectionDemo also has code (not included in the preceding snippet) that changes the table's selection orientation. By changing a couple of boolean values, you can make the table allow either column selections or individual cell selections, instead of row selections. 
    For more information and examples of implementing selection, see How to Write a List Selection Listener.