[英]JSP Web Application Will Not Insert Data Into A Database Using NetBeans And MySQL
我正在嘗試使用 NetBeans 將數據插入到我的數據庫中。 連接工作正常。 我嘗試了另一個使用 SELECT 數據的項目,它工作正常,但我的 INSERT 數據代碼沒有插入數據,我在我的代碼中找不到問題所在。 解決方案 ID 是自動遞增的,這就是為什么我沒有在我的插入語句中包含它。這是我的代碼:
<%@page import="java.sql.*"%>
<%Class.forName("com.mysql.cj.jdbc.Driver");%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert Solutions</title>
</head>
<body onload="displayResults()">
<h1>Insert Solutions</h1>
<%!
public class Backup {
String url = "jdbc:mysql://localhost:3306/solutions";
String user = "root";
String pass = "t3i3n3a3";
Connection connection = null;
PreparedStatement insertBackups = null;
ResultSet resultSet = null;
public Backup() {
try {
connection = DriverManager.getConnection(url, user, pass);
insertBackups = connection.prepareStatement(
"INSERT INTO backup (backup_name, backup_issue, backup_solution)"
+ "VALUES (?, ?, ?)");
} catch (SQLException e) {
e.printStackTrace();
}
}
public int setBackups(String first, String second, String third) {
int result = 0;
try {
insertBackups.setString(1, first);
insertBackups.setString(2, second);
insertBackups.setString(3, third);
result = insertBackups.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();;
}
return result;
}
}
%>
<%
int result = 0;
if (request.getParameter("submit") != null) {
String firstName = new String();
String secondIssue = new String();
String thirdSolution = new String();
if (request.getParameter("first") != null) {
firstName = request.getParameter("first");
}
if (request.getParameter("second") != null) {
secondIssue = request.getParameter("second");
}
if (request.getParameter("third") != null) {
thirdSolution = request.getParameter("third");
}
Backup backup = new Backup();
result = backup.setBackups(firstName, secondIssue, thirdSolution);
}
%>
<form name="myForm" action="index.jsp" method="POST">
<table>
<thead>
<tr>
<th>Backup Name :</th>
<th><input type="text" name="first" value="" size="50" /></th>
</tr>
</thead>
<tbody>
<tr>
<td>Backup Issue</td>
<td><input type="text" name="second" value="" size="50" /></td>
</tr>
<tr>
<td>Backup Solution</td>
<td><input type="text" name="third" value="" size="50" /></td>
</tr>
</tbody>
</table>
<br>
<input type="hidden" name="hidden" value="<%= result%>" />
<input type="reset" value="Clear" name="clear" />
<input type="submit" value="Submit" name="submit" />
</form>
<SCRIPT LANGUAGE="JaavaScript">
function displayResults()
{
if (document.myForm.hidden.value === 1) {
alert("Data Inserted!");
}
}
</SCRIPT>
</body>
</html>
另一個問題得到了回答。 這是 Javascript function 中的值。 當我將 === 1 更改為 ==1 時,它起作用了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.