如何使一个MySQL的表列不可见 [英] How to make one mySQL's table column invisible

查看:79
本文介绍了如何使一个MySQL的表列不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对ID列运行查询,但我不希望它在我的框架/窗格中可见.我怎样才能做到这一点?我可以制作另一个表吗,sql/mysql中是否有一个函数可以隐藏列?我试图用谷歌搜索,但还没有发现任何东西. 这是代码:

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

推荐答案

这解决了我的问题:

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

我将其放置在班级建设者中.这样就可以从表的视图中删除该列,但TableModel仍包含"ID"列.我发现很多人都在寻找从sql/mysql中的SELECT语句中排除特定列(例如自动增量)的选项,但是语言本身没有该功能.因此,我希望该解决方案也能对其他人有所帮助.

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.

这篇关于如何使一个MySQL的表列不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆