Results 1 to 14 of 14
  1. #1
    tiger100plus is offline Member
    Join Date
    Nov 2008
    Location
    Kuala Lumpur,Malaysia
    Posts
    16
    Rep Power
    0

    Default What is the problem....?

    Please refer to the code below.....when i press next,first,last and previous button there is error appearing..."ERROR IN SHOWING DATA".... why...?i copy directly from notes given by lecturer.....please detect the error....thanks


    public void showRecord(String whereToMove){

    try{
    if(whereToMove.equals("First"))
    rsStudent.first();
    if(whereToMove.equals("Last"))
    rsStudent.last();
    if(whereToMove.equals("Previous"))
    rsStudent.previous();
    if(whereToMove.equals("Next"))
    rsStudent.next();

    jtfStudentID.setText(rsStudent.getString(1));
    jtfStudentName.setText(rsStudent.getString(2));
    jtfCourseID.setText(rsStudent.getString("CourseID" ));
    jtfYear.setText(""+rsStudent.getInt(4));
    jtfTutorialGroup.setText(rsStudent.getString(5));

    }catch(SQLException ex){
    JOptionPane.showMessageDialog(null,"Error in showing data...");
    }
    }

    //begining i declare this and same goes to other buttons
    jbtNext.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    showRecord("Next");
    }
    });

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    you are getting an SQLException, more we also cannot say until you change that catch block in such a way as to display it.

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Change your catch block to: System.err.println(ex);

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by carderne View Post
    Change your catch block to: System.err.println(ex);

    Yes do this and, copy-paste your error message here to see.

  5. #5
    tiger100plus is offline Member
    Join Date
    Nov 2008
    Location
    Kuala Lumpur,Malaysia
    Posts
    16
    Rep Power
    0

    Default what is the problem?

    Actually there is no problem with the coding because when i run the program i press next button...it shows error,it is because the data that is display is the last data(last line)...so i shouldn't press the next button....i should press the previous button to see the other records and then oni the next button ...anyway thanks for your help....

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    So what's your question? If you don't have any question what's the point of sending here this thread?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by tiger100plus View Post
    Actually there is no problem with the coding because when i run the program i press next button...it shows error,it is because the data that is display is the last data(last line)...so i shouldn't press the next button....i should press the previous button to see the other records and then oni the next button ...anyway thanks for your help....
    You will only, ever, get that message if an Exception occurs. That does not, necessarily, mean there is a "problem with the code", but an exception is occurring. If you do not want to take the steps necessary to dignose and fix the problem, then please, do not come here and whine about it.

    But considering what you have said here, it sounds as though your code is the problem, as you are probably not checking whether or not "rs.next()" returns true before attempting to read the column data.

    Edit: And now that I look at the code again, that is precisely what is wrong.

  8. #8
    tiger100plus is offline Member
    Join Date
    Nov 2008
    Location
    Kuala Lumpur,Malaysia
    Posts
    16
    Rep Power
    0

    Default

    Based on the coding ,why am i getiing the last record.....when i run the program .....how to retrieve the first row of the record.....?(So that i can use the next button for the first time and not depending on previous button or first button)

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Pushing next the first time should be okay (as long as the query actually returned anything). Those methods are fine, but you need to save the result of the "next/first/last/previous" method in a boolean and place the "get" methods in an if block with a message claiming that there is no more data in that direction in the else block.

    Also, of course, you must be using a Scrollable ResutlSet, and not the default ResultSet type. And, you still need to produce a meaningful error message in your catch block. As already stated, the one you used, is not.

    Of course, this mixing of the GUI code and the data code is a very bad practice, as is performing real actions (which you are doing here when retreiving the data) on the event thread (which is where you are when actionPerformed is called). While those actions are taking place, the GUI is "frozen/unresponsive" as it is the Event Thread that controls that, and you are currently using the event thread for your actions.

  10. #10
    tiger100plus is offline Member
    Join Date
    Nov 2008
    Location
    Kuala Lumpur,Malaysia
    Posts
    16
    Rep Power
    0

    Default

    How to save the result in a boolean method......and where to place the boolean method......u mean in initializeDB method like at the bottom......

    the resultset at the bottom is wrong?
    ("SELECT * FROM Student;");
    What is scrollable ResultSet?


    public void initializeDB(){
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conTar=DriverManager.getConnection("jdbc:odbc:TARC ollegeDS");
    stmtInitialize=conTar.createStatement(ResultSet.TY PE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

    rsStudent=stmtInitialize.executeQuery("SELECT * FROM Student;");

    while(rsStudent.next()){
    jtfStudentID.setText(rsStudent.getString(1));
    jtfStudentName.setText(rsStudent.getString(2));
    jtfCourseID.setText(rsStudent.getString(3));
    jtfYear.setText(""+rsStudent.getInt("YearOfStudies "));
    jtfTutorialGroup.setText(rsStudent.getString(5));
    }
    }catch(Exception ex){
    JOptionPane.showMessageDialog(null,"Error in connecting database");
    }
    }

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    I did not say a "boolean method" I said a boolean. :sigh: Another cut-n-paste

    Java Code:
    public void showRecord(String whereToMove){
      try{
        boolean success = false;
        if(whereToMove.equals("First")) {
          success = rsStudent.first();
        } else if(whereToMove.equals("Last"))
          success = rsStudent.last();
        } else if(whereToMove.equals("Previous"))
          success = rsStudent.previous();
        } else if(whereToMove.equals("Next"))
          success = rsStudent.next();
        }
        if (success) {
          jtfStudentID.setText(rsStudent.getString(1));
          jtfStudentName.setText(rsStudent.getString(2));
          jtfCourseID.setText(rsStudent.getString("CourseID" ));
          jtfYear.setText(""+rsStudent.getInt(4));
          jtfTutorialGroup.setText(rsStudent.getString(5)); 
        } else {
          JOptionPane.showMessageDialog(null,"No more rows in that direction.");
        }
      }catch(SQLException ex){
        JOptionPane.showMessageDialog(null,"Error in showing data..." + ex.getMessage());
      }
    }

    As far as a Scrollable ResultSet goes, see Connection (Java Platform SE 6) - createStatement(int, int)

    And if you don't have one of these already, previous, first, and last won't work anyway.

    Edit: And, if you're using access, I don't know that you can even create scrollable resultsets.
    Last edited by masijade; 12-16-2008 at 06:10 PM.

  12. #12
    tiger100plus is offline Member
    Join Date
    Nov 2008
    Location
    Kuala Lumpur,Malaysia
    Posts
    16
    Rep Power
    0

    Default

    Your code can run the program ....but it doesn't make any difference,i am still receiving the last record from database(access) once i run the program....so i can't use next button...how to solve this....?

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Then make sure that the query is actually returning anything (as I had already intimated in an earlier post). And have you tried getting a Scrollable ResultSet yet?

  14. #14
    tiger100plus is offline Member
    Join Date
    Nov 2008
    Location
    Kuala Lumpur,Malaysia
    Posts
    16
    Rep Power
    0

    Default

    At last...i found the solution.....i change the while loop in the initializeDB(that i have posted earlier)to if loop and it is now returning the first record....and now i can immediately use the next button.....Thank you very much for your solution....i've learnt a lot out of it....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •