gpt4 book ai didi

java - 使用jquery和ajax显示来自mysql的数据

转载 作者:行者123 更新时间:2023-11-29 23:33:37 26 4
gpt4 key购买 nike

我是一个初学者,我正在尝试制作一个简单的应用程序来使用 JavaScript 和 jquery、json 对象显示 mysql 中的表。这并没有给我任何错误,但我不知道如何继续。请提供您的反馈!

这是我的 books.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Books {
private int id;
private String title;
private String author;
private float price;
private int qty;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Books(int id, String title, String author, float price, int qty) {
super();
this.id = id;
this.title = title;
this.author = author;
this.price = price;
this.qty = qty;
}

public Books() {

}
public ArrayList <Books> getBooks(ArrayList<Books> bookList){

Connection conn =null;
ResultSet rset = null;

try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop", "root", "root");
PreparedStatement pst = (PreparedStatement) conn
.prepareStatement("SELECT * from books");
rset = pst.executeQuery();

while (rset.next()){
Books books = new Books();
books.setId(rset.getInt("id"));
books.setAuthor(rset.getString("author"));
books.setTitle(rset.getString("title"));
books.setPrice(rset.getFloat("price"));
books.setQty(rset.getInt("qty"));
bookList.add(books);

}
} catch (SQLException e) {
System.out.println("Could not connect to DB" + e);
} catch (ClassNotFoundException classexpt){
System.out.println("Couldn't find the class" + classexpt);
}

return bookList;

}

}

Servlet 文件:

        import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class BooksServlet
*/
@WebServlet("/BooksServlet")
public class BooksServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public BooksServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("application/json");
Books books = new Books();
ArrayList <Books> bookList = new ArrayList <Books>();
bookList = books.getBooks(bookList);
request.setAttribute("bookList", bookList);

JSONObject obj=new JSONObject();
JSONArray arr = new JSONArray();
for(int i = 0 ; i< bookList.size() ; i++)
{
Books b = bookList.get(i);

obj.put("id", b.getId());
obj.put("title", b.getTitle());
obj.put("author", b.getAuthor());
obj.put("price", b.getPrice());
obj.put("qty", b.getQty());

arr.add(obj.toString());

obj = new JSONObject();
}

String address = "DisplayBook.jsp";
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

Jsp 文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function showBook(){
$.ajax({
type:"GET",
url:"BooksServlet.java",
dataType: "json",
data: JSON.stringify({ bookList: bookdata }),
success:function(data){
$("#content").html(data);
}
});
}
showBook();
});
</script>

<h3 align="center">Manage Book Details</h3>
<table border="1" align="center">
<tr>
<td> <input type="button" id="display" value="Display All Data" /> </td>
</tr>
</table>
<div id="content" align="center"></div>
<table>
<tr>
<td>id</td>
</tr>
</table>

</body>
</html>

JSp 文件是我迷路的地方。我认为我正在将数据与列表一起发送,但没有收到它。或者我在 servlet 中发送的数据错误?任何帮助将不胜感激!

最佳答案

尝试访问 [server]/[context]/BooksServlet,例如 localhost:8080/myappp/BookServlet,因为/BookServlet 是 servlet 中的映射 URL。

查看此@WebServlet annotation with Tomcat 7http://www.codejava.net/java-ee/servlet/webservlet-annotation-examples

顺便说一句,我还是不明白,你的Ajax URL转到/BookServlet并返回json,但那个servlet不返回json数据。

关于java - 使用jquery和ajax显示来自mysql的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26461605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com