[英]SQL syntax error on WHERE clause using Netbeans IDE / Derby
[英]Using SELECT statement with a WHERE clause in Netbeans derby with JFrame forms
我已经制作了一个JFrame表单和一个数据库,该数据库将jTable
数据jTable
到JFrame表单的一个面板中。 当用户在用作搜索栏的jTextField
键入内容时,SEARCH查询将显示整个表,而不仅仅是特定的记录。 我不知道如何在代码中添加WHERE条件。
到目前为止,用户可以在搜索栏中键入任何内容,按搜索按钮并查看数据库中的所有信息。 我尝试使用以下语句:
myDataObj = myStatObj.executeQuery ("Select* from Gabrielle.PlantData where PName = 'search'");
但所有这些显示都是空白表。
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();
}
我使用设计视图创建了数据库,所以我不知道如何显示代码。 该数据库具有以下字段:
ID
(主键), PName
(搜索中涉及的字段), PLevel
PArea
PType
, PWater
PSun
。 除PSun
( INTEGER
外,它们都是VARCHAR
。
我现在要做的是获取用户在jTextField
输入的数据(保存为String搜索),在数据库中搜索该数据,然后仅在用户输入的表中显示单个记录。匹配表中名为PName
的某个字段。
如果有什么太笼统的事,我感到抱歉,这是我第一次使用此网站。 请尝试使用简单的解释,因为我还是编程的新手。 任何帮助表示赞赏。
到目前为止,我非常感谢您提供的答案,但是我对程序进行了很大的更改,以至于无法识别。
我犯的错误是试图在同一程序中管理数据库和GUI。 我现在将程序分为两部分。 有一个称为“后端”的主类,它管理从数据库中读取数据。 然后还有另一个称为“ GUI”的类,用于处理从Backend类获取的信息的显示。
结果,该程序的许多功能已更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.