aspose file tools*
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
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 ?
 
Don't get me started about those stupid light bulbs.
 
subject: Selecting next record from a table