gpt4 book ai didi

java - 如何使用AJAX在java中显示MYSQL的数据

转载 作者:行者123 更新时间:2023-11-29 00:13:54 25 4
gpt4 key购买 nike

我在 mysql 中有一个表,其中包含三个插入了数据的字段。其中包含三个字段,如下所示。 this is snapshot of a database, table and inserted field in it

我想在 java 中使用 ajax 在 Web 浏览器上显示此数据。

我在网上搜索,发现下面的代码最有用

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ajax_demo","root","");  
PreparedStatement ps=con.prepareStatement("select * from ajax");

为了显示输出,我还必须执行哪些其他操作。

最佳答案

需要做一个ajax请求,ajax请求的主要代码如下

request.onreadystatechange=handleResponse;
request.open(typeReq, url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.send(queryString);

typeReq 是请求的类型:POST 或 GET。url:目标url地址。queryString:您要发送的数据集。

handleResponse 是一个处理响应的函数。例如:

function handleResponse () {
if (request.readyState = 4) {
if (request.status = 200) {
var response = request.responseText;
//code to handle response
} else {
//code to handle errors
}
}

现在,您可以使用 prototype 或 jquery 之类的 API,这更容易。

希望这些信息对您有所帮助。

祝你好运

关于java - 如何使用AJAX在java中显示MYSQL的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23655338/

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