这是我正在尝试做的事情(现在已经超过一天了 :( 用户单击书名的链接,我读到了该书的名称。然后我获取该书名并向其发出 Ajax 请求Jersey 资源。在该 Jersey 资源中,我调用 POJO 类中的一个方法,其中一个方法与数据库交互并获取要发送回 Jersey 资源的数据。我遇到了很多错误,但我已经能够修复它们一次。目前我遇到的错误是:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“?”附近使用的正确语法在第 1 行
这是我的 JavaScript 代码:
function dealWithData(nameOfBook){
var bookName = encodeURI(nameOfBook);
console.log("http://localhost:8080/library/rest/books/allBooks/"+bookName);
var requestData = {
"contentType": "application/json",
"dataType": "text",
"type": "GET",
"url": "http://localhost:8080/library/rest/books/allBooks/"+bookName
**//beforeSend has been added as an edit to original code**
beforeSend: function (jqXHR, settings) {
var theUrlBeingSent = settings.url;
alert(theUrlBeingSent);
}
};
var request = $.ajax(requestData);
request.success(function(data) {
alert("Success!!");
});
request.fail(function(jqXHR, status, errorMessage) {
if((errorMessage = $.trim(errorMessage)) === "") {
alert("An unspecified error occurred. Check the server error log for details.");
}
else {
alert("An error occurred: " + errorMessage);
}
});
}
出于某种原因,在上面的代码中,console.log 行显示 url 中的空格被编码为 %20,而在变量“requestData”中,url 没有该编码。我无法理解为什么。
这是我的资源的代码:
@GET
@Path("/allBooks/{bookName}")
@Produces(MediaType.APPLICATION_JSON)
public Response getBook(@PathParam("bookName") String bookName){
System.out.println("Book name is: "+ bookName);
BookInformation bookInfo = new BookInformation();
String bookInformation =bookInfo.bookInformation(bookName);
ResponseBuilder responseBuilder = Response.status(Status.OK);
responseBuilder.entity(bookInformation);
Response response = responseBuilder.build();
return response;
}
这里是本书的信息方法:
public String bookInformation(String bookName){
String infoQuery = "Select * from bookinfo where name = ?";
ResultSet result = null;
conn = newConnection.dbConnection();
try
{
preparedStatement = conn.prepareStatement(infoQuery);
preparedStatement.setString(1, bookName);
result = preparedStatement.executeQuery(infoQuery);
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
if(result != null){
while(result.next()){
availability = result.getString("availability");
isbn = result.getInt("isbn");
hardback = result.getString("hardback");
paperback = result.getString("paperback");
name = result.getString("name");
}
}
else{
System.out.println("No result set obtained");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
//I will build this String using a String builder which I will return
String finalBookInformation = information.toString();
return finalBookInformation;
}
之前,在 dataType 中,我有 json,它抛出了一个不同的错误,但我意识到我没有构建 json,所以我将 dataType 更改为 text,并且该错误消失了。我的参数化查询不执行。如果我尝试对数据库中的值进行硬编码,它可以正常工作,但当我使用准备好的语句时则不行。我最终想返回 JSON,但现在我只想它能工作。任何帮助将不胜感激。我尝试过研究并尽我所能,但没有成功。是编码引起的问题吗?这是我的 Ajax 调用吗?任何帮助表示赞赏。谢谢。
最佳答案
问题似乎出在您的数据库查询执行中,请替换代码
preparedStatement = conn.prepareStatement(infoQuery);
preparedStatement.setString(1, bookName);
result = preparedStatement.executeQuery(infoQuery);
与
preparedStatement = conn.prepareStatement(infoQuery);
preparedStatement.setString(1, bookName);
result = preparedStatement.executeQuery();
关于参数化查询中的 JavaScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30826535/