我有一个包含月份(九月、七月...)的 JComboBox 和一个jTable 数据库中的一个表(账单)包含(账单 ID、日期、产品 ..)
我希望当从 jComboBox 中选择一个值(例如 9 月)时,它会为我提供 9 月的所有产品。
日期格式为2014/May/27 14:31:04
我尝试了这段代码,但它不起作用,因为在 java 中我无法使用:
String sql1 = "select Products from Bills where Date LIKE "?????+jComboBox1.getSelectedItem()+*" ";
(它给出一个错误,我无法编译)
代码:
try
{
Class.forName(driver).newInstance();
Connection con = (Connection)DriverManager.getConnection(url,user,pass);
String sql1 = "select Products from Bills where Date LIKE "?????+jComboBox1.getSelectedItem()+* " ";
PreparedStatement pst = con.prepareStatement(sql1);
ResultSet rs = (ResultSet) pst.executeQuery(sql1);
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
catch( Exception e){
JOptionPane.showMessageDialog(null, e);
}
最佳答案
String sql1 = "select Products from Bills where Date LIKE "?????+jComboBox1.getSelectedItem()+* " ";
为什么会这样??问号是什么?
应该是这样的:
String sql1 = "select Products from Bills where Date LIKE '"+jComboBox1.getSelectedItem()+ "'";
关于java - 从具有日期条件的数据库中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23889802/