tags:

views:

18

answers:

1

Given the following example (supposed to be correct JDBC, but I'm noob):

Connection conn = getConnection();
Statement st = conn.createStatement();

ResultSet rs = st.executeQuery("SELECT NAME FROM USERS");
while(rs.next())
{
     string name = rs.getString("NAME");
     //...
}

At what point is NAME information transferred from the database to the program? Is this each time that getString is called? If so, is it also possible to collect the names for all users in one I/O operation?

A: 

It may be that I misunderstand the question, but answering based on what I think you're asking:

All information is fetched from the database when you execute

ResultSet rs = st.executeQuery("SELECT NAME FROM USERS");

All operations performed on rs after that will take place in program memory, and not on the database.

I'm not sure what you mean about if it's possible to collect the names for all users in one I/O operation.

kskjon
So if there are many users in the database could the executeQuery cause a memory overflow?
Hopefully others will correct me if I'm wrong, but yes. I think so. HOWEVER, that shouldn't be a problem unless you have an enormous amount of users, or an extremely underpowered server. Perhaps both.
kskjon
From the first comment on: http://stackoverflow.com/questions/3868500/purpose-of-splitting-a-select-query-into-chunksI actually get the impression that an I/O action is performed on every rs.next() call. So that would mean executeQuery does not transfer any actual row information to the program.
It'seems like I might have been partially right and wrong.Apparently, you can (to a certain degree) choose if you want to fetch the whole result set from the server, or if you want to use a server side cursor which fetches just the information you request, one row at a time.http://www.coderanch.com/t/203098/Performance/java/JDBC-Cursors-vs-client-side
kskjon