我在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; }
这解决了我的问题:
table.removeColumn(table.getColumnModel().getColumn(0));
我把它放在班级建设者中。这样就可以从表的视图中删除该列,但TableModel仍包含“ ID”列。我发现很多人都在寻找一种从sql / mysql中的SELECT语句中排除特定列(例如自动增量)的选项,但是语言本身没有该功能。因此,我希望该解决方案也能对其他人有所帮助。