是否可以使用连接查询作为refrernce更新联结表?
我正在使用Jtable来显示数据库中的数据。 jtable使用CacheRowSet
加载到Jframe上jframeconstructor(){
...
CachedRowSet myCachedRowSet = getContentsOfTable();// performs query to load table
myTableModel = new TheTableModel(myCachedRowSet);// class extends the AbstractTableModel to enable editing
jtable.setModel(TheTableModel);
...
}
public CachedRowSet getContentsTable() throws SQLException {
CachedRowSet crs = null;
int [] key={1,2};
try {
conn = s.getConnection(); //get connection from session
conn.setAutoCommit(false);//set auto commit to false
crs = new CachedRowSetImpl();
crs.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
crs.setConcurrency(ResultSet.CONCUR_UPDATABLE);
crs.setUsername(s.getM_sappuser());
crs.setPassword(s.getM_spassword());
if (this.s.getM_database().equals("mysql")) {
crs.setUrl(s.getM_surl()
+ "?relaxAutoCommit=true");
} else
{
crs.setUrl(s.getM_surl());
}
crs.setCommand("SELECT TICKET_id, line TICKETNumber, item_id, price, Units FROM TICKETLINES ;");
crs.setKeyColumns(key);
crs.execute();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
return crs;
}
我可以在TheTableModel类中使用此方法从swing UI编辑product_id:
public void setValueAt(Object value, int row, int column) {
System.out.println("Calling setValueAt row " + row + ", column " + column + ", Value :"+ value);
try {
//get the value and update cacheRowSet
this.coffeesRowSet.absolute( row + 1 );
this.coffeesRowSet.updateObject((column + 1), value);
this.coffeesRowSet.updateRow();
this.coffeesRowSet.setTableName("TICKETLINES");
this.coffeesRowSet.acceptChanges(s.getConnection());
this.coffeesRowSet.refreshRow();
} catch (SQLException ex) {
Logger.getLogger(CoffeesTableModel.class.getName()).log(Level.SEVERE, null, ex);
}
fireTableCellUpdated(row, column);
}
我希望表格显示项目名称而不是项目ID。因此,如果我在三个表上执行连接,是否可以从结果表中编辑(更新)记录,以便在juction表上重新选择它们?
我的意思是我有这个联结表
TICKETLINES
-----------------------------------------------------------
Ticketid |line number |item_id |description |units price
---+--------+------------+--------+------------+-----------
| | | | |
和另外两个表
ITEM
id |name |description |price
---+-----+------------+-----
| | |
TICKET
-------------------
id |ticketnumber
---+-----+---------
| |
在表格上执行内部联接后
SELECT t.ticketnumber AS TicketNumber, i.name as ProductName, tl.price as PRICE, tl.line FROM TICKETS AS T JOIN TICKETLINES AS tl ON tl.TICKET= t.id JOIN ITEM AS i on tl.product=i.id ORDER BY t.ticketnumber,tl.line ;
如果第一条记录是
,我会得到一个结果表ticketnumber ProductName price line
------------+-----------+-------+-------
1 | unicorn | 0 | 0
我可以在此resultSet上更改产品名称以说“粉红色大象”(假设粉红色大象在项目表中有id),然后更新我的标题item_id吗? (实际上,我试过了,我在同步时遇到了冲突,所以我只想知道这样的事情是否真的有可能)