发生错误:语法错误:意外的令牌y

时间:2015-06-12 20:30:28

标签: mysql json jersey

我不知道为什么我会这样做。下面是一些代码,我尝试从数据库中检索一些信息并将其作为响应发送到映射到某个URL的Jersey资源。我不知道y是指错误是什么:

public String myInformation(String theName){

        String infoQuery = "Select * from bookinfo where name= \'theName\'";
        ResultSet result = null;
        conn = newConnection.dbConnection();

        try
        {   

            preparedStatement = conn.prepareStatement(infoQuery);
            result = preparedStatement.executeQuery(infoQuery);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        StringBuilder information = new StringBuilder();
        try
        {
            if(result != null){
                while(result.next()){

               // Build the string which is returned from this
              // method and sent as json response for a URL of a resource

             I read columns from database and use StringBuilder to store it.                          At the end I convert it to String and pass it to Jersey resource. 
           }

else{
                System.out.println("No result");
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        String someInformation = information.toString();
        return someInformation;
}

在我的资源中:

@GET
    @Path("/allSome/{theName}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getSomeInfo(@PathParam("theName") String theName){

        System.out.println("Name is: "+ theName);
        BookInformation bookInfo = new BookInformation();
        String bookInformation =bookInfo.bookInformation(bookName);

        ResponseBuilder responseBuilder = Response.status(Status.OK);
        responseBuilder.entity(bookInformation);

        Response response = responseBuilder.build();
        return response;
    }

修改

我的方法是返回一个String。在Postman客户端上,我正在从数据库接收数据,但它以字符串形式返回,它们之间没有空格。我想我需要将该字符串转换为JSON,以便我的资源可以将其发送回客户端以便在页面上显示。

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

'y'必须在struct参数中,还有一些引号。您应该使用占位符参数正确使用theName

PreparedStatement

您的代码结构不合理。取决于try块成功的代码应该在try块内。