This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Selecting next record from a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Selecting next record from a table" Watch "Selecting next record from a table" New topic
Author

Selecting next record from a table

Shashank Anand
Greenhorn

Joined: Oct 30, 2008
Posts: 26
Hi All,

I have a table with two columns firstname and lastname. I want to display the values in textfields, navigating to next/previous records on pressing 'next' and 'previous' buttons. I am a newbie in jdbc and expecting some advice.

Thanks.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

this article will help you to move a head..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Shashank Anand
Greenhorn

Joined: Oct 30, 2008
Posts: 26
That's a nice one. But I am not using netBeans at present. I am trying to do this with ResultSet. See my code below (that is not working):

ResultSet rs;
Statement stmt;
Connection con;
//Add ActionListeners to Buttons- first,next,previous,last.
public void actionPerformed (ActionEvent e) {
if (e.getActionCommand()=="first") {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbcdbcSNname", "name", "password");
stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("SELECT firstname, lastname FROM mytable ORDER BY firstname");
while (rs.first()) {
text1.setText(rs.getString(1));
text2.setText(rs.getString(1));
}
con.close();
}
catch (Exception ex) { System.out.println("Error:" + ex); }
}


if (e.getActionCommand()=="next") {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbcdbcSNname", "name", "password");
stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("SELECT firstname, lastname FROM mytable ORDER BY firstname");
while (rs.next()) {
text1.setText(rs.getString(1));
text2.setText(rs.getString(1));
}
con.close();
}
catch (Exception ex) { System.out.println("Error:" + ex); }
}

...
How can I make it work ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selecting next record from a table
 
Similar Threads
placing the next, previous bar on the bottom of the table
displaytag should show only previous and next
how to paging database results in different html pages
Pagination in jsp
Regarding displying data from the table