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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi i was using JDBC in a progra 1

Status
Not open for further replies.

vkarthik

Programmer
Aug 30, 2001
126
US
hi

i was using JDBC in a program when i got this problem. i had 2 buttons 'next'and 'back' to browse thro' the records in a table. i got the records into a ResultSet object with an sql query. the 'next' button works fine, but the 'back' button gives an exception like this
"The resultset is TYPE_FORWARD_ONLY" something like that. this happens when the line
rs.previous() is encountered. (same thing happens for methods like rs.first()).
i tried to use a method setFetchDirection() of the resultset class but that failed too. how do i move back in the resultset? plz help.
i vote for any answer i find useful.

luv
Karthik.
 
Hi there - if you check out the Java docs on the java.sql.ResultSet interface (which any JDBC 2 driver must implement) there's the following specification :

"New methods in the JDBC 2.0 API make it possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.

Statement stmt = con.createStatement (
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");

// rs will be scrollable, will not show changes made by others, and will be updatable
"

There's a of bunch of static constants in the class that can be used in the Connection.createStatement(} method to define the nature of the ResultSet. Hope that helps. RjB.
 
I don't know if this solution is the perfect answer....but once you browse through a record set I think you cannot go back to it....The best option would be to get all the records and save them in a vector or something and then browse the vector..... Thats what I had done....

Hope it helps

Sharad
 
Yes, you can browse fowards and backwards thru a ResultSet w/ JDBC2 using ResultSet.TYPE_SCROLL_INSENSITIVE in the Connection.createStatement method.

Sharad's approach is fine if you want to make a local copy of the data for cursor query performance (if you have a slow JDBC connection over a WAN) but it has the following disadvantages
[1] Extra coding effort
[2] As you have cached a local copy of the data you cannot make use of ResultSet.TYPE_SCROLL_SENSITIVE in the Connection.createStatement method to make your result set sensitive to changes made by others (which can be very important for dynamic data in a multiuser DBMS).

RjB.
 
thanx all. will try all ur methods and see if it works. thanx again.

luv
Karthik.
 
Hey mate that's a stupid problem you've got there! :)
The only thing you have to do is to specify your resultset as TYPE_SCROLL_INSENSITIVE so that your cursor may fool around in any direction. I had the same problem when I first started Java blaming it to be some geeky stuff, but then I discovered the scroll insensitive thing.
Try that ok?

Take care mate!
 
(May be u r looking for this vkarthik SIR)
/* FIRST PREVIOUS NEXT LAST
________________________ */

TRY USING THIS CODE

import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;


public class JDBC1 extends Frame implements ActionListener
{
Label l1,l2,l3,l4;
Button b1,b2,b3,b4;
TextField t1,t2,t3;
String drive;
Connection c;
String du;
String sql;
int max=0,p1;
int i=1;
String arr1[][] = new String[100][4];


public JDBC1()
{
setLayout(null);
l1=new Label("QUERY FORM");
l2=new Label("Ecode");
l3=new Label("Name");
l4=new Label("Salary");
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
b1=new Button("first");
b2=new Button("previous");
b3=new Button("next");
b4=new Button("last");
l1.setBounds(200,30,100,40);
l2.setBounds(20,80,150,20);
l3.setBounds(20,120,150,20);
l4.setBounds(20,160,150,20);
t1.setBounds(200,80,250,20);
t2.setBounds(200,120,250,20);
t3.setBounds(200,160,250,20);
b1.setBounds(150,470,100,40);
b2.setBounds(250,470,100,40);
b3.setBounds(350,470,100,40);
b4.setBounds(450,470,100,40);
this.add(l1);
this.add(l2);
this.add(t1);
this.add(l3);
this.add(t2);
this.add(l4);
this.add(t3);
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);

try{
drive="sun.jdbc.odbc.JdbcOdbcDriver";
Driver dr=(Driver)Class.forName(drive).newInstance();
Properties p=new Properties();
/* fa1 is the name of the dsn created in control panel 32odbc by using database of access2000*/
du="jdbc:eek:dbc:fa1";
c=dr.connect(du,p);
Statement stmt=c.createStatement();
String s ="select * from Table2";
ResultSet result=stmt.executeQuery(s);
this.setSize(300,300);
this.show();
while(result.next())
{
arr1[1]=result.getString(1);
arr1[2]=result.getString(2);
arr1[3]=result.getString(3);
i++;
max=i-1;
}
}catch(SQLException e)
{
System.out.println("Error" + e);
} catch(Exception ee)
{
System.out.println("Error "+ ee);
}
for(int i=0;i<=max;i++)
{
for(int j=0;j<=3;j++)
{
System.out.println(arr1[j]);

}
}
}



public void actionPerformed(ActionEvent e)
{

// FIRST

if(e.getSource()==b1)
{

t1.setText(arr1[1][1]);
t2.setText(arr1[1][2]);
t3.setText(arr1[1][3]);
i=2;
}


//NEXT

if(e.getSource()==b3)
{
if(i<=max && i >=1)
{
t1.setText(arr1[1]);
t2.setText(arr1[2]);
t3.setText(arr1[3]);

p1=i-1;i++;
}
}

//PREVIOUS

if(e.getSource()==b2)
{
if(p1<=max && p1 >=1)
{
t1.setText(arr1[p1][1]);
t2.setText(arr1[p1][2]);
t3.setText(arr1[p1][3]);
p1--;
i=p1;
}
}

// LAST

if(e.getSource()==b4)
{

t1.setText(arr1[max][1]);
t2.setText(arr1[max][2]);
t3.setText(arr1[max][3]);
p1=max-1;

}

}


public static void main(String ar[])
{

JDBC1 J= new JDBC1();


}}

FROM

RISHU CHOPRA


 
thanx sharad, ur method works nicely. i've voted 4 ur tip. bonscott im yet to try ur idea. thanx rishu 4 takin the pain of writing so much code. thanx again.

luv
Karthik.
Pearls and Diamonds are not found on surfaces.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top