我有一个表01在MyDatabase01
注意:在netbeans中,id=jTextField1, day=jComboBox1, month=jComboBox2, year=jComboBox3
+=================================+
|id |day |month |year |
+=================================+
|A1 |monday |january |2016 |
|A2 |monday |january |2017 |
|A3 |sunday |february |2016 |
|A4 |sunday |march |2016 |
|A5 |monday |july |2016 |
+=================================+
和,如何选择1月和7月中的一个,其中一天是星期一?这样的
+=========+
|month |
+=========+
|january |
|july |
+=========+
那么,如何选择年份2016 &2017年哪一天是星期一?这样的
+=======+
|year |
+=======+
|2016 |
|2017 |
+=======+
之后,我想查看月和年进入jComboBox java netbeans,这是我的代码
月的源代码
try {
connectDB();
jComboBox2.removeAllItems();
String sql = "SELECT * FROM table01 WHERE day='"+jComboBox1.getSelectedItem()+"' UNION SELECT * FROM table01 WHERE day='"+jComboBox1.getSelectedItem()+"'";
//not work.always show all data, not january and july.
ResultSet res = stat.executeQuery(sql);
while (res.next()) {
String val = res.getString("month");
jComboBox2.addItem(val);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
年份的源代码
try {
connectDB();
jComboBox3.removeAllItems();
String sql = "SELECT * FROM table01 WHERE month='"+jComboBox2.getSelectedItem()+"' AND day='"+jComboBox1.getSelectedItem()+"'";
//not work. always show all data, not 2016 and 2017.
ResultSet res = stat.executeQuery(sql);
while (res.next()) {
String val = res.getString("year");
jComboBox3.addItem(val);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
谢谢你的帮助
尝试如下。它将返回您所期望的。
月的源代码
String sql = "SELECT DISTINCT(month) FROM table01 WHERE day='"+jComboBox1.getSelectedItem()"';
ResultSet res = stat.executeQuery(sql);
while (res.next()) {
String val = res.getString(0);
jComboBox2.addItem(val);
}
年份的源代码
String sql = "SELECT DISTINCT(year) FROM table01 WHERE month='"+jComboBox2.getSelectedItem()+"' AND day='"+jComboBox1.getSelectedItem()+"'";
ResultSet res = stat.executeQuery(sql);
while (res.next()) {
String val = res.getString("0");
jComboBox3.addItem(val);
}
您可以使用以下命令:
select distinct(yourtable.month) from yourtable where UPPER(yourtable.day) = UPPER('monday')