目前我正在为使用mysql的用户写一个数据库,用户有几个字段,我希望他们能够使用表单更新他们当前的用户配置文件。在占位符中,我想显示他们当前的信息。提交应更改提供的信息并更新表格。
<h2>Update your profile</h2>
<div class="form">
<form action="/updateprofile">
<input type="text" name="Email_Address" placeholder="Email Address">
<input type="text" name="First_Name" placeholder="First Name">
<input type="text" name="Last_Name" placeholder="Last Name">
<input class="button" type="submit">
</form>
</div>
这是servlet代码{并非全部只写入sqldb}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//Obtain submitted form data
String firstName = req.getParameter("First_Name");
String lastName = req.getParameter("Last_Name");
String username = req.getParameter("User_Name");
String email = req.getParameter("Email_Address");
String password =req.getParameter("Password");
try {
//Setup the Database datasource
Context ctx = new InitialContext();
Context env = ( Context )ctx.lookup( "java:comp/env" );
DataSource ds = ( DataSource )env.lookup( "jdbc/carRentalSystem");
Connection conn = ds.getConnection();
//Prepare the SQL statmenet to insert the values
PreparedStatement stmt = conn.prepareStatement("INSERT INTO userdetails(First_Name, Last_Name, Email_Address, Password, User_Name) VALUES (?,?,?,?,?)");
stmt.setString(1, firstName);
stmt.setString(2, lastName);
stmt.setString(3, email);
stmt.setString(4, password);
stmt.setString(5, username);
//Execute the insert
stmt.executeUpdate();
conn.close();
//Dispatch into success page
RequestDispatcher requestDispatcher = req.getRequestDispatcher("login.html");
requestDispatcher.forward(req, res);
}
catch(Exception e){
System.out.println(e);
}
答案 0 :(得分:0)
我把它更改为更新而不是插入,这是一个很大的愚蠢的错误,我道歉花了很长时间。