我正在尝试从jsp页面上的mysql数据库中检索数据,当页面加载时,它会加载最新的数据(这就是我想要的),但是当我点击刷新按钮时我就刷新了具有较旧数据的表然后按钮在此之后不起作用(不会刷新表)。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*"%>
<!DOCTYPE html>
<html>
<head>
<title>Main Page</title>
<h1>
<strong>test page</strong>
</h1>
<hr />
<canvas id="mycanvas" width="400" height="186"></canvas>
<script src="JavaScript/smoothie.js"></script>
<script src="JavaScript/chart.js"></script>
<script src="JavaScript/basic.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#refresh").click(function(){
$("#heart_rate").load("basic2.jsp")
});
});
</script>
</head>
<body>
<br>
<br>
<button id="refresh">Refresh table</button>
<br>
<br>
<form method="post" name="form">
<table id="heart_rate" border="1">
<tr>
<th>Sensor id</th>
<th>Time stamp</th>
<th>Heart rate</th>
<th>Event time</th>
</tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "Avantidrome";
String driver = "com.mysql.jdbc.Driver";
String un ="root";
String pw="root";
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url + db, un, pw);
String sql = "select * from avantidrome.heartratedata order by timestamp DESC limit 10";
st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
%>
<%
while(rs.next())
{
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
</tr>
<%
}
%>
<%
} catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
单击按钮后,它会调用basic.jsp,这只是:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<br>
<br>
<form method="post" name="form">
<table id="heart_rate" border="1">
<tr>
<th>Sensor id</th>
<th>Time stamp</th>
<th>Heart rate</th>
<th>Event time</th>
</tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "Avantidrome";
String driver = "com.mysql.jdbc.Driver";
String un ="root";
String pw="root";
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url + db, un, pw);
String sql = "select * from avantidrome.heartratedata order by timestamp DESC limit 10";
st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
%>
<%
while(rs.next())
{
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
</tr>
<%
}
%>
<%
} catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
所有需要做的就是按钮从数据库中选择最新数据,并在点击它时将其返回到表中。
答案 0 :(得分:0)
有两种方法可以做你想做的事:
在我看来,我更喜欢第一个选项,但是如果你选择第二个选项,那么另一个可以帮助你的SO帖子:How to use Servlets and Ajax?