简体   繁体   中英

Using SELECT statement with a WHERE clause in Netbeans derby with JFrame forms

I have made a JFrame form and a database that feeds data from a jTable into one of the panels of the JFrame form. When the user types in something to the jTextField used as a search bar, the SEARCH query displays the whole table and not just a specific record. I have no idea how to add a WHERE condition to the code.

As of right now, the user can type anything in the search bar, press the search button and view all the information in the database. I tried using the statement :

myDataObj = myStatObj.executeQuery ("Select* from Gabrielle.PlantData where PName = 'search'");

but all this shows is a blank table.

Connection myConObj = null;
Statement myStatObj = null;
ResultSet myDataObj = null;

public WikiPlantGUI() 
{
    initComponents();
    selectionAll();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */

public void selectionAll()
{
   try
   {
    myConObj= DriverManager.getConnection ("jdbc:derby://localhost:1527/InfoDB", "Gabrielle", "plants");
    myStatObj = myConObj.createStatement();
    myDataObj = myStatObj.executeQuery ("Select* from Gabrielle.PlantData where PName = 'search'");
    guiTable.setModel(DbUtils.resultSetToTableModel(myDataObj) );   
   }

   //Then there is some code that creates the JFrame form

    private void mainSearchbtnActionPerformed(java.awt.event.ActionEvent evt) {                                              
    boolean valid = true;
    String search = mainSearch.getText();

    if (search.length() ==0)
    {
        valid = false;
    }

    else
    {
        basePanel.removeAll();
        basePanel.add(dispPanel);
        basePanel.repaint();
        basePanel.revalidate(); 
    }

I created the database using the design view, so I don't know how to show the code. The database has the following fields:

  1. ID (the primary key),
  2. PName (the field involved in the search),
  3. PLevel ,
  4. PArea ,
  5. PType ,
  6. PWater ,
  7. PSun .

All of them are VARCHAR , except for PSun , which is INTEGER .

What I want to do now is to take data that the user has entered in a jTextField (saved as String search), search for that data in a database,and then display only the single record in the table where the data the user has entered matches a certain field in the table called PName .

I apologise if anything is too general, this is my first time using this website. Please try to use simple explanations, because I am also relatively new to programming. Any help is appreciated.

I appreciate the answers so far, but I have changed the program so much that it is unrecognizable.

The mistake I made was trying to manage the database and the GUI in the same program. I have now broken the program up into two pieces. There is the main class called "Backend" which manages reading in data from the database. There is then another class called "GUI" that handles the display of the information that is gotten from the Backend class.

As a result, much of the program's functions have changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM