【发布时间】:2015-05-11 18:52:07
【问题描述】:
我收到以下错误
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'Date = '2015-04-26' ORDER BY TraceNum' 附近使用正确的语法
这是查询
"SELECT Name, sporecount.* FROM sporesfungi INNER JOIN sporecount ON sporesfungi.IDSpore = sporecount.IDSpore WHERE Date = ? ORDER BY TraceNum";
如果我删除 WHERE 子句,查询就可以正常工作。
这是完整的代码
public void exportToExcel(String date) throws IOException, BiffException,
WriteException, SQLException, ParseException{
Db_Connect connection = new Db_Connect();
conn = connection.connect();
String originalFile = "C:\\Users\\Geni\\Desktop\\Book1-Template-new.xls";
date = date.replace("/", "-");
SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd");
try{
Workbook original = Workbook.getWorkbook(new File(originalFile));
WritableWorkbook copy = Workbook.createWorkbook(new File(date+".xls"), original);
java.util.Date format = myDate.parse(date);
java.sql.Date newDate = new java.sql.Date(format.getTime());
String sql = "SELECT Name, sporecount.* FROM sporesfungi "
+ "INNER JOIN sporecount ON sporesfungi.IDSpore = sporecount.IDSpore"
+ "WHERE Date = ? ORDER BY TraceNum";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setDate(1, newDate);
rs = statement.executeQuery();
/*stmt = conn.createStatement();
rs = stmt.executeQuery(sql);*/
WritableSheet sheet = copy.getSheet(0);
WritableCell cell;
String spore;
while(rs.next()){
for(int i = 2; i < 64 ;i++){
cell = sheet.getWritableCell(1,i);
spore = cell.getContents();
if(rs.getString("Name").equals(spore)){
Number l1 = new Number(14-rs.getInt("TraceNum"),i,rs.getInt("Amount")) ;
sheet.addCell(l1);
}
}
}
copy.write();
copy.close();
original.close();
}
catch (BiffException | IOException e) {
}
}
【问题讨论】:
-
从代码示例来看,
WHERE之前需要一个空格 -
Omg 简直不敢相信事情这么简单,谢谢你成功了!
-
添加为答案,以便正确标记。