基于MySQL查询动态更改数据表列标题

时间:2019-03-03 14:56:28

标签: java mysql

我有1个数据表,该数据表填充了来自2个不同查询的2组MySQL结果,但是它们具有定义的固定列标题的数据表。如何根据查询动态更改列标题。

    private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:

    ((DefaultTableModel) tblloan.getModel()).setRowCount(0);

    DefaultTableModel model = (DefaultTableModel) tblloan.getModel();

    try {

        Statement st = con.createStatement();
        String query = "select firstname,lastname,age from info ";
        ResultSet rs = st.executeQuery(query);

        while (rs.next()) {
            String fn = rs.getString("firstname");
            String ln = rs.getString("lastname");
            String ag = rs.getString("age");

            model.addRow(new Object[]{fn, ln, ag});
        }
        rs.close();
        st.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());

    }
} 

    private void jButton30ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:

    ((DefaultTableModel) tblloan.getModel()).setRowCount(0);

    DefaultTableModel model = (DefaultTableModel) tblloan.getModel();

    try {

        Statement st = con.createStatement();
        String query = "select loanID,loandate,username from loan ";
        ResultSet rs = st.executeQuery(query);

        while (rs.next()) {
            String LID = rs.getString("loanID");
            String LDate = rs.getString("loandate");
            String Un = rs.getString("username");

            model.addRow(new Object[]{LID, LDate, Un});
        }
        rs.close();
        st.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());

    }
} 

当我按下jButton20时,表列标题应显示名字,姓氏,年龄,当我按下jButton30时,表列标题应显示借贷ID,借贷日期,用户名。

0 个答案:

没有答案