简体   繁体   English

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

[英]How to make one mySQL's table column invisible

I am running a query on ID column but I don't want it to be visible in my frame/pane. 我在ID列上运行查询,但我不希望它在我的框架/窗格中可见。 How can I achieve this? 我该如何实现? Shall I make another table, is there a function in sql/mysql which allows to hide columns? 我可以再创建一张表吗,sql / mysql中是否有一个函数可以隐藏列? I tried to google it but havent found anything yet. 我试图用谷歌搜索,但还没有发现任何东西。 Here is the code: 这是代码:

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;
    }

This has solved my problem: 这解决了我的问题:

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

I placed this in my class contructor. 我把它放在班级建设者中。 This lets remove the column from the view of the table but column 'ID' is still contained in the TableModel. 这样就可以从表的视图中删除该列,但TableModel仍包含“ ID”列。 I found that many people looking for an option to exclude specific column (like autoincrement) from SELECT statement in sql / mysql but the language itself doesn't have that feature. 我发现很多人都在寻找一种从sql / mysql中的SELECT语句中排除特定列(例如自动增量)的选项,但是语言本身不具有该功能。 So I hope this solution will help others as well. 因此,我希望该解决方案也能对其他人有所帮助。

Don't put ID in the select part of the query 不要将ID放入查询的选择部分

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM