如何使一个mySQL的表列不可见

时间:2014-12-08 12:58:54

标签: java mysql database jtable tablemodel

我正在ID列上运行查询,但我不希望它在我的框架/窗格中可见。我怎样才能做到这一点?我要制作另一个表,sql / mysql中是否有一个允许隐藏列的函数?我试着去谷歌,但还没找到任何东西。 这是代码:

public void tableChanged(TableModelEvent e) {
    int row = e.getFirstRow();
    int col = e.getColumn();
    model = (MyTableModel) e.getSource();
    String stulpPav = model.getColumnName(col);
    Object data = model.getValueAt(row, col);
    Object studId = model.getValueAt(row, 0);
    System.out.println("tableChanded works");
    try {
        new ImportData(stulpPav, data, studId);
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}
public class ImportData {
    Connection connection = TableWithBottomLine.getConnection();

    public ImportData(String a, Object b, Object c)
            throws ClassNotFoundException, SQLException {
        Statement stmt = null;
        try {

            String stulpPav = a;
            String duom = b.toString();
            String studId = c.toString();
            System.out.println(duom);
            connection.setAutoCommit(false);
            stmt = connection.createStatement();
            stmt.addBatch("update finance.fin set " + stulpPav + " = " + duom
                    + " where ID = " + studId + ";");
            stmt.executeBatch();
            connection.commit();
        } catch (BatchUpdateException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null)
                stmt.close();
            connection.setAutoCommit(true);
            System.out.println("Data was imported to database");
        }
    }   
    }        
public class MyTableModel extends AbstractTableModel{
    int rowCount;
    Object data [][];
    String columnNames [];
    public  MyTableModel() throws SQLException{
        String query ="SELECT ID, tbl_Date as Date, Flat, Mobile, Food, Alcohol, Transport, Outdoor, Pauls_stuff, Income, Stuff FROM finance.fin";
        ResultSet rs ;
        Connection connection = TableWithBottomLine.getConnection();

        Statement stmt = null;
        stmt = connection.createStatement();
        rs = stmt.executeQuery(query);

        rs.last();
        rowCount = rs.getRow();
        data = new Object[rowCount][11];
        rs = stmt.executeQuery(query);
        for (int iEil = 0; iEil < rowCount; iEil++){
            rs.next();
            data[iEil][0] = rs.getInt("ID");
            data[iEil][1] = rs.getDate("Date");
            data[iEil][2] = rs.getFloat("Flat");
            data[iEil][3]  = rs.getFloat("Mobile");
            data[iEil][4] = rs.getFloat("Food");
            data[iEil][5]  = rs.getFloat("Alcohol");
            data[iEil][6] = rs.getFloat("Transport");
            data[iEil][7] = rs.getFloat("Outdoor");
            data[iEil][8] = rs.getFloat("Pauls_stuff");
            data[iEil][9] = rs.getFloat("Income");
            data[iEil][10] = rs.getFloat("Stuff");
        }

         String[] columnName  = {"ID", "Date","Flat","Mobile"        
                ,"Food","Alcohol","Transport", "Outdoor", "Pauls_stuff", "Income", "Stuff"};
         columnNames = columnName;
    }

2 个答案:

答案 0 :(得分:2)

这解决了我的问题:

table.removeColumn(table.getColumnModel().getColumn(0));

我把它放在我的班级构造函数中。这样就可以从表格的视图中删除列,但是列“ID&#39;仍然包含在TableModel中。我发现许多人在sql / mysql中寻找从SELECT语句中排除特定列(如自动增量)的选项,但语言本身并没有这个功能。所以我希望这个解决方案能够帮助其他人。

答案 1 :(得分:0)

不要将ID放在查询的选择部分

String query ="SELECT tbl_Date as Date, Flat, Mobile, Food, Alcohol, Transport,    
Outdoor, Pauls_stuff, Income, Stuff FROM finance.fin";