This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Selecting next record from a table

 
Shashank Anand
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this article will help you to move a head..
 
Shashank Anand
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic