sql在MySQLWorkbench中工作,但在netbeans中抛出MySQLSyntaxErrorException#565

时间:2017-10-26 01:29:06

标签: java mysql mysql-workbench

      public List<StateGalleryAdListing> getGalleryListing(String state, String category) throws SQLException {

    List<StateGalleryAdListing> adListingList = new ArrayList<>();

    try{

        Statement stmt = conn.createStatement();

        String sql = "SELECT * FROM stateGalleryListingList WHERE state =" + state +  "AND category = " + category;

        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()) {

            StateGalleryAdListing GalleryAdListing = new StateGalleryAdListing();

            GalleryAdListing.setState(rs.getString(1));
            GalleryAdListing.setCategory(rs.getString(2));
            GalleryAdListing.setAdLink(rs.getString(3));
            GalleryAdListing.setImageHeaderTxt(rs.getString(4));
            GalleryAdListing.setImageSrc(rs.getString(5));

            adListingList.add(GalleryAdListing);
        }

    } catch(SQLException ex) {

        ex.getSQLState();
    }

    return adListingList;

}

我也试过使用PreparedStatment:

 String sql = "SELECT * FROM stateGalleryListingList Where state = ? AND category = ?";
 PreparedStatement stmt = conn.conn.prepareStatement(sql);

 stmt.setString(1, state);
 stmt.setString(2, category);

 ResultSet rs = stmt.executeQuery();

这条路线仍然不起作用,当我看到错误时,我没有看到任何有用的东西。提前感谢任何有用的建议!

1 个答案:

答案 0 :(得分:0)

我的问题在这里:       ResultSet rs = stmt.executeQuery(sql);

我不需要将sql传递给executeQuery()方法。感谢你们的帮助。