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);
S开发者_如何学JAVAystem.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.
You will need to step through the result set, very rusty on my java, but you can probably call "toString()" on the resultset to at least show you what was returned although its not going to be very usable.
System.out.println(rs.toString())
精彩评论