473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Database query

Hi
my program consists of a few classes where one is a SEARCH class used for selecting for example from a checkbox that will filter my database. and the RESULT class that displays the jtable.

my problem is in the search class when i select a certain radio button it must set a string.. that string is then send to the result class where it is used in the query. when i click on a radio button it sets correct but as soon as i call it from the result class it becomes null. am i missing something?

the Search class
Expand|Select|Wrap|Line Numbers
  1. String q
  2.  private void yesItemStateChanged(java.awt.event.ItemEvent evt) {
  3.        if (yes.isSelected())
  4.         {
  5.            q = "y";
  6.  
  7.         }
  8.     }
  9. public String getQuery()
  10.      { 
  11.  
  12.       return q; 
  13.      }
the Result class
Expand|Select|Wrap|Line Numbers
  1.  stmt = conn.createStatement();
  2.      Search s = new Search();
  3.      String q = s.getQuery();
  4.  
  5.      PreparedStatement prep = conn.prepareStatement("SELECT * FROM  employee where ee = ?");
  6.       prep.setString(1,q);
  7.  
  8.         rs = prep.executeQuery();
May 6 '08 #1
10 1624
JosAH
11,448 Expert 8TB
So your Result object runs while possibly no button whas pressed? Obviously
variable 'q' doesn't have a non-null value yet. Your program logic is inside out:
the object that receives the button press even (an ActionEvent mayhap) should
'feed' the String value to the Result object; and possibly make it execute your
database query.

kind regards,

Jos
May 6 '08 #2
So your Result object runs while possibly no button whas pressed? Obviously
variable 'q' doesn't have a non-null value yet. Your program logic is inside out:
the object that receives the button press even (an ActionEvent mayhap) should
'feed' the String value to the Result object; and possibly make it execute your
database query.

kind regards,

Jos
thanks for the reply but im not sure i understand exactly what it is i must do?
May 6 '08 #3
BigDaddyLH
1,216 Expert 1GB
Here is your error, in your Result class:

Expand|Select|Wrap|Line Numbers
  1. Search s = new Search();
  2. String q = s.getQuery();
You construct a brand new Search object and then immediately ask it what its query string is -- not surprisingly, it is null.

Solution: don't construct a brand new Search object. Pass to your Result object either the existing Search object (what I assume is having its query string set to "y" correctly) or pass to it the query string itself.
May 6 '08 #4
Here is your error, in your Result class:

Expand|Select|Wrap|Line Numbers
  1. Search s = new Search();
  2. String q = s.getQuery();
You construct a brand new Search object and then immediately ask it what its query string is -- not surprisingly, it is null.

Solution: don't construct a brand new Search object. Pass to your Result object either the existing Search object (what I assume is having its query string set to "y" correctly) or pass to it the query string itself.
Could you please give me an example of how to do it? im still new at java so im not quite sure how to do it..

thanks
May 6 '08 #5
BigDaddyLH
1,216 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. class Search {
  2.     private String query;
  3.  
  4.     public void setQuery(String query) {
  5.         this.query = query;
  6.     }
  7.  
  8.     public String getQuery() {
  9.         return query;
  10.     }
  11. }
  12.  
  13. class Result {
  14.     private Search search;
  15.  
  16.     public void setSearch(Search search) {
  17.         this.search = search;
  18.     }
  19.  
  20.     public Search getSearch() {
  21.         return search;
  22.     }
  23.  
  24.     public void method() {
  25.         String query = getSearch().getQuery();
  26.         System.out.println("method: " + query);
  27.     }
  28. }
  29.  
  30. public class Elsewhere {
  31.     private Search search;
  32.     private Result result;
  33.  
  34.     public Elsewhere() {
  35.         search = new Search();
  36.         result = new Result();
  37.         result.setSearch(search);
  38.     }
  39.  
  40.     public void go() {
  41.         search.setQuery("Hello, world");
  42.         result.method();
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         Elsewhere app = new Elsewhere();
  47.         app.go();
  48.     }
  49. }
In this example, Result knows a Search object and Elswhere did the introduction.
May 6 '08 #6
I tried something else but still have a problem with the query being null..

This is the search class..
Expand|Select|Wrap|Line Numbers
  1. private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
  2.            if (yes.isSelected())  //radio button
  3.        {
  4.          ee = "y";
  5.  
  6.        }
  7.        else if (no.isSelected()) 
  8.        {
  9.          ee = "n";
  10.  
  11.        }
  12.        Results r = new Results(); 
  13.         r.queries(ee);
  14.  
  15.         goToResults();
  16.  
  17.     }                                
result class..

Expand|Select|Wrap|Line Numbers
  1.    stmt = conn.createStatement();
  2.        prep = conn.prepareStatement("SELECT * FROM employee where ee = ?");
  3.  
  4.  
  5.  
  6.          ResultSetMetaData meta = rs.getMetaData();
  7.        cache = new Vector(); 
  8.          row = new Vector();
  9.         while (rs.next()) {
  10.  
  11.         cache.addElement(rs.getString("id"));
  12.         cache.addElement(rs.getString("First_Name"));
  13.         cache.addElement(rs.getString("Last_Name"));
  14.         cache.addElement(rs.getString("Gender"));
  15.         cache.addElement(rs.getString("Age"));
  16.         cache.addElement(rs.getString("Race"));
  17.         cache.addElement(rs.getString("ee"));
  18.         cache.addElement(rs.getString("Telephone"));
  19.         cache.addElement(rs.getString("Previous_Company"));
  20.         cache.addElement(rs.getString("Education"));
  21.         cache.addElement(rs.getString("Expertise"));
  22.         cache.addElement(rs.getString("Email"));
  23.  
  24.         row.addElement(cache);
  25.             colNames = new Vector(12);
  26.             colNames.addElement("id");
  27.             colNames.addElement("First_Name");
  28.             colNames.addElement("Last_Name");
  29.             colNames.addElement("Gender");
  30.             colNames.addElement("Age");
  31.             colNames.addElement("Race");
  32.             colNames.addElement("ee");
  33.             colNames.addElement("Telephone");
  34.             colNames.addElement("Previous_Company");
  35.             colNames.addElement("Education");
  36.             colNames.addElement("Expertise");
  37.             colNames.addElement("Email");
  38.         model = new DefaultTableModel(row, colNames);
  39.         table.setModel(model);
  40.         table.addNotify();
  41.         cache = new Vector();  
  42.         }
  43.  
  44.        }
  45.  
  46.        catch(Exception e)
  47.        {
  48.  
  49.            System.out.println("ERROR QUERY " +e.getMessage());   // i get this error saying query is null
  50.  
  51.        }
  52.        }
  53.  
  54.  
  55.      public void queries(String q)
  56.     {
  57.  
  58.         try {
  59.         System.out.println(q);  //Here the q still has the value obtained in search class
  60.         prep.setString(1,q);      
  61.          rs = prep.executeQuery();
  62.  
  63.     }catch (Exception e)
  64.     {
  65.         System.out.println("Error" );
  66.     }
May 6 '08 #7
BigDaddyLH
1,216 Expert 1GB
I tried something else but still have a problem with the query being null..

This is the search class..
Expand|Select|Wrap|Line Numbers
  1. private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
  2.            if (yes.isSelected())  //radio button
  3.        {
  4.          ee = "y";
  5.  
  6.        }
  7.        else if (no.isSelected()) 
  8.        {
  9.          ee = "n";
  10.  
  11.        }
  12.        Results r = new Results(); 
  13.         r.queries(ee);
  14.  
  15.         goToResults();
  16.  
  17.     }                                
I'm not sure what you are claiming, but if it is that queries is being passed null, you should debug your code. That would seem to imply that both yes and no are not selected. Why not add some print statements to see?
May 6 '08 #8
I'm not sure what you are claiming, but if it is that queries is being passed null, you should debug your code. That would seem to imply that both yes and no are not selected. Why not add some print statements to see?

i put print statements everywhere and it displays the string correct depending on which one i selected. but the moment that this statement:
Expand|Select|Wrap|Line Numbers
  1. prep.setString(1,qq);
is sent to
Expand|Select|Wrap|Line Numbers
  1. prep = conn.prepareStatement("SELECT * FROM employee where ee = ?");
then it becomes null.

i cant understand why that is happening..
May 7 '08 #9
i have tried a few things now and it seems that the problem is with this statement:
Expand|Select|Wrap|Line Numbers
  1. prep.setString(1,q);
because even if insert a "n" in there myself it still doesnt work..

it only does that because it is in another method.. could that be the problem?
is there a specific place where it must be?
May 7 '08 #10
BigDaddyLH
1,216 Expert 1GB
i have tried a few things now and it seems that the problem is with this statement:
Expand|Select|Wrap|Line Numbers
  1. prep.setString(1,q);
because even if insert a "n" in there myself it still doesnt work..

it only does that because it is in another method.. could that be the problem?
is there a specific place where it must be?
It's hard to say, since the complete sequence of code is hard to discern, at least for me.

When you want to execute this query, why not start by creating a new PreparedStatement? I think the problem may be in the way you are trying to reuse a single PreparedStatement.
May 7 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
8
by: RibGnaw | last post by:
Long, long ago when Access 97 first hit the streets a sample database called Northwind was shipped with it. At that time a number of articles had been posted on the correct procedure to remove all...
3
by: Bob Bedford | last post by:
hello I'm looking for some functions or objects allowing to select-insert-update-delete from any table in a mysql database without the need to create a new query every time. Example: ...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
2
by: azmiza | last post by:
Hi everybody, I need your help. I want to view my sql database and its work very well which is display in my web browser but once I want to press button yes, its not working, I check the...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
2
by: basestring | last post by:
Hi I am busy now for many hours without luck I have a database and when I use PHP to add date to it, it works only one time when i want to add the next data, It doesn't work. but i don't get any...
15
by: harvey | last post by:
How do I make PHP create a database for mysql please? I can see how to make tables and I have read all the documents I can find but I don't understand how to make the database itself. All...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.