wood burning stoves 2.0*
The moose likes JDBC and the fly likes problem in where clause while fetching records in jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem in where clause while fetching records in jtable" Watch "problem in where clause while fetching records in jtable" New topic
Author

problem in where clause while fetching records in jtable

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
i m sorry if i am posting this questing to wrong forum.
i m trying to fetch data in jtable from database, but when i am adding where clause, my jtable displaying nothing, whereas records are in my database.
here is what i am doing:
Db.java


below is part of my another java class where i m fetching the data:
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196


Run the generated query against your db and see if there are records against it.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
yes my query is working,
when i printing records on console based on query, the are printing..
like in Db.java



but the are not printing in JTable.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

The problem is in the JTable then? Try to populate the table with hard-coded values, so that you can untangle JTable-related and database-related issues.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Punit you are doing so much database stuff, why don't you hava connection class by now?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2

The problem is in the JTable then? Try to populate the table with hard-coded values, so that you can untangle JTable-related and database-related issues.


my table is working with hard coded values.
i mean i am getting data.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2

Punit you are doing so much database stuff, why don't you hava connection class by now?


but my database class is a separate class Db.java
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Punit Jain wrote:my table is working with hard coded values.

If your database stuff works on its own, and JTable stuff works on its now, then the bug must be in the way the two things are put together.

The code you've posted does not show the scrollpane being inserted into a frame. Do you create the JTable with hard-coded values in the same exact way? It might help if you show us that code too.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
no i created two array of string type:



and i putted them into the table like this:

table = new DefaultTableModel(arr, header);


Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

I'd need to see a little bit more of this code, especially the construction of the model, the table, the scrollpane and the frame. Is this identical to the code you use when loading data from the database?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
it's worked, thank you..
i don't know why this creating problem for me:
what i did now is:
part of my code:


earlier i m putting these two lines outside of the actionListener:

and now i put these two lines inside actionListener, and it's work for me now..
but i don't know why it was creating problem earlier...
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Punit Jain wrote:earlier i m putting these two lines outside of the actionListener:

and now i put these two lines inside actionListener, and it's work for me now..
but i don't know why it was creating problem earlier...

It's certainly not the only change you've made to your code. In your original post, you've constructed the data, model, table and scrollpane in the ActionListener and probably didn't insert this whole bunch of controls into the frame (hence nothing was shown). The code you've posted now does not show the creation of these controls, and you seem to be manipulating the table model of an existing table. This is probably a better approach and if it works for you, the better.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
yes, but after done all the changes, when i am putting these two lines


outside the actionListener, still that was not working.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Punit Jain wrote:yes, but after done all the changes, when i am putting these two lines

outside the actionListener, still that was not working.


It's not that surprising, since the Name variable is used on the following line:
so if you don't initialize it, the correct value is not passed to the connect() method. (By the way, variable names usually start with lower case letter in Java, to differentiate them from class and interface names which start with an upper case letter.)

If you want to get help identifying the original bug, you'd need to post the non-working code and description of the problem again. Otherwise I don't know what code are we actually discussing.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
okay..
here is the working code..


and if i put these two lines

anywhere outside the actionListener, this code not works..
this is what i am not understanding, why is this happening??
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Code that you put into the ActionListener.run() method is executed when a user presses the searchButton button. At this moment you store the current value in the textboxes to the Name and RegistrationNumber variables, and use these variables as a criteria to find records in the database.

So far so good. But when you move the two mentioned lines elsewhere, they will get not be executed at the time the user presses the searchButton, but at some other time (depending on where exactly do you move them, which you didn't specify). I presume you move them to the code which gets executed when the form is created, but I may be wrong. In any case, these variables either don't get initialized at all (and vould have a value of null), or get initialized to whatever value was in the textboxes at the moment, probably an empty string. These nulls or empty strings are then passed to the db1.Search(Name,RegistrationNumber) call, which in turn does not find any matching record in the database.

I'd like to add a few other points: your code does not use PreparedStatement. It is therefore subject to SQL injection attacks and probably also implicit conversion bugs.

Secondly, in your original post, there is the Db.connect method. You're using System.out to print diagnostic messages, which is great. However, you print only the Inside db string. If you wrote also the name and possibly registration number which were passed to the procedure, you would be able to spot strange values of the Name parameter much sooner. Logging can be much more useful if you log relevant data as well. Also, you might want use some logging framework which would allow you to configure various logging options, such as the one built in Java or log4j, for example.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2

I'd like to add a few other points: your code does not use PreparedStatement. It is therefore subject to SQL injection attacks and probably also implicit conversion bugs.


thanks i am going to do this..

If you wrote also the name and possibly registration number which were passed to the procedure



Logging can be much more useful if you log relevant data as well. Also, you might want use some logging framework which would allow you to configure various logging options, such as the one built in Java or log4j, for example.

i dont know anything about logging, but i will check out the tutorials...

thank you for your suggestions....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in where clause while fetching records in jtable
 
Similar Threads
JTable & JDBC
problem in button ActionListener.
jtable not refreshing
Help with JTable
JTable type yes/no