使用Like运算符的netbeans sql语句

时间:2014-02-05 20:01:15

标签: netbeans-7

我试图从Jcombobox传递值,我使用Like Operator来从数据库中检索数据。 我需要从组合框中检索与所选月份相关的所有数据

String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '?%'";
try {
    ps = con.prepareStatement(sql);
    ps.setString(1, cmb.getSelectedItem().toString());
    rs = ps.executeQuery();
    table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage());
}

2 个答案:

答案 0 :(得分:0)

String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '"+cmb.getText().toString()+"%'";

使用此格式。 这将有助于更多...

答案 1 :(得分:-1)

尝试将其直接放在?标记符号中,如此

String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '"+cmb.getSelectedItem().toString()+"%'";

try {
    ps = con.prepareStatement(sql);
    rs = ps.executeQuery();
    table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage());
}