How to make one mySQL's table column invisible

我怕爱的太早我们不能终老 提交于 2019-12-20 03:05:33

问题


I am running a query on ID column but I don't want it to be visible in my frame/pane. How can I achieve this? Shall I make another table, is there a function in sql/mysql which allows to hide columns? 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;
    }

回答1:


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. 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. So I hope this solution will help others as well.




回答2:


Don't put ID in the select part of the query

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


来源:https://stackoverflow.com/questions/27358576/how-to-make-one-mysqls-table-column-invisible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!