GeeCON Prague 2014*
The moose likes JDBC and the fly likes Please help me the problem in rs.previous Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Please help me the problem in rs.previous" Watch "Please help me the problem in rs.previous" New topic
Author

Please help me the problem in rs.previous

vikram veera
Ranch Hand

Joined: Dec 26, 2000
Posts: 43
hello,
I had one problem jdbc.I am using Ms Acess database.If i
use rs.next() it gives no problem.Here rs stands for Resultset.
while usins rs.previous() it gives me a error saying
java.lang.unsupportedoperationException.

I have created table with two columns ,I am using awt as fronthand in that i created two textfield for dispaly of two columns from the table,and two Button one button by name Next and another button by name Previous.If i press Next button it will retrive frist row from the table and will be displayed in the textfield.Once again if i press Next button it display the next row from the table.Here In the Next Button there is no problem.I have problem in the previous Button.I could'nt able to move Backwords in the table.
If i use rs.previous() i gives me a error as i mention before.
Please help me in this with coding.Along with this i am attaching
My program so please make some changes so that i is possible to move to the previous row.
Program is given below
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class But1 extends Frame implements ActionListener
{
int i=1;
Button next;
Button pre;
TextField t1;
TextField t2;
Connection co;
Statement st;
ResultSet rs;
But1()
{
super("My Button");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver registered ");
co=DriverManager.getConnection("jdbc dbc:vicky");
System.out.println("Connection Established");
setSize(300,300);
setLayout(new FlowLayout());
next=new Button("Next");
pre =new Button("Prev");
t1=new TextField(10);
t2=new TextField(10);
add(t1);
add(t2);
add(next);
add(pre);
next.addActionListener(this);
pre.addActionListener(this);
setVisible(true);
}
catch(ClassNotFoundException ce)
{
System.out.println(ce);
}
catch(Exception e)
{
System.out.println(e);
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Next")
{
try
{
st=co.createStatement();
rs=st.executeQuery("Select * from mytable ");
while(rs.next())
{
if(rs.getInt(1)==i)
{
t1.setText(rs.getString(2));
t2.setText(rs.getString(3));
}

}
}catch(Exception e)
{
System.out.println(e);
}
i++;
}
/* if(ae.getActionCommand()=="Prev")
{
System.out.println("control here");
try{
st=co.createStatement();
rs=st.executeQuery("Select * mytable");
while(rs.previous())
{
System.out.println("yes previous");
if(rs.getInt(1)==i)
{
t1.setText(rs.getString(2));
t2.setText(rs.getString(3));
}

}
}catch(Exception e){}
i--;
}*/

}
}
class But
{
public static void main(String a[])
{
But1 b=new But1();
}
}

Please help me in coding.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The exception says it all, really: your JDBC driver does not support the previous() method. If you want to go to the previous record you'll either have to cache your result set or redo the query. Or something in between -- cache a certain number of results, re-querying when you need a record not in your cache.
- Peter

[This message has been edited by Peter den Haan (edited March 03, 2001).]
Rajesh Kumar77
Greenhorn

Joined: Sep 04, 2001
Posts: 12
to use rs.previous() method
you need to have jdk 1.3 in your system
pl check the following code in your system after giving jdbc connections
import java.sql.*;
public class MyConnection
{
Connection con;
MyConnection()
{
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:dsnaccess","","");
}
catch(Exception e)
{
System.out.println(" in myconnection.java file " +e);
}
}
}

*********************************************
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class UserSearchMod extends Frame implements ActionListener
{
Label userIDLbl,userNameLbl;
TextField userIDTxt,userNameTxt;
Label lbUser[][];
Checkbox chUser[];
Button searchUser;
ScrollPane spUser;
Panel userPanel,recUserPanel[];
Button previous,next,reset,exit;
MyConnection myCon;
Statement st,st1,st2;
ResultSet rs,rs2;
int i;
boolean nextP,prev,ent;

UserSearchMod()
{
setLayout(null);
myCon = new MyConnection();

try
{
st = myCon.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
st1 = myCon.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
st2 = myCon.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(Exception e)
{
System.out.println("defining connection objects "+e);
}
userIDLbl = new Label("User ID");
userNameLbl = new Label("User Name");
userIDTxt = new TextField(15);
userNameTxt = new TextField(15);
searchUser = new Button("Search");
previous=new Button("Previous");
next=new Button("Next");
reset=new Button("Reset");
exit = new Button("Exit");
spUser = new ScrollPane();
userPanel = new Panel();
add(userIDLbl);
add(userIDTxt);
add(userNameLbl);
add(userNameTxt);
add(searchUser);
add(spUser);
spUser.add(userPanel);
add(previous);
add(next);
add(reset);
add(exit);
searchUser.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
userIDLbl.setBounds(150,30,50,25);
userIDTxt.setBounds(210,30,100,25);
userNameLbl.setBounds(335,30,65,25);
userNameTxt.setBounds(410,30,100,25);
searchUser.setBounds(550,30,75,25);
spUser.setBounds(50,60,500,200);
previous.setBounds(150,400,75,25);
next.setBounds(250,400,75,25);
reset.setBounds(350,400,75,25);
exit.setBounds(450,400,75,25);
/* previous.setBounds(150,530,75,25);
next.setBounds(250,530,75,25);
reset.setBounds(350,530,75,25);
exit.setBounds(450,530,75,25); */

setSize(600,450);
setLocation(0,0);
show();
}
public void nextRecords()
{
nextP=true;
userPanel.removeAll();
lbUser = new Label[6][5];
userPanel.setLayout(new GridLayout(6,1));
recUserPanel=new Panel[6];
System.out.println("Hello sop from next() before try ");
try
{
System.out.println("Hello sop 1 "+rs.getRow());
while(prev&&i<6)
{
rs.next();
i++;
}
i=0;
System.out.println("Hello sop 2 "+rs.getRow());
if(!rs.isFirst()&&prev)
{
rs.previous();
prev=false;
}
System.out.println("Hello sop 3 "+rs.getRow());
if(ent)
{
do
{
if(rs.isLast())
{
next.setEnabled(false);
}
recUserPanel[i] = new Panel();
recUserPanel[i].setLayout(new GridLayout(1,5));
System.out.println("Hello sop 4 "+rs.getRow());
for(int j=0;j<5;j++)
{
lbUser[i][j] = new Label(rs.getString(j+1));
lbUser[i][j].setSize(75,25);
recUserPanel[i].add(lbUser[i][j]);
}
userPanel.add(recUserPanel[i]);
i++;
}
while( i<6 && rs.next() );
System.out.println("Hello sop 5 "+rs.getRow());
}
else
{
while( i<6 && rs.next() )
{
if(rs.isLast())
{
next.setEnabled(false);
}
recUserPanel[i] = new Panel();
recUserPanel[i].setLayout(new GridLayout(1,5));

for(int j=0;j<5;j++)
{
lbUser[i][j] = new Label(rs.getString(j+1));
lbUser[i][j].setSize(75,25);
recUserPanel[i].add(lbUser[i][j]);
}
userPanel.add(recUserPanel[i]);
i++;
}
}
System.out.println("Hello sop 6 "+rs.getRow());
}
catch(Exception e)
{
System.out.println("Exception in next records () "+e);
}
}
public void prevRecords()
{
prev=true;
ent=true;
System.out.println("Previous..");
userPanel.removeAll();
lbUser = new Label[6][5];
userPanel.setLayout(new GridLayout(6,1));
recUserPanel=new Panel[6];
try
{
while(i>0)
{
rs.previous();
i--;
}
i=5;
while(i>=0 && rs.previous())
{
System.out.println(i);
if(rs.isFirst())
{
previous.setEnabled(false);
}
recUserPanel[i] = new Panel();
recUserPanel[i].setLayout(new GridLayout(1,5));

for(int j=0;j<5;j++)
{
lbUser[i][j] = new Label(rs.getString(j+1));
lbUser[i][j].setSize(75,25);
recUserPanel[i].add(lbUser[i][j]);
}
userPanel.add(recUserPanel[i]);
i--;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void actionPerformed(ActionEvent ae)
{
String sqlString=null;
if(ae.getSource()==searchUser)
{
show();
next.setEnabled(true);
previous.setEnabled(false);
if(!(userIDTxt.getText().equals("") && userNameTxt.getText().equals("")))
{
sqlString = "select * from users where userid like '" + userIDTxt.getText() + "%' and name like '" + userNameTxt.getText() + "%'";
}
if(!(userIDTxt.getText().equals("")) && userNameTxt.getText().equals(""))
{
sqlString = "select * from users where userid like '" + userIDTxt.getText() + "%'";
}
if(userIDTxt.getText().equals("") && !(userNameTxt.getText().equals("")))
{
sqlString = "select * from users where name like '" + userNameTxt.getText() + "%'";
}
if(userIDTxt.getText().equals("") && userNameTxt.getText().equals(""))
{
sqlString = "select * from users";
}

show();
try
{
if(nextP)
{
rs.close();
nextP=false;
}
rs = st.executeQuery(sqlString);
nextP=true;
nextRecords();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(sqlString);
show();
}
if(ae.getSource()==next)
{
previous.setEnabled(true);
nextRecords();
show();
}

if(ae.getSource()==previous)
{
next.setEnabled(true);
prevRecords();
show();
}

if(ae.getSource()==reset)
{
userPanel.removeAll();
userIDTxt.setText("");
userNameTxt.setText("");
}
if(ae.getSource()==exit)
{
System.exit(0);
}
}
public static void main(String args[])
{
new UserSearchMod();
}
}
******+
just try this program
and give me feed back
i am facing the pblm with sql server in the same situation even if i use jdk 1.3
help me
bye

Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

the driver you are using does not support the rs.previous(), as well as many other methods. That means that you CAN NOT use them. If you want to use them, you will have to get a new jdbc 2.0 COMPLIANT driver. Otherwise, you will have to deal with not using rs.previous() and other such methods.
Jamie
 
GeeCON Prague 2014
 
subject: Please help me the problem in rs.previous