Java SQL语句在SQLItemManager中工作,但在我的程序中不工作
我试图从一个现有表中获取相关值,并将它们放入一个虚拟表中,然后在其中搜索并获得所需的条目 除了通过程序将条目实际加载到这个虚拟表中之外,我的一切或多或少都在工作。我已经在SqlItemManager中尽我最大的能力测试了这些语句,它们似乎没有问题,但是当我试图通过程序执行它们时,什么都没有发生,Java SQL语句在SQLItemManager中工作,但在我的程序中不工作,java,sqlite,javafx,sqlitemanager,fts5,Java,Sqlite,Javafx,Sqlitemanager,Fts5,我试图从一个现有表中获取相关值,并将它们放入一个虚拟表中,然后在其中搜索并获得所需的条目 除了通过程序将条目实际加载到这个虚拟表中之外,我的一切或多或少都在工作。我已经在SqlItemManager中尽我最大的能力测试了这些语句,它们似乎没有问题,但是当我试图通过程序执行它们时,什么都没有发生,stmt.execute()返回false 有关守则: public void getResults() { String sqlGetData = "INSERT INTO VirtualPr
stmt.execute()
返回false
有关守则:
public void getResults() {
String sqlGetData = "INSERT INTO VirtualProductTable SELECT * FROM
H4HProductTable WHERE id = ? AND price <= ?" +
" AND length <= ? AND width <= ? AND height <= ?";
switch (((Categories)this.categorySelect.getValue()).toString()) {
case "Furniture":
sqlGetData = sqlGetData.concat(" AND isFurniture = 1");
break;
case "Appliances":
sqlGetData = sqlGetData.concat(" AND isAppliance = 1");
break;
case "Building_Materials":
sqlGetData = sqlGetData.concat(" AND isBuildingMaterial = 1");
break;
case "Tools":
sqlGetData = sqlGetData.concat(" AND isTool = 1");
break;
default:
break;
}
// corrects for empty/unimportant fields //
try {
Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sqlGetData);
if(this.idSearch.getText().equals(null) ||
this.idSearch.getText().isEmpty()) {
stmt.setString(1, "id");
} else {
stmt.setString(1, this.idSearch.getText());
}
if(this.priceSearch.getText().equals(null) ||
this.priceSearch.getText().isEmpty()) {
stmt.setString(2, "price");
} else {
stmt.setString(2, this.priceSearch.getText());
}
if(this.lengthSearch.getText().equals(null) ||
this.lengthSearch.getText().isEmpty()) {
stmt.setString(3, "length");
} else {
stmt.setString(3, this.lengthSearch.getText());
}
if(this.widthSearch.getText().equals(null) ||
this.widthSearch.getText().isEmpty()) {
stmt.setString(4, "width");
} else {
stmt.setString(4, this.widthSearch.getText());
}
if(this.heightSearch.getText().equals(null) ||
this.heightSearch.getText().isEmpty()) {
stmt.setString(5, "height");
} else {
stmt.setString(5, this.heightSearch.getText());
}
if(!stmt.execute()) {
System.out.println("insert statement not executed");
}
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
现在,我知道我的程序至少连接到了我的数据库,因为“DELETE FROM VirtualProductTable”语句工作得很好(我已经在有问题的表中手动输入了项,对它进行了测试)
我可能认为导致此问题的另一个原因是我的stmt.setString(…)
s工作不正常,但我不知道如何正确测试它们并查看实际使用的值
我应该说清楚,我没有收到来自eclipse的任何实际错误消息,因此我认为我在语法上是明智的;您应该真正使用一些东西来清理查询参数的输入(或者使用真正的参数化查询与字符串替换)。另外,VirtualProductTable的使用意味着在任何给定时间只有use用户可以这样做。为什么要把它放在一个表中,而不是仅仅返回SELECT的结果呢?嗨,Joe,如果我说了一些毫无意义的话,请原谅,这实际上是一个更大的学校项目的一部分,我仍在努力学习更多关于SQL的知识。虚拟表的用途是能够使用FTS5通过秩进行排序(这在showFirstSix()函数中使用,但谢天谢地,它实际上工作得相当好,尽管一开始很难弄清楚)。至于SQL注入的内容,这是我稍后要解决的问题,但是我正试图首先让实际的框架工作起来。
PreparedStatement.execute()
的结果显示底层SQL查询是返回ResultSet
还是更新计数。它不显示是否执行了实际语句。您是否在insert后检查了表,或者刚刚检查了stmt.execute()
?根据您对PreparedStatement
的使用情况,我假设您的“price”、“length”、“width”和“height”值属于VARCHAR
(或SQLite中的TEXT
)数据类型。但是,这些列的名称可能表示某种基于数字的数据类型。例如,当没有输入时,您可以使用类似于stmt.setString(2,“price”)
的内容。这意味着查询的一部分将是price假设price是一个十进制数(我相信SQLite调用这个数据类型REAL
),那么正确的方法是stmt.setDouble(2,0.0)
,其中0.0
是希望price小于或等于的任何值。最后,与此查询的价格相关的条件如下:price
public void searchProduct (ActionEvent event) throws Exception {
// makes sure we are not just piling entry after entry every consecutive search //
String drop = "DELETE FROM VirtualProductTable";
PreparedStatement stmt = this.newSearchModel.connection.prepareStatement(drop);
stmt.execute();
stmt.close();
// 1. Creates a virtual table and inserts all entries of the category we're looking for, that are also within our other criteria
// 2. Sort that table into a list and print out the fields we need row by row
getResults();
showFirstSix();
}