Java中两个ResultSet的比较
处理 Java ResultSet 的最佳方法是什么?我正在创建一个桌面应用程序,它将使用 JDBC 连接到 Oracle 数据库。
但是,我在处理 ResultSet 时遇到问题,因为我无法使用 for 循环进行比较。
// create a database connection object
DB db = new DB();
// get rows of my first table and second table
ResultSet firstRS = db.getMyFirstTable();
ResultSet seconRS = db.getSecondTable();
// compare the two tables
while(firstRS.next())
{
// get the row of my first table
String firstRSRow = firstRS.getString("col1");
while(secondRS.next())
{
// get the row of my second table
String secondRSRow = seconRS.getString("col1");
// compare row from first table to the row in second table
if(firstRSRow.startsWith(secondRSRow))
{
// do some processing here....
} // end if
} // end while
} // end while
我知道这可以通过 Hibernate 用几行代码来完成,但我没有这样的奢侈是时候研究一下了。
我还从 apache 找到了 commons DbUtils 但对于我作为一个新手程序员来说这似乎很复杂。
是否有其他足够简单的方法/工具/库/框架可以从数据库获取结果集并以简单直接的方式操作它?
如果您能引导我访问一个包含有关 Java 数据库连接的示例代码的网站,我也将不胜感激。
非常感谢您的支持。
What is the best way to handle Java ResultSet? I'm creating a Desktop application that will connect to an oracle database using JDBC.
However, I am having problem with handling ResultSets since I can't do comparison using for loop.
// create a database connection object
DB db = new DB();
// get rows of my first table and second table
ResultSet firstRS = db.getMyFirstTable();
ResultSet seconRS = db.getSecondTable();
// compare the two tables
while(firstRS.next())
{
// get the row of my first table
String firstRSRow = firstRS.getString("col1");
while(secondRS.next())
{
// get the row of my second table
String secondRSRow = seconRS.getString("col1");
// compare row from first table to the row in second table
if(firstRSRow.startsWith(secondRSRow))
{
// do some processing here....
} // end if
} // end while
} // end while
I know that this could be accomplish with Hibernate with a few lines of code but I don't have the luxury of time to study it.
I also found commons DbUtils from apache but it seems complicated for me as a novice programmer.
Is there other way/tools/libraries/framework simple enough to get ResultSet from databases and manipulate it in simple and straightforward manner?
I will also appreciate if you could direct me to a website that has sample codes concerning java database connectivity.
Thank you very much for your support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么
结果集
?您可以轻松地使用SELECT
语句比较值。如果您仍然想要 ResultSet,请考虑 CachedRowSet。Why
ResultSet
? You could easily compare values withSELECT
statement. In case you still want to ResultSet then think about CachedRowSet.ResultSet 最终是 Object 的子类,我们可以直接比较两个或多个对象。具体到你的问题:
where sql1 and sql2 are 2 statements
ResultSet is a subclass of Object ultimately and we can compare two or more objects directly. A little specific to your question:
where sql1 and sql2 are 2 Statements