我不完全理解为什么ResultSet.next()会在我的代码中返回false
。
我使用的是intelliJ Idea和oracle数据库。我有2个类:DataHandler和JavaClient
import java.sql.ResultSet;
import java.util.Locale;
public class JavaClient {
JavaClient() {}
public static void main(String[] args) throws Exception {
Locale.setDefault(Locale.ENGLISH);
DataHandler datahandler = new DataHandler();
ResultSet rset = datahandler.getAllStudent();
if (!rset.isBeforeFirst())
System.out.println("False");
while (rset.next()) {
System.out.println(rset.getObject(1) + " " +
rset.getString(2) + " " +
rset.getString(3) + " " +
rset.getString(4));
}
}
}
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
public DataHandler() {
}
String jdbcUrl = "jdbc:oracle:thin:@//localhost:1525/XE";
String userid = "correctuserid";
String password = "correctpassword";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
public void getDBConnection() throws SQLException {
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
}
public ResultSet getAllStudent() throws SQLException {
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "SELECT * FROM TEST ORDER BY FullName";
System.out.println("\nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}
}
当我运行此程序时,我得到以下结果:
Executing query: SELECT * FROM TEST ORDER BY FullName
False
Process finished with exit code 0
当我在sqlplus中编写此查询时,我得到以下结果: enter image description here
所以我的问题是,当我的表不空时,为什么我会在resultset.next()中获得false
?
答案 0 :(得分:0)
请进行提交,然后再次执行该程序。