我将使用JoinRowSet加入两个表(它们位于不同的DB服务器上)。 我能够让它运作起来。但是,我发现了一个严重的问题。 我的joines resultSet中有两行丢失。为了证明这一点,我加入了相同的查询,但仍然缺少两行(我认为我必须返回一个结果集,每个连接集的行数相同)。
这是我的代码:
@Stateless
public class TestEJB {
public void getRektCrossJoin() throws Exception {
Connection firstDB = null;
Connection secondDB = null;
try {
Context ic = new InitialContext();
try {
firstDB = ((DataSource) ic.lookup("java:app/env/jdbc/firstDB")).getConnection();
} catch (Exception e) {
firstDB = ((DataSource) ic.lookup("java:comp/env/jdbc/firstDB")).getConnection();
}
try {
secondDB = ((DataSource) ic.lookup("java:app/env/jdbc/secondDB")).getConnection();
} catch (Exception e) {
secondDB = ((DataSource) ic.lookup("java:comp/env/jdbc/secondDB")).getConnection();
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
String sqlAfa = "SELECT * FROM afa order by percent;";
try (PreparedStatement stmtS = firstDB.prepareStatement(sqlAfa);PreparedStatement stmtJ = secondDB.prepareStatement(sqlAfa)) {
RowSetFactory rowSetFactory = RowSetProvider.newFactory();
try (ResultSet rsS = stmtS.executeQuery();
ResultSet rsJ = stmtJ.executeQuery();
JoinRowSet joinRowSet = rowSetFactory.createJoinRowSet()) {
System.out.println("\n >>> First DB <<< ");
int columnCountS = rsS.getMetaData().getColumnCount();
for(int i = 1; i<= columnCountS; i++) {
System.out.print(rsS.getMetaData().getColumnName(i) + "\t");
}
System.out.println();
while (rsS.next()) {
for(int i=1; i<= columnCountS; i++) {
System.out.print(rsS.getString(i) + "\t");
}
System.out.println();
}
System.out.println("\n >>> Second DB <<< ");
int columnCountJ = rsJ.getMetaData().getColumnCount();
for(int i = 1; i<= columnCountJ; i++) {
System.out.print(rsJ.getMetaData().getColumnName(i) + "\t");
}
System.out.println();
while (rsJ.next()) {
for(int i=1; i<= columnCountJ; i++) {
System.out.print(rsJ.getString(i) + "\t");
}
System.out.println();
}
rsS.beforeFirst();
rsJ.beforeFirst();
CachedRowSet cachedRowSet = rowSetFactory.createCachedRowSet();
CachedRowSet cachedRowSet2 = rowSetFactory.createCachedRowSet();
cachedRowSet.populate(rsS);
joinRowSet.addRowSet(cachedRowSet,2); //second field is unique
cachedRowSet2.populate(rsJ);
joinRowSet.addRowSet(cachedRowSet2,2); //second field is unique
joinRowSet.first();
if (!joinRowSet.next() ) {
System.err.println("No data");
} else {
int columnCount = joinRowSet.getMetaData().getColumnCount();
for(int i = 1; i<= columnCount; i++) {
System.out.print(joinRowSet.getMetaData().getColumnName(i) + "\t");
}
System.out.println();
while(joinRowSet.next()) {
for(int i=1; i<= columnCount; i++) {
System.out.print(joinRowSet.getString(i) + "\t");
}
System.out.println();
}
}
cachedRowSet.close();
cachedRowSet2.close();
}
}
firstDB.close();
secondDB.close();
}
这是输出:
INFO [stdout] Southpark DB megvan
INFO [stdout] Jerry DB megvan
INFO [stdout]
INFO [stdout] >>> First DB <<<
INFO [stdout] category percent from_date to_date
INFO [stdout] 0 0 1900-01-01 2030-12-31
INFO [stdout] 2 5 2018-01-01 2030-12-31
INFO [stdout] 2 18 2017-01-01 2017-12-31
INFO [stdout] 1 20 1900-01-01 2009-06-30
INFO [stdout] 1 25 2009-07-01 2011-12-31
INFO [stdout] 1 27 2012-01-01 2030-12-31
INFO [stdout]
INFO [stdout] >>> Second DB <<<
INFO [stdout] category percent from_date to_date
INFO [stdout] 0 0 1900-01-01 2030-12-31
INFO [stdout] 2 5 2018-01-01 2030-12-31
INFO [stdout] 2 18 2017-01-01 2017-12-31
INFO [stdout] 1 20 1900-01-01 2009-06-30
INFO [stdout] 1 25 2009-07-01 2011-12-31
INFO [stdout] 1 27 2012-01-01 2030-12-31
INFO [stdout]
INFO [stdout] >>> JoinRowSet <<<
INFO [stdout] category percent from_date to_date category from_date to_date
INFO [stdout] 1 20.0 1900-01-01 2009-06-30 1 1900-01-01 2009-06-30
INFO [stdout] 2 18.0 2017-01-01 2017-12-31 2 2017-01-01 2017-12-31
INFO [stdout] 2 5.0 2018-01-01 2030-12-31 2 2018-01-01 2030-12-31
INFO [stdout] 0 0.0 1900-01-01 2030-12-31 0 1900-01-01 2030-12-31
可能发生了什么? 提前谢谢!
编辑: 我复制了afa选择:
String sqlAfa = "SELECT * FROM afa order by percent DESC;";
String sqlAfa2 = "SELECT * FROM afa order by percent ASC;";
之后我修改了outter try-with-resources块:
PreparedStatement stmtJ = secondDB.prepareStatement(sqlAfa2)
这是输出:
INFO [stdout] >>> First DB <<<
INFO [stdout] category percent from_date to_date
INFO [stdout] 1 27 2012-01-01 2030-12-31
INFO [stdout] 1 25 2009-07-01 2011-12-31
INFO [stdout] 1 20 1900-01-01 2009-06-30
INFO [stdout] 2 18 2017-01-01 2017-12-31
INFO [stdout] 2 5 2018-01-01 2030-12-31
INFO [stdout] 0 0 1900-01-01 2030-12-31
INFO [stdout]
INFO [stdout] >>> Second DB <<<
INFO [stdout] category percent from_date to_date
INFO [stdout] 0 0 1900-01-01 2030-12-31
INFO [stdout] 2 5 2018-01-01 2030-12-31
INFO [stdout] 2 18 2017-01-01 2017-12-31
INFO [stdout] 1 20 1900-01-01 2009-06-30
INFO [stdout] 1 25 2009-07-01 2011-12-31
INFO [stdout] 1 27 2012-01-01 2030-12-31
INFO [stdout]
INFO [stdout] >>> JoinRowSet <<<
INFO [stdout] category percent from_date to_date category from_date to_date
INFO [stdout] 2 18.0 2017-01-01 2017-12-31 2 2017-01-01 2017-12-31
INFO [stdout] 1 20.0 1900-01-01 2009-06-30 1 1900-01-01 2009-06-30
INFO [stdout] 1 25.0 2009-07-01 2011-12-31 1 2009-07-01 2011-12-31
INFO [stdout] 1 27.0 2012-01-01 2030-12-31 1 2012-01-01 2030-12-31
现在缺少的行也发生了变化。
答案 0 :(得分:1)
如果您注意到,每次都缺少两行。所以,首先将光标移动到第一行
joinRowSet.first();
然后,在开始实际循环之前,您已经两次调用next
并且没有打印
if (!joinRowSet.next() ) {
...
...
while(joinRowSet.next()) {
joinRowSet.next()
会将光标从当前位置移动一行。所以,当你真正开始打印你的行时,你已经到了第三行。