【发布时间】:2015-04-07 23:53:04
【问题描述】:
我是 MySql 数据库的新手,目前面临日期格式的问题。我从互联网上尝试了一些解决方案,但没有任何效果。因此,我在这里发布问题。
我有一个 GUI,我在其中提供日期作为输入,格式为“mm/dd/yyyy”。
这是我的包含查询的代码:
public boolean fetchAPReportDomino(Date edDate, APReport apReport ){
Statement stmt=null;
ResultSet rs=null;
SimpleDateFormat sd= new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat sd1=new SimpleDateFormat("yyyy-MM-dd");
String tempDate=sd.format(edDate);
if(getConnection()!=null){
try {
stmt= getConnection().createStatement();
String query="SELECT * FROM AP_Report where Edition Date = " + tempDate;
rs = stmt.executeQuery(query);
while (rs.next()) {
apReport.setRoomHumidity(rs.getInt("Room Humidity"));
apReport.setRoomTemperature(rs.getInt("Room Temperature"));
}
rs.close();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(DataService.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
return true;
}
在 MySql 工作台中,我尝试将日期字段更改为“DATE”、“TIMESTAMP”、“DATETIME”数据类型。但没有任何帮助。
以下是我执行代码时看到的错误:
SEVERE: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Date = 02/14/2015' at line 1
下面的表结构截图:
http://prntscr.com/6qz5my
【问题讨论】:
-
了解prepared statements,并将Date作为java.sql.Date类型的参数传递。不是通过连接字符串:docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
-
另外,你在数据库中的日期字段不能是
Edition Date(带空格),除非它是用反引号创建的,所以你必须使用它! -
能否提供表结构?
-
粘贴表格结构的屏幕:prntscr.com/6qz5my