简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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