简体   繁体   English

在具有JFrame形式的NetBeans derby中将SELECT语句与WHERE子句一起使用

[英]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. 我已经制作了一个JFrame表单和一个数据库,该数据库将jTable数据jTable到JFrame表单的一个面板中。 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. 当用户在用作搜索栏的jTextField键入内容时,SEARCH查询将显示整个表,而不仅仅是特定的记录。 I have no idea how to add a WHERE condition to the code. 我不知道如何在代码中添加WHERE条件。

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), ID (主键),
  2. PName (the field involved in the search), PName (搜索中涉及的字段),
  3. PLevel , PLevel
  4. PArea , PArea
  5. PType , PType
  6. PWater , PWater
  7. PSun . PSun

All of them are VARCHAR , except for PSun , which is INTEGER . PSunINTEGER外,它们都是VARCHAR

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 . 我现在要做的是获取用户在jTextField输入的数据(保存为String搜索),在数据库中搜索该数据,然后仅在用户输入的表中显示单个记录。匹配表中名为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. 我犯的错误是试图在同一程序中管理数据库和GUI。 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. 然后还有另一个称为“ GUI”的类,用于处理从Backend类获取的信息的显示。

As a result, much of the program's functions have changed. 结果,该程序的许多功能已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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