Moving The Cursor To A Designated Row - due on Tuesday

Hey guyz,

I need your help desperately to put this piece together for my project which is due on Wednesday.... so please help if you know what i'm talking about...

OK here is something that has been making me scratch my head... ok i'm trying to use a next and previous button on my interface so that it processes the database row by row and this is all i've been able to come up with...

find.addActionListener (

new ActionListener() {

public void actionPerformed(ActionEvent e)

{

String url = "jdbc:odbc:PP";

Connection con;

Statement stmt;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(java.lang.ClassNotFoundException exc){

System.err.print("ClassNotFoundException: ");

System.err.print("/n"+exc.getMessage());

}

try{

con = DriverManager.getConnection(url);

if(!tf.getText().equals("")){

stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

String query = "select * from HarvestFile where SegmentCode = '" +

tf.getText() + "'";

ResultSet rs = stmt.executeQuery(query);

display (rs);

stmt.close();

}

else

tf.setText("Enter Segment Code");

}

catch(SQLException sqlex){

System.err.println("SQLException: " + sqlex.getMessage());

}

}

public void display (ResultSet rs)

{

try {

rs.next();

int NumberOfTrees = rs.getInt(4);

int NumberRemaining = rs.getInt(5);

double Revenue = rs.getDouble(6);

tf1.setText(rs.getString(1));

tf.setText(rs.getString(2));

tf2.setText(rs.getString(3));

tf3.setText(String.valueOf(NumberOfTrees));

tf5.setText(String.valueOf(NumberRemaining));

tf4.setText(String.valueOf(Revenue));

tf6.setText(rs.getString(7));

tf7.setText(rs.getString(8));

}

catch (SQLException sqlex) {

sqlex.printStackTrace();

}

}

}

);

nx.addActionListener (

new ActionListener() {

public actionPerformed(ActionEvent e)

{

int absolute = 0;

rs.absolute++;

}

}

);

pe.addActionListener (

new ActionListener() {

public actionPerformed(ActionEvent e)

{

int relative = 0;

rs.relative--;

}

}

);

that's it from me now...

thanx

regards

John

[2471 byte] By [SanDMaN] at [2007-9-26 2:22:00]
# 1

Well first off absolute and relative are not properties but APIs so to call them you need this:

rs.absolute(i);

rs.relative(i);

Second of all you need to keep track on what row you are currently located. You can use getRow() API of the ResultSet to do that. But to do that declare your ResultSet as a instance variable of the class...

So the code for your next and pre buttons will look something like this:

nx.addActionListener (

new ActionListener() {

public actionPerformed(ActionEvent e)

{

int absolute = i_rs.getRow();

if (absolute == 0) {

// there is no row in the result set

// do something like displaying the error message

return;

}

rs.absolute(absolute+1);

}

}

);

pe.addActionListener (

new ActionListener() {

public actionPerformed(ActionEvent e)

{

int relative = i_rs.getRow();

if (relative == 0) {

// there is no row in the result set

// do something like displaying the error message

return;

}

rs.relative(relative-1);

}

}

);

There are ton of other ways you can implement this, for one instead of declaring ResultSet as an instance declare int row_num as an instance and increment it in nx buttun and decrement it in pre button events and then use it in the calls to the absolute or relative. I mean there is countless ways to implement this. Good luck

dsklyut at 2007-6-29 9:28:01 > top of Java-index,Archived Forums,Java Programming...
# 2

thanx for the tip...

the good news is... when i compile it i have no errors... but the bad news is... now my find action listener doesn't work... it... doesn't retrieve any information even if i type in the text field...

could you do me a huge favour and look at these codes and see if you know what has gone wrong...

find.addActionListener (

new ActionListener() {

public void actionPerformed(ActionEvent e)

{

String url = "jdbc:odbc:PP";

Connection con;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(java.lang.ClassNotFoundException exc){

System.err.print("ClassNotFoundException: ");

System.err.print("/n"+exc.getMessage());

}

try{

con = DriverManager.getConnection(url);

if(!tf.getText().equals("")){

Statement stmt = con.createStatement();

String query = "select * from HarvestFile where SegmentCode = '" +

tf.getText() + "'";

ResultSet rs = stmt.executeQuery(query);

display (rs);

stmt.close();

}

else

tf.setText("Enter Segment Code");

}

catch(SQLException sqlex){

System.err.println("SQLException: " + sqlex.getMessage());

}

}

public void display (ResultSet rs)

{

try {

rs.next();

int NumberOfTrees = rs.getInt(4);

int NumberRemaining = rs.getInt(5);

double Revenue = rs.getDouble(6);

tf1.setText(rs.getString(1));

tf.setText(rs.getString(2));

tf2.setText(rs.getString(3));

tf3.setText(String.valueOf(NumberOfTrees));

tf5.setText(String.valueOf(NumberRemaining));

tf4.setText(String.valueOf(Revenue));

tf6.setText(rs.getString(7));

tf7.setText(rs.getString(8));

}

catch (SQLException sqlex) {

sqlex.printStackTrace();

}

}

}

);

nx.addActionListener (

new ActionListener () {

public actionPerformed(ActionEvent e)

{

int absolute = i_rs.getRow();

if(absolute == 0){

System.err.println("There is no rows in the result set!");

return;

}

rs.absolute(absolute+1);

}

}

);

pe.addActionListener (

new ActionListener () {

public actionPerformed(ActionEvent e)

{

int relative = i_rs.getRow();

if(relative == 0){

System.err.println("There is no rows in the result set!");

return;

}

rs.relative(relative-1);

}

}

);

cancelButton.addActionListener (

new ActionListener() {

public void actionPerformed(ActionEvent e)

{

HRecordFrame rc = MainFrame.HRecord;

rc.show();

HRecordPanel.vr.dispose();

}

}

);

}

}//End of functi

SanDMaN at 2007-6-29 9:28:01 > top of Java-index,Archived Forums,Java Programming...
# 3

I would venture to say that you declared an instance variable of

java.sql.ResultSet i_rs=null;

or something like that. Well that is good and dandy but you never initialized it.

Change code in the find action listener to this:

try{

con = DriverManager.getConnection(url);

if(!tf.getText().equals("")){

Statement stmt = con.createStatement();

String query = "select * from HarvestFile where SegmentCode = '" +

tf.getText() + "'";

i_rs = stmt.executeQuery(query);

display (i_rs);

stmt.close();

}

This is one thing. I can't think of the reason why something that worked stoped working (Magic, black and white comes to mind), but maybe your DB connection is down. Check if there is some sytem out put on the DOS screen. Good luck

dsklyut at 2007-6-29 9:28:01 > top of Java-index,Archived Forums,Java Programming...