我不确定问题出在我的代码中。当我按下提交按钮时,我正在尝试向我的数据库添加值。我还添加了一个println命令来打印出所请求的参数,但即使这样也是空的。请帮忙。
<%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver");%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Inserting data Check</title>
</head>
<body>
<h1>Checking data insertion</h1>
<%!public class Actor {
String URL = "jdbc:mysql://localhost:3306/sakila";
String USERNAME = "root";
String PASSWORD = "root";
Connection connection = null;
PreparedStatement insertActors = null;
ResultSet resultSet = null;
public Actor() {
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
insertActors = connection.prepareStatement(
"INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)"
+ " VALUES (?, ?, ?, ?, ?)");
} catch (SQLException e) {
e.printStackTrace();
}
}
public int setActors(String id, String last, String first, String address, String city) {
int result = 0;
try {
insertActors.setString(1, id);
insertActors.setString(2, last);
insertActors.setString(3, first);
insertActors.setString(4, address);
insertActors.setString(5, city);
result = insertActors.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
%>
<%
int result = 0;
String id = new String();
String last = new String();
String first = new String();
String add = new String();
String c = new String();
if (request.getParameter("id") != null) {
id = request.getParameter(id);
System.out.println("id" + id);
}
if (request.getParameter("last") != null) {
last = request.getParameter(last);
System.out.println("id" + last);
}
if (request.getParameter("first") != null) {
first = request.getParameter(first);
System.out.println("id" + first);
}
if (request.getParameter("address") != null) {
add = request.getParameter(add);
System.out.println("id" + add);
}
if (request.getParameter("city") != null) {
c = request.getParameter(c);
System.out.println("id" + c);
}
%>
<form name="myForm" action="insert.jsp" method="POST">
<table>
<tbody>
<tr>
<td>Id</td>
<td><input type="text" name="id" value="" size="50" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last" value="" size="50" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="first" value="" size="50" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" value="" size="50" /></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city" value="" size="50" /></td>
</tr>
</tbody>
</table>
<input type="reset" value="Reset" name="reset" />
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
这似乎是你的问题(至少当你检查这些值时):
if (request.getParameter("id") != null) {
id = request.getParameter(id); // change to request.getParameter("id");
System.out.println("id" + id);
}
if (request.getParameter("last") != null) {
last = request.getParameter(last); // change to "last"
System.out.println("id" + last);
}
if (request.getParameter("first") != null) {
first = request.getParameter(first); // change to "first"
System.out.println("id" + first);
}
if (request.getParameter("address") != null) {
add = request.getParameter(add); // change to "address"
System.out.println("id" + add);
}
if (request.getParameter("city") != null) {
c = request.getParameter(c); // change to "city"
System.out.println("id" + c);
}