Results 1 to 14 of 14
Thread: What is the problem....?
- 12-13-2008, 05:52 PM #1
Member
- Join Date
- Nov 2008
- Location
- Kuala Lumpur,Malaysia
- Posts
- 16
- Rep Power
- 0
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");
}
});
- 12-13-2008, 07:14 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
you are getting an SQLException, more we also cannot say until you change that catch block in such a way as to display it.
- 12-13-2008, 10:35 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Change your catch block to: System.err.println(ex);
- 12-14-2008, 06:51 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-14-2008, 07:01 PM #5
Member
- Join Date
- Nov 2008
- Location
- Kuala Lumpur,Malaysia
- Posts
- 16
- Rep Power
- 0
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....
- 12-15-2008, 05:02 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So what's your question? If you don't have any question what's the point of sending here this thread?
- 12-15-2008, 08:21 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 12-15-2008, 07:29 PM #8
Member
- Join Date
- Nov 2008
- Location
- Kuala Lumpur,Malaysia
- Posts
- 16
- Rep Power
- 0
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)
- 12-16-2008, 07:52 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 12-16-2008, 04:39 PM #10
Member
- Join Date
- Nov 2008
- Location
- Kuala Lumpur,Malaysia
- Posts
- 16
- Rep Power
- 0
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");
}
}
- 12-16-2008, 06:04 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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-17-2008, 05:58 PM #12
Member
- Join Date
- Nov 2008
- Location
- Kuala Lumpur,Malaysia
- Posts
- 16
- Rep Power
- 0
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....?
- 12-17-2008, 07:42 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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?
- 12-19-2008, 02:03 PM #14
Member
- Join Date
- Nov 2008
- Location
- Kuala Lumpur,Malaysia
- Posts
- 16
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks