yippiekyyay
Programmer
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
dbc
river={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);
}
}
}
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
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);
}
}
}