在IntelliJ中,我试图将新记录添加到MySQL表中,但是,当我启动服务器(Tomcat 9.0.141)时,它会添加双记录。当我刷新浏览器时,它只添加一次,而当我启动服务器(Tomcat)时,它添加两次。我到处都在寻找答案,即使是在FB上也是如此,没人知道该说些什么。请帮助。这是我的整个servlet:
@WebServlet(value = "/sqlu")
public class MySQLServletUpdate extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain; charset=utf-8");
try{
Driver driver = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/AppJEE?useSSL=false&characterEncoding=utf8&useJDBCCompianttimezoneShift=true&useLegacyDatetimecode=false&serverTimezone=UTC", "root", "password");
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO user VALUES (null, ?, ?, ?)");
pstmt.setString(1, "Mark");
pstmt.setString(2, "Marks");
pstmt.setString(3, "Mar");
if (pstmt.executeUpdate() > 0) {
response.getWriter().println("Record added");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
当我在phpMyAdmin中查看表时,会出现一些错误:
Warning in ./libraries/sql.lib.php#613
count(): Parameter must be an array or an object that implements Countable
Backtrace
./libraries/sql.lib.php#2128: PMA_isRememberSortingOrder(array)
./libraries/sql.lib.php#2079: PMA_executeQueryAndGetQueryResponse(
array,
boolean true,
string 'AppJEE',
string 'uzytkownik',
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
string '',
string './themes/pmahomme/img/',
NULL,
NULL,
NULL,
string 'SELECT * FROM `uzytkownik`',
NULL,
NULL,
)
./sql.php#221: PMA_executeQueryAndSendQueryResponse(
array,
boolean true,
string 'AppJEE',
string 'uzytkownik',
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
string '',
string './themes/pmahomme/img/',
NULL,
NULL,
NULL,
string 'SELECT * FROM `uzytkownik`',
NULL,
NULL,
)
./index.php#53: include(./sql.php)
这是我桌子上的一个视图1
这是我桌子上的另一个视图2
答案 0 :(得分:0)
我通过将Tomcat服务器设置为仅运行而无需立即加载页面来解决我的问题。当Tomcat运行时,我手动转到浏览器并加载页面,然后仅添加一次记录。