使用Java和Mysql获取异常com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

时间:2016-11-26 09:56:45

标签: java mysql jdbc

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在'?'附近使用正确的语法在第1行获得此异常

        private String selectCustDetail= "SELECT NAME, PASSWORD, RESID, ACTIVATED, USERTYPE FROM USERDETAIL WHERE MOBILENO=?";

    ResultSet rs;
        //PreparedStatement preparedStatement = null;
        PreparedStatement preparedStatement = null ;
        try {
        //  preparedStatement = con.prepareStatement(selectCustDetail);
 preparedStatement = con.prepareStatement(selectCustDetail);

            preparedStatement.setString(1, mobileno.toString().trim());

         rs = preparedStatement.executeQuery(selectCustDetail );
        while (rs.next()) {
        //  LOGGER.info("fetching - 1" + mobileno);
            System.out.println(rs.getString("name"));
            userDetai.setName(rs.getString("name"));
            userDetai.setPassword(rs.getString("password"));
            userDetai.setRestaurant(rs.getString("resId"));
            userDetai.setActivated(rs.getString("activated"));
            userDetai.setType(rs.getString("userType"));
        //  LOGGER.info("fetching - 2" + userDetai.getActivated());


        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //LOGGER.info("Exception - " + e.getMessage());
            dinepostcons.msg = dinepostcons.dbError;
            return false;
        }
        finally
        {try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }

1 个答案:

答案 0 :(得分:4)

更改

rs = preparedStatement.executeQuery(selectCustDetail );

rs = preparedStatement.executeQuery();

您希望使用绑定参数调用preparedStatement的方法。不是来自Statement的方法,它使String没有绑定参数。