有三個表<Table 1>, <Table 2> and <Table 3>
JDBC左連接3臺
我的SQL是這樣的:
"Select table1.col1, table1.col2, table1.col3, table2.col4, table2.col5, table2.col6,
table3.col7, table3.col8 from Table 1 as table1
LEFT JOIN Table 2 as table2 on (table1.col1 = table2.col4)
LEFT JOIN Table 3 as table3 on (table1.col1 = table3.col8)"
正常的方式來獲得結果集是:
public List getExportDataList() throws ClassNotFoundException, SQLException {
Connection connect = null;
String url = "jdbc:.....";
String username = "username ";
String password = "password ";
try {
connect = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
System.out.println("in exec");
System.out.println(ex.getMessage());
}
List dataList = new ArrayList<>();
PreparedStatement pstmt = connect.prepareStatement(
THE SQL CODE SHOWN ABOVE
}
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Table1 table1 = new Table1();
table1.setCOL1(rs.getString("col1"));
table1.setCOL2(rs.getString("col2"));
dataList.add(table1);
}
rs.close();
pstmt.close();
connect.close();
return dataList;
}
從而使「dataList」可用於在Primefaces數據表中顯示數據。
但是,這種方式只能將表1中的列保存到「dataList」中。我試圖dataList.add(table2)
以及dataList.add(table3)
在同一時間,但有一個錯誤:「/reportGenerate.xhtml @ 50,75值=」#{reportData.dateCreated}「:類‘net.picary.model.Liaison’沒有'dateCreated'屬性。「
有人能告訴我如何將三個表中的所有選定列保存到「dataList」中嗎?或者有其他的方法來實現它?
請顯示你使用的確切代碼和你得到的錯誤。另外請注意,當您按列名檢索值時,您需要確保這些名稱是唯一的。現在,您的結果集中有多列'col1',但您只能使用該名稱檢索第一個列,請參閱[這裏](http://stackoverflow.com/a/42410902/466862)。 –
正如@Mark所示,您可能需要使用列別名來確保ResultSet中的列名是唯一的:'SELECT table1.col1 AS t1_col1,table2.col1 AS t2_col1,...' –
感謝您的回覆。這是錯誤: 「/reportGenerate.xhtml @ 50,75 value =」#{reportData.dateCreated}「:類'net.picary.model.Liaison'沒有屬性'dateCreated'。」 – vennis