Java选择查询SQL语法中有错误
我得到以下错误 您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解第1行“日期=”2015-04-26“ORDER BY TraceNum”附近使用的正确语法 这就是问题所在Java选择查询SQL语法中有错误,java,mysql,Java,Mysql,我得到以下错误 您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解第1行“日期=”2015-04-26“ORDER BY TraceNum”附近使用的正确语法 这就是问题所在 "SELECT Name, sporecount.* FROM sporesfungi INNER JOIN sporecount ON sporesfungi.IDSpore = sporecount.IDSpore WHERE Date = ? ORDER BY TraceNum"; 如果删除WHER
"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前面没有空格。根据代码示例判断,WHERE前面需要空格OMG无法相信它这么简单,谢谢你,它成功了!添加为答案,以便正确标记。