我正在制作一个Java程序,它将确定JComboBox
值设置为默认值,以便在每日,每周,每月和每年内选择用户的输入。现在我每月都有一个想法,但我的查询返回一个异常错误。这是我的代码。
Calendar timer = Calendar.getInstance();
timer.getTime();
int month = timer.get(Calendar.MONTH);
int year = timer.get(Calendar.YEAR);
int day = timer.get(Calendar.DAY_OF_YEAR);
int week = timer.get(Calendar.WEEK_OF_YEAR);
String combovalue = comboBox.getSelectedItem().toString();
if(combovalue.equals("Monthly")) {
try {
String sql = "SELECT * FROM sales WHERE date between January and February";
pst = connection.prepareStatement(sql);
pst.setString(1, String.valueOf(day));
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
pst.close();
JOptionPane.showMessageDialog(null, "Monthly" );
}catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1 );
}
答案 0 :(得分:0)
定义Sales.date和/或Try TRUNC(日期)功能。或者使用像
这样的数字值BETWEEN' 01-JAN-2018' AND' 01-FEB-2018'。
请务必插入''标记围绕着约会。
答案 1 :(得分:0)
您需要在数字值Jan之间写日期为'01 -01-2018',2月'02 -01-2018'所以SELECT * FROM sales WHERE date between '01-01-2018' and '02-01-2018'
Calendar timer = Calendar.getInstance();
timer.getTime();
int month = timer.get(Calendar.MONTH);
int year = timer.get(Calendar.YEAR);
int day = timer.get(Calendar.DAY_OF_YEAR);
int week = timer.get(Calendar.WEEK_OF_YEAR);
String combovalue = comboBox.getSelectedItem().toString();
if(combovalue.equals("Monthly")) {
try {
String sql = "SELECT * FROM sales WHERE date between 01-01-2018 and 02-01-2018";
pst = connection.prepareStatement(sql);
pst.setString(1, String.valueOf(day));
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
pst.close();
JOptionPane.showMessageDialog(null, "Monthly" );
}catch (Exception e1) {
JOptionPane.showMessageDialog(null, e1 );
}
答案 2 :(得分:0)
让我们开始......
String sql = "SELECT * FROM sales WHERE date between January and February";
pst = connection.prepareStatement(sql);
pst.setString(1, String.valueOf(day));
sql
一开始就无效,January
和February
应该被引用,但是,因为您尝试使用PreparedStatement
,所以没有参数,所以pst.setString(1, String.valueOf(day));
会失败......我的意思是,参数1
无处不在?
我建议仔细查看Using Prepared Statements
相反,您应该使用 之类的内容 ......
String sql = "SELECT * FROM sales WHERE date between ? and ?";
try (PreparedStatement pst = connection.prepareStatement(sql)) {
pst.setString(1, new java.sql.Date(fromDate.getTime()));
pst.setString(2, new java.sql.Date(toDate.getTime()));
try (ResultSet rs = pst.executeQuery()) {
table.setModel(DbUtils.resultSetToTableModel(rs));
}
JOptionPane.showMessageDialog(null, "Monthly");
} catch (SQLException exp) {
JOptionPane.showMessageDialog(null, ep);
}
这假设sales.date
是实际的日期/时间类型。
有多种方法可以获取开始/结束日期,其中一种更好的方法是使用较新的日期/时间API,而不是使用已失效的Calendar
API,例如... < / p>
LocalDate startDate = LocalDate.of(year, Month.JANUARY, 1);
LocalDate endDate = startDate.plusMonths(1);
然后你需要使用......
pst.setString(1, java.sql.Date.valueOf(startDate));
pst.setString(2, java.sql.Date.valueOf(endDate));
使它们与java.sql.Date
为了证实我没有疯狂,我写了这个简单的测试课......
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.Month;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.h2.Driver");
try (Connection con = DriverManager.getConnection("jdbc:h2:mem:test")) {
con.setAutoCommit(true);
String make = "create table if not exists Test (id bigint identity, value date)";
try (Statement stmt = con.createStatement()) {
stmt.execute(make);
}
try (Statement stmt = con.createStatement()) {
System.out.println(">> Remove " + stmt.executeUpdate("delete from test") + " rows");
}
LocalDate ld = LocalDate.of(2017, Month.DECEMBER, 25);
for (int index = 0; index < 100; index++) {
insert(con, ld);
ld = ld.plusDays(5);
}
try (Statement stmt = con.createStatement()) {
try (ResultSet rs = stmt.executeQuery("select count(*) from test")) {
while (rs.next()) {
int count = rs.getInt(1);
System.out.println("Found " + count + " rows");
}
}
}
// String sql = "select value from test";
// try (PreparedStatement stmt = con.prepareStatement(sql)) {
// try (ResultSet rs = stmt.executeQuery()) {
// while (rs.next()) {
// System.out.println(rs.getObject(1));
// }
// }
// }
LocalDate startDate = LocalDate.of(2018, Month.JANUARY, 1);
LocalDate endDate = startDate.plusMonths(1);
String sql = "select value from test where value between ? and ?";
try (PreparedStatement stmt = con.prepareStatement(sql)) {
stmt.setDate(1, java.sql.Date.valueOf(startDate));
stmt.setDate(2, java.sql.Date.valueOf(endDate));
System.out.println(startDate);
System.out.println(endDate);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
java.sql.Date date = rs.getDate(1);
System.out.println(date);
}
}
}
}
}
public static void insert(Connection con, LocalDate ld) throws SQLException {
String sql = "insert into test (value) values (?)";
try (PreparedStatement stmt = con.prepareStatement(sql)) {
java.sql.Date date = java.sql.Date.valueOf(ld);
stmt.setDate(1, date);
stmt.executeUpdate();
}
}
}
它在内存数据库中使用H2 Database Engine,在2017年12月25日至2019年5月9日期间填充100个日期。
然后执行查询2018年1月1日到2018年2月1日之间的值并打印
2018-01-01
2018-02-01
2018-01-04
2018-01-09
2018-01-14
2018-01-19
2018-01-24
2018-01-29
因此,您需要确保数据库中包含有效值。如果您没有在表中获得任何内容,请首先验证数据库是否实际返回了某些内容,并确保您的查询对您尝试获取的数据有效