我只想使用 Ajax 在 div 通知中显示数据库中的数据,但它不起作用。我正在使用 Maven 和 Spring、Hibernate,在这段代码中我只使用 JDBC,只是为了看看它是否工作。但它没有给出任何输出。
function getNotifications() {
var xmlHttp;
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
alert("your browser cant support");
}
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
var x = (xmlHttp.responseText);
document.getElementById("notifications").innerHTML = x;
/* document.write(xmlHttp.responseText); */
}
}
var url = "auto.jsp";
url = url + "?q=" + str;
xmlHttp.open("get", url, true);
xmlHttp.send(url);
}
自动.jsp
<%@ page import="java.sql.*" %>
<% String s="G";
System.out.println("helloooooooooo1111111");
String result="";
ArrayList li = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2402","root","admin");
System.out.println("helloooooooooo1111111");
Statement st = con.createStatement();
String sql = "select * from message ";
System.out.println("helloooooooooo22222266666");
st.execute(sql);
ResultSet rs = st.getResultSet();
System.out.println("helloooooooooo222222");
int i = 1;
while(rs.next()){
li.add(rs.getString(i));
i = i++;
}
}
catch(Exception ex){
out.println(ex);
}
%>
<%
Iterator itr = li.iterator();
while (itr.hasNext()) {
out.println(itr.next());
}
%>