Java 如何使一个mySQL的表列不可见
我正在对ID列运行查询,但我不希望它在我的框架/窗格中可见。我怎样才能做到这一点?我再做一个表,sql/mysql中有允许隐藏列的函数吗?我试着用谷歌搜索,但还没有找到任何东西。 代码如下:Java 如何使一个mySQL的表列不可见,java,mysql,database,jtable,tablemodel,Java,Mysql,Database,Jtable,Tablemodel,我正在对ID列运行查询,但我不希望它在我的框架/窗格中可见。我怎样才能做到这一点?我再做一个表,sql/mysql中有允许隐藏列的函数吗?我试着用谷歌搜索,但还没有找到任何东西。 代码如下: public void tableChanged(TableModelEvent e) { int row = e.getFirstRow(); int col = e.getColumn(); model = (MyTableModel) e.getSource(); St
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;
}
不要将ID放入查询的选择部分
String query ="SELECT tbl_Date as Date, Flat, Mobile, Food, Alcohol, Transport,
Outdoor, Pauls_stuff, Income, Stuff FROM finance.fin";
这解决了我的问题:
table.removeColumn(table.getColumnModel().getColumn(0));
我把这个放在了我的教室里。这允许从表的视图中删除列,但列“ID”仍包含在TableModel中。我发现很多人都在寻找一个选项,从sql/mysql中的SELECT语句中排除特定的列(如autoincrement),但该语言本身没有这个功能。所以我希望这个解决方案也能帮助其他人 如果您不想显示ID列,并且查询中不需要ID列,您可以将其保留在select语句中,并显示其余结果……此外,您还可以将ID保留在数据数组中,将日期保留在第0列中,将日期保留在第1列中,等等,并停止盗取我的名字的尝试!:查询中需要ID列,但不需要在显示数据时可见。好的,我需要消化信息;谢谢你的回复。我尝试了保罗和巴特的方法,但没有成功。表显示时没有ID列,但数据库中的数据没有更改。这是代码中使用ID的部分:stmt.addBatchupdate finance.fin set+stulpPav+=+duom+其中ID=+studId+;;