我在Java NetBeans中遇到了一些高级搜索问题。该特定区域的代码不是由我制作的,而是我试图纠正它。 clieninput
,productinput
和truckinput
无效。他们找不到任何东西,或者只展示一些记录。
其他2个工作正常,一个只是数字,另一个是日期,是否与任何事情有关?
该部分的代码是:
private void generarButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
model.getRows().clear();
String sql = "SELECT id_scale_load, ticket_number, customer_code, customer_name, product_code, " +
"product_name, truck_code, truck_name, tagid, tare, gross, (gross - tare) " +
"AS net, ash, btu, mustuire, sulfur, fin FROM scale_load " +
"WHERE (ticket_number = ? OR ? = true)AND(customer_name LIKE ? OR ? = true) " +
"AND (date(fin) = ? OR ? = true) AND(product_name LIKE ? OR ? = true) " +
"AND(truck_code LIKE ? OR > ? = true) ";
pst = conn.prepareStatement(sql);
if (!ticketInput.getText().isEmpty()) {
pst.setString(1, ticketInput.getText());
pst.setBoolean(2, false);
} else {
pst.setString(1, "");
pst.setBoolean(2, true);
}
if (!clientInput.getText().isEmpty()) {
pst.setString(3, "'" + clientInput.getText() + "'");
pst.setBoolean(4, false);
} else {
pst.setString(3, "");
pst.setBoolean(4, true);
}
if (dateInput.getDate() != null) {
pst.setDate(5, new java.sql.Date(dateInput.getDate().getTime()));
pst.setBoolean(6, false);
} else {
pst.setString(5, null);
pst.setBoolean(6, true);
}
if (!productInput.getText().isEmpty()) {
pst.setString(7, "'" + productInput.getText() + "%'");
pst.setBoolean(8, false);
} else {
pst.setString(7, "");
pst.setBoolean(8, true);
}
if (!truckInput.getText().isEmpty()) {
pst.setString(9, "'" + truckInput.getText() + "%'");
pst.setBoolean(10, false);
} else {
pst.setString(9, "");
pst.setBoolean(10, true);
}
rs = pst.executeQuery();
printButton.setEnabled(true);
ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
Ticket row = new Ticket();
row.setNo(rs.getInt(1));
row.setTicket(rs.getInt(2));
row.setCustomerCode(rs.getString(3));
row.setCustomerName(rs.getString(4));
row.setProductCode(rs.getString(5));
row.setProductName(rs.getString(6));
row.setTruckCode(rs.getString(7));
row.setTruckName(rs.getString(8));
row.setTagID(rs.getString(9));
row.setGross(rs.getInt(10));
row.setTare(rs.getInt(11));
row.setNet(rs.getInt(12));
row.setAsh(rs.getDouble(13));
row.setBtu(rs.getInt(14));
row.setMustuire(rs.getDouble(15));
row.setSulfure(rs.getDouble(16));
row.setDate(new Date(rs.getDate(17).getTime()));
model.addRow(row);
}
model.refresh();
//ticketInput.setText("");
printTicketButton.setEnabled(false);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
代码中有错误吗?
答案 0 :(得分:0)
您没有提供太多信息,但看起来有些错误:当您提供查询参数时,您会单引号,但是当您没有提供参数时,您会使用"&# 34;,我认为这会导致SQL语法错误
带有提供值的版本将创建一个如下所示的(正确)行:
(product_name LIKE 'Product' OR false = true)
vs不提供应该创建这样一行的行:
(product_name LIKE OR true = true)
^ SQL Wants something here, even if true=true!
难道第二个问题不是吗?尝试将其默认为" ' ' "而不是""得到:
(product_name LIKE '' OR true = true)