Mor*_*yit 3 java sql oracle jdbc resultset
我有这个方法来获取一串行并打印它们.
另外,我要做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"));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,第一个Resultset.next()工作正常,但第二个不起作用!
有人能解释为什么吗?我怎样才能让它发挥作用?
注意:我知道,还有另外一种方法可以做到这一点,Resultset.next()
但我想做两次;)
你可以初始化你Statement的如下
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Run Code Online (Sandbox Code Playgroud)
因此,您可以在Statement中移动光标.
现在你可以循环它.
while(re.next())
num_row++;
re.beforeFirst();
Run Code Online (Sandbox Code Playgroud)
但这是非常必要的,最佳解决方案是跳转到集合的末尾并返回行
num_row = 0;
if(re.last()) {
num_row = rs.getRow();
re.beforeFirst();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5328 次 |
| 最近记录: |