Java SELECT 查询 您的 SQL 语法有错误

标签 java mysql

我收到以下错误

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 = '2015-04-26' ORDER BY TraceNum' at line 1

这是查询

"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 之前没有空格。

关于Java SELECT 查询 您的 SQL 语法有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175414/

相关文章:

java - 将 Map<String,String> 转换为 Map<Long,Long>

java - Android HTTP POST 请求错误 - 套接字失败 EACCES(权限被拒绝)

java - 组合枚举并使用 getter 返回指定的枚举

mysql - 如何克服MySQL 'Subquery returns more than 1 row'错误并选择所有相关记录

java - JAVA中如何查看mysql更新语句的返回值

java - 如何将 PrimeFaces SelectOneMenu ItemLabel 和 ItemValue 与列表一起使用?

java - 在 Swing 应用程序启动期间,首次调用 JFrame 构造函数需要很长时间(因为 java.awt.Window())

mysql - 哪些工具可用于记录遗留数据库模式(PDF、DOC、HTML、RTF)

javascript - 获取 connection.release 不是 Node 中的函数错误

PHP/MYSQL 检查数据库的枚举值?