这段代码非常适合我。我创建了一个html表单,用户输入的所有输入都将转换为java中的字符串。我会使用这些字符串和准备好的语句将信息输入SQLite。
我让它工作,测试了几次,它是完美的。然后我将代码最小化了几个小时。我再打开它,它就停止了工作。现在所有的字符串都是“null”。不知道为什么。
这是employeeDAO准备好的声明:
static boolean insertEmployeeAtId(String name, String gender, String dob, String address, String postcode, String natins, String title, String startdate, String salary, String email) throws SQLException {
Employees e = new Employees();
PreparedStatement s;
s = connect.prepareStatement(
"INSERT INTO Emp_db (Name, Gender, DOB, Address, Postcode, NIN, JobTitle, StartDate, Salary, Email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
s.setString(1, name);
s.setString(2, String.valueOf(gender));
s.setString(3, dob);
s.setString(4, address);
s.setString(5, postcode);
s.setString(6, natins);
s.setString(7, title);
s.setString(8, startdate);
s.setString(9, salary);
s.setString(10, email);
return s.execute();
}
}
html表单的片段:
<form id="empForm" method="POST" action="/processEmp">
<h1 class="card-title">Add Employees</h1>
<div class="form-group">
<label for="name">Name:</label>
<input id="name" class="form-control" type="text" name=\"name\" pattern="[a-zA-Z-'\s]+" required/>
</div>
以下是涉及信息处理的控制器:
server.createContext("/processEmp", new HttpHandler() {
@Override
public void handle(HttpExchange t) throws IOException {
boolean insertEmployee;
try {
HashMap<String, String> post = new HashMap<String, String>();
BufferedReader in = new BufferedReader(new InputStreamReader(t.getRequestBody()));
String line = "";
String request = "";
while ((line = in.readLine()) != null) {
request = request + line;
}
String[] pairs = request.split(
"&"
);
for
(
int
i = 0; i < pairs.
length
; i++) {
String pair = pairs[i];
post.put(URLDecoder.decode(pair.split("=")[0], "UTF-8")
, URLDecoder.decode(pair.split("=")[1], "UTF-8"));
}
String EmpName = post.get("name");
String EmpGender = post.get("gender");
String EmpDob = post.get("dob");
String EmpAddress = post.get("address");
String EmpPostcode = post.get("postcode");
String EmpNatins = post.get("natInscNo");
String EmpTitle = post.get("title");
String EmpStartDate = post.get("startDate");
String EmpSalary = post.get("salary");
String EmpEmail = post.get("email");
String allEmp = "Name: " + EmpName + " Gender: " + EmpGender + " Dob: " + EmpDob + " Address: " + EmpAddress + " Postcode: " + EmpPostcode + " NatInscNo: " + EmpNatins + " Title: " + EmpTitle + " StartDate: " + EmpStartDate + " Salary: " + EmpSalary + " Email: " + EmpEmail;
dao.insertEmployeeAtId(EmpName, EmpGender, EmpDob, EmpAddress, EmpPostcode, EmpNatins, EmpTitle, EmpStartDate, EmpSalary, EmpEmail);
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(t.getResponseBody()));
out.write("Result = ");
out.write(String.valueOf(allEmp));
t.sendResponseHeaders(200, 0);
out.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
server.setExecutor(null); // default implementation of threading
server.start();
System.out.println("The server is up and running on port 8005");
}
我宁愿知道它为什么要工作以及如何修复它然后使用另一种方法将html中的输入值转换为java中的字符串。任何形式的帮助将不胜感激。