java-如何两次运行Resultsetet.next()?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何两次运行Resultsetet.next()?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1609字,纯文字阅读大概需要3分钟。
内容图文
![java-如何两次运行Resultsetet.next()?](/upload/InfoBanner/zyjiaocheng/664/09ad66fb25214310be9baa01996c815d.jpg)
我有这种方法来获取一行字符串并打印它们.
另外,我必须做两次while(Resultset.next())两次.第一个是获取行数,第二个是打印字符串.但是,当该方法第一次运行Resultset.next()时,该方法将跳过第二个Resultset.next().
public static String[] gett() throws ClassNotFoundException, SQLException{
// this for get conneced to the database .......................
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","111");
Statement st = conn.createStatement();
ResultSet re = st.executeQuery("select location_id from DEPARTMENTS");
// Ok , now i have the ResultSet ...
// the num_row it's counter to get number of rows
int num_row = 0;
// this Arrar to store String values
String[] n = new String[num_row];
// this is the first ResultSet.next , and it's work ..!
// also , this ResultSet.next work to get number on rows and store the number on 'num_row'
while(re.next())
num_row++;
// NOW , this is the secound 'ResultSet.next()' , and it's doesn't WORK !!!!
while(re.next()) {
System.out.println(re.getString("location_id"));
}
}
问题是,第一个Resultset.next()可以正常工作,但是第二个则无法工作!
有人可以解释为什么吗?我该如何运作呢?
注意:
我知道,在一个Resultset.next()中还有另一种方法可以做到这一点.
但是我想做两次;)
解决方法:
您可以将您的Statement初始化为以下内容
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
因此,您可以在Statement中移动光标.
现在您可以遍历它.
while(re.next())
num_row++;
re.beforeFirst();
但这是不必要的,最佳解决方案是跳到集合的末尾并返回行
num_row = 0;
if(re.last()) {
num_row = rs.getRow();
re.beforeFirst();
}
内容总结
以上是互联网集市为您收集整理的java-如何两次运行Resultsetet.next()?全部内容,希望文章能够帮你解决java-如何两次运行Resultsetet.next()?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。