目前我正在为使用 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);
}
最佳答案
我将其更改为更新而不是插入,这是一个愚蠢的错误,我很抱歉花了这么长时间。
关于java - mysql使用servlet更新用户信息的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46472851/