Navigating through records
I have used the follwing code to navigate the records of my table.
But the problem is when the last record is reached it displays error message.
I have used even isLast() or isAfterLast() to check but still not working.
Also the previous butto not working properly.
Where exactly is the mistake ?
******************************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class project extends JFrame
{
JTextField t1,t2,t3;
JScrollPane p1;
JButton b1,b2,b3,b4;
Connection con;
Statement stmt;
ResultSet rs;
JPanel p;
int i;
public project()
{
super("Database Example");
i=1;
getContentPane().setLayout(new GridLayout(4,1,12,12));
t1= new JTextField("");
t1.setColumns(15);
getContentPane().add(t1);
t2= new JTextField("");
t2.setColumns(15);
getContentPane().add(t2);
t3= new JTextField("");
t3.setColumns(15);
getContentPane().add(t3);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Lalit");
}
catch(Exception ex)
{
JOptionPane.showConfirmDialog(null,ex);
}
p=new JPanel();
p.setLayout(new GridLayout(1,4,12,12));
b1=new JButton("Next Record");
p.add(b1);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("Select * from emp");
if(rs.isLast()==true)
{
rs.first();
i=1;
}
else
{
rs.absolute(i);
i++;
String name,sal;
name=rs.getString(2);
sal=rs.getString(3);
t1.setText(name);
t2.setText(sal);
}
}
catch(Exception ex)
{
JOptionPane.showConfirmDialog(null,"End of Record Reached");
}
}
}
);
b2=new JButton("Previous Record");
p.add(b2);
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("Select * from emp");
if(rs.isBeforeFirst()==true||rs.isFirst()==true)
{
rs.first();
}
else
{
rs.absolute(i-1);
i--;
JOptionPane.showConfirmDialog(null,Integer.toString(i));
String name,sal;
name=rs.getString(2);
sal=rs.getString(3);
t1.setText(name);
t2.setText(sal);
}
}
catch(Exception ex)
{
JOptionPane.showConfirmDialog(null,ex);
}
}
}
);
}
public static void main(String args[])
{
project p =new project();
}
}
Message was edited by:
Siriya

