gpt4 book ai didi

Java servlet 在服务器重启后跳过一个数据库读取

转载 作者:行者123 更新时间:2023-11-30 22:38:02 28 4
gpt4 key购买 nike

所以我刚开始学习java EE,我做了一个简单的servlet,用数据库统计访问者。最好的选择是每次访问者打开网站时添加条目,并简单地读取最后一个条目的自动增量字段(主键),这样我还可以保存访问日期和时间等。但是由于我正在学习,所以我选择做一些愚蠢的事情来进行实验,并且我有这个数据库:

id = 1,访问次数 = 21。

对于每个访问者,servlet 将访问变量更新为新数字。只要我不重启服务器,它就可以工作。当我执行 servlet 时跳过读取并开始计算 visits 设置的值变量的值(数据库本身不会重置)。
问题是我有旧笔记本电脑,所以我不想制作本地服务器进行测试,这需要几个小时,我使用的是 openshift tomcat7,但我不知道如何检查那里的错误日志。

它只是一个简单的 httpServlet,这是我的 get 方法中的逻辑:

String sqlRead = "SELECT *my_cell* FROM *my_table* WHERE id = 1";
String sqlUpdate = "UPDATE *my_table* SET *my_cell*=? WHERE id = 1";
int visits = 0;


try {
Class.forName("com.mysql.jdbc.Driver");
ResultSet qResult = DriverManager.getConnection
("jdbc:mysql://IPI.P.IPI.P:PORT/dbname", "user", "pass")
.createStatement().executeQuery(sqlRead);

//i know this reader starts at index-1
qResult.next();
this.visits = qResult.getInt("visits");
} catch (Exception e) {
System.out.print(e);
}

visits++;

try {
Class.forName("com.mysql.jdbc.Driver");
Connection writeConnection = DriverManager.getConnection
("jdbc:mysql://IPI.P.IPI.P:PORT/dbname", "user", "pass");

PreparedStatement preparedStmt = writeConnection.prepareStatement(sqlUpdate);
preparedStmt.setInt(1, this.visits);
preparedStmt.executeUpdate();

writeConnection.close();
} catch (Exception ex) {System.out.println(ex);
}
String visitsString = ""+visits;
response.setContentType("application/json");
response.getWriter().write(visitsString);

最佳答案

好的,所以我得到了一个工作版本,我把它拆掉了,这样数据库处理就可以进入新的类(class)了。我没有读完有关并发的内容,但到目前为止,我了解到 servlet 是由 tomcat 处理的,所以我只是同步了函数,希望我做对了。请更正任何进一步的错误。这是新版本:

private int localVisits = -1;

String sqlRead = "SELECT visits FROM visitsTable WHERE id = 1;";
String sqlUpdate = "UPDATE visitsTable SET visits=visits+1 WHERE id = 1;";
Connection con;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://XXX.XXX.XXX.XXX:PPPP/dbName", "user", "pass");
} catch (Exception e) {}

//calling write new visitor to db function
writeDBCounter();

//reading from db to send to frontend
String report = "-2";
try {
report = "" + readDBCounter();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response.setContentType("application/json");
response.getWriter().write(report);
//closing connection
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}

}

// read
private synchronized int readDBCounter() throws SQLException {
int newVisits = -1;
ResultSet rs = null;
try {
java.sql.Statement s =con.createStatement();
s.executeQuery(sqlRead);
rs = s.getResultSet();
rs.next();
newVisits = rs.getInt("visits");


} catch (Exception ex) {
System.out.print(ex);
}
rs.close();
return newVisits;
}

// write
private synchronized void writeDBCounter() {
try {
PreparedStatement preparedStmt = con.prepareStatement(sqlUpdate);
preparedStmt.executeUpdate();
} catch (Exception ex) {
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

关于Java servlet 在服务器重启后跳过一个数据库读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31876086/

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