简体   繁体   中英

Little Confusion (ResultSets, OracleDB, JAVA)

I can successfully connect to an oracle database and print out the list of all table names using the getAllTableNames() method.

My next Idea is to filter specific tables and display all of their columns.

I am running this query at the moment (I am including the entire method.)

static void getLengths(){
String query = "SELECT column_name from user_tab_columns where table_name = '<MytableName>'"

try{
ResultSet rs = db.runQuery(query);
System.out.println(rs):
} catch (Exception ex) {
System.out.println(ex);
//Seems to be successful
}
} //End of Method

In the System.out Window I am receiving this which makes me think I just need to view my result set somehow?

oracle.jdbc.driver.DcrollableResultSet@(different number everytime I run code)

Is my result set hiding somewhere? What is the best way to go about viewing it?

System.out.println(rs) just prints out the class name and handle of the ResultSet object. This is pretty useless.

You need to loop through the result set and read the appropriate fields. Generally you write something like:

ResultSet rs=db.runQuery(query) // I don't know where this function is coming from, but okay
while (rs.next())
{
  String myColumn=rs.getString("column_name");
  System.out.println(myColumn);
}
rs.close();

You might want to read up on JDBC in general, and look at the Java Docs for Connection, Statement, and ResultSet.

You have to iterate over your ResultSet to get the values. Do something like that

while(rs.next()) {System.out.println(rs.getString("COLUMN_NAME"));}

ResultSet javadoc states

A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

So, you'll have to use the appropriate getXXX(int index) methods where XXX is your equivalent Java Datatype. And don't forget to invoke next() before doing anything else!

Read Retrieving and Modifying Values from Result Sets to see an example of what you're trying to accomplish.

您将需要逐步浏览结果集,这在我的Java上非常生锈,但是您可能可以在结果集上调用“ toString()”,以至少向您显示返回的内容,尽管它不会非常有用。

System.out.println(rs.toString())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM