Database query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elaine121
    New Member
    • Aug 2007
    • 23

    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
    Code:
    String q
     private void yesItemStateChanged(java.awt.event.ItemEvent evt) {
           if (yes.isSelected())
            {
               q = "y";
               
            }
        }
    public String getQuery()
         { 
           
          return q; 
         }
    the Result class
    Code:
     stmt = conn.createStatement();
         Search s = new Search();
         String q = s.getQuery();
        
         PreparedStatement prep = conn.prepareStatement("SELECT * FROM  employee where ee = ?");
          prep.setString(1,q);
         
            rs = prep.executeQuery();
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #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

    Comment

    • Elaine121
      New Member
      • Aug 2007
      • 23

      #3
      Originally posted by JosAH
      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?

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Here is your error, in your Result class:

        [CODE=Java]Search s = new Search();
        String q = s.getQuery();[/CODE]

        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.

        Comment

        • Elaine121
          New Member
          • Aug 2007
          • 23

          #5
          Originally posted by BigDaddyLH
          Here is your error, in your Result class:

          [CODE=Java]Search s = new Search();
          String q = s.getQuery();[/CODE]

          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

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            [CODE=Java]class Search {
            private String query;

            public void setQuery(String query) {
            this.query = query;
            }

            public String getQuery() {
            return query;
            }
            }

            class Result {
            private Search search;

            public void setSearch(Searc h search) {
            this.search = search;
            }

            public Search getSearch() {
            return search;
            }

            public void method() {
            String query = getSearch().get Query();
            System.out.prin tln("method: " + query);
            }
            }

            public class Elsewhere {
            private Search search;
            private Result result;

            public Elsewhere() {
            search = new Search();
            result = new Result();
            result.setSearc h(search);
            }

            public void go() {
            search.setQuery ("Hello, world");
            result.method() ;
            }

            public static void main(String[] args) {
            Elsewhere app = new Elsewhere();
            app.go();
            }
            }[/CODE]
            In this example, Result knows a Search object and Elswhere did the introduction.

            Comment

            • Elaine121
              New Member
              • Aug 2007
              • 23

              #7
              I tried something else but still have a problem with the query being null..

              This is the search class..
              Code:
              private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
                         if (yes.isSelected())  //radio button
                     {
                       ee = "y";
                      
                     }
                     else if (no.isSelected()) 
                     {
                       ee = "n";
                       
                     }
                     Results r = new Results(); 
                      r.queries(ee);
                      
                      goToResults();
                    
                  }
              result class..

              Code:
                 stmt = conn.createStatement();
                     prep = conn.prepareStatement("SELECT * FROM employee where ee = ?");
                    
                      
                     
                       ResultSetMetaData meta = rs.getMetaData();
                     cache = new Vector(); 
                       row = new Vector();
                      while (rs.next()) {
                              
                      cache.addElement(rs.getString("id"));
                      cache.addElement(rs.getString("First_Name"));
                      cache.addElement(rs.getString("Last_Name"));
                      cache.addElement(rs.getString("Gender"));
                      cache.addElement(rs.getString("Age"));
                      cache.addElement(rs.getString("Race"));
                      cache.addElement(rs.getString("ee"));
                      cache.addElement(rs.getString("Telephone"));
                      cache.addElement(rs.getString("Previous_Company"));
                      cache.addElement(rs.getString("Education"));
                      cache.addElement(rs.getString("Expertise"));
                      cache.addElement(rs.getString("Email"));
                      
                      row.addElement(cache);
                          colNames = new Vector(12);
                          colNames.addElement("id");
                          colNames.addElement("First_Name");
                          colNames.addElement("Last_Name");
                          colNames.addElement("Gender");
                          colNames.addElement("Age");
                          colNames.addElement("Race");
                          colNames.addElement("ee");
                          colNames.addElement("Telephone");
                          colNames.addElement("Previous_Company");
                          colNames.addElement("Education");
                          colNames.addElement("Expertise");
                          colNames.addElement("Email");
                      model = new DefaultTableModel(row, colNames);
                      table.setModel(model);
                      table.addNotify();
                      cache = new Vector();  
                      }
                   
                     }
                     
                     catch(Exception e)
                     {
                          
                         System.out.println("ERROR QUERY " +e.getMessage());   // i get this error saying query is null
                         
                     }
                     }
                   
                      
                   public void queries(String q)
                  {
                              
                      try {
                      System.out.println(q);  //Here the q still has the value obtained in search class
                      prep.setString(1,q);      
                       rs = prep.executeQuery();
                     
                  }catch (Exception e)
                  {
                      System.out.println("Error" );
                  }

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by Elaine121
                I tried something else but still have a problem with the query being null..

                This is the search class..
                Code:
                private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
                           if (yes.isSelected())  //radio button
                       {
                         ee = "y";
                        
                       }
                       else if (no.isSelected()) 
                       {
                         ee = "n";
                         
                       }
                       Results r = new Results(); 
                        r.queries(ee);
                        
                        goToResults();
                      
                    }
                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?

                Comment

                • Elaine121
                  New Member
                  • Aug 2007
                  • 23

                  #9
                  Originally posted by BigDaddyLH
                  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:
                  Code:
                  prep.setString(1,qq);
                  is sent to
                  Code:
                  prep = conn.prepareStatement("SELECT * FROM employee where ee = ?");
                  then it becomes null.

                  i cant understand why that is happening..

                  Comment

                  • Elaine121
                    New Member
                    • Aug 2007
                    • 23

                    #10
                    i have tried a few things now and it seems that the problem is with this statement:
                    Code:
                    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?

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by Elaine121
                      i have tried a few things now and it seems that the problem is with this statement:
                      Code:
                      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 PreparedStateme nt? I think the problem may be in the way you are trying to reuse a single PreparedStateme nt.

                      Comment

                      Working...