• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Selecting next record from a table

 
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.
 
Ranch Hand
Posts: 1325
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("jdbc:odbc:DSNname", "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("jdbc:odbc:DSNname", "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
Boost this thread!