Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigate records

Status
Not open for further replies.

yippiekyyay

Programmer
Oct 26, 2002
186
CA
Hello,

I'm a java newbie and am having trouble with navigating records in a resultset.

I managed to pass a resultset from main to the class which creates my form. I was then able to populate my textfields with the fields - so my first record shows up fine. I would now like to add a record navigation button. I can add buttons and create actionlisteners - but cannot think of what to put in the actionlistener. My instinct was to "rs.next();" which didn't work and now I'm out of ideas. Any help would be greatly appreciated! (including any mistakes I'm making on things that are working)

-Sean
(here's the code in case it helps)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Test extends JFrame {
private JButton nextButton;
private JTextField txtFirstName, txtLastName;
private JLabel lblFirstName, lblLastName;
private String fn, ln;

public Test( ResultSet rs)
{
super( "Icon Test" );

Container container = getContentPane();
container.setLayout( new GridLayout(3,3) );

try {
fn = rs.getString(1);
ln = rs.getString(2);
}
catch (Exception e)
{
System.out.println("Error: " + e);
}

lblFirstName = new JLabel( " First Name: " );
container.add( lblFirstName );
txtFirstName = new JTextField( fn, 10 );
container.add( txtFirstName );
lblLastName = new JLabel( " Last Name: " );
container.add(lblLastName);
txtLastName = new JTextField( ln, 10 );
container.add( txtLastName );

nextButton = new JButton( ">" );
container.add( nextButton );

ButtonHandler handler = new ButtonHandler();
nextButton.addActionListener( handler );

setSize( 350, 120 );
setVisible( true );
}


private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent event )
{
//instead of this message, I would like it to go
//to the next record ...?
JOptionPane.showMessageDialog( null, "Next record" );
//rs.next(); //...?
}
}

public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename = "C:/Java/Forms.mdb";
String database = "jdbc:eek:dbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}";

Connection con = DriverManager.getConnection( database ,"","");

Statement s = con.createStatement();
s.execute("select FirstName, LastName from tblPerson");
ResultSet rs = s.getResultSet();
if (rs != null)
rs.next();
{
Test application = new Test(rs);
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
System.out.println(rs.getString(1));
}

s.close();
con.close();
}
catch (Exception e)
{
System.out.println("Error: " + e);
}

}
}
 
Have you tried the JDBC tutorials and java.sun.com? All your fundamental JDBC questions should have answers there. [cheers]

-pete
 
Guess I'm further off than I thought!

I had started to look over those tutorials, but didn't know where to go because I don't have a problem creating a form nor getting java to read from a db - it's the combination of the two. The tutorials at first glance seemed to focus on one or the other - but I'll take a closer look.

Cheers!
-Sean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top