我正在尝试更新数据库中的数据,我有一个控制器servlet,DBhelper,User类和update.jsp。
这是控制器servlet ..
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import model.DBhelper;
import user.User;
/**
* Servlet implementation class servletJsp
*/
@WebServlet("/controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
@Resource(name="")
private DataSource dataSource;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page;
if(request.getParameterMap().containsKey("page")){
page =request.getParameter("page");
}else{
page="index";
}
switch (page) {
case "index":
homepage(request,response);
break;
case "update":
String username = request.getParameter("username");
update(username, request,response);
break;
}
}
private void update(String username, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameterMap().containsKey("action")){
if(request.getParameter("action").equals("submit")){
String authority = request.getParameter("authority");
User temp = new User(username,authority);
try {
new DBhelper().update(temp,dataSource);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("message", "Record updated");
request.getRequestDispatcher("message.jsp").forward(request, response);
}
}else{
User user = null;
try {
user = new DBhelper().getUser(username,dataSource);
} catch (SQLException e) {
e.printStackTrace();
}
//set the attribute
request.setAttribute("user", user);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
}
这是DBhelper ..
public class DBhelper {
private ResultSet users =null;
public List<User> getUsers(DataSource dataSource) throws SQLException {
Connection con=null;
Statement st= null;
List<User> userList = new ArrayList<>();
try{
con =dataSource.getConnection();
st=con.createStatement();
String query="select * from authorities";
users= st.executeQuery(query);
while(users.next()){
String authority = users.getString("Authority");
String username = users.getString("Username");
User temp = new User(username, authority);
userList.add(temp);
}
}catch(Exception e){
e.printStackTrace();
return userList;
}finally{
con.close();
st.close();
}
return userList;
}
public User getUser(String username, DataSource dataSource) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
User user =null;
try{
conn = dataSource.getConnection();
String query ="select from Authorities where Username=?";
stmt = conn.prepareStatement(query);
stmt.setString(1, username);
rs = stmt.executeQuery();
String authority=null;
if(rs.next()){
authority = rs.getString("authority");
}
user = new User(username, authority);
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
stmt.close();
}
return user;
}
public boolean update(User user, DataSource dataSource) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = dataSource.getConnection();
String Query = "update Authorities set Authority=? where Username=?";
stmt = conn.prepareStatement(Query);
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getAuthority());
return stmt.execute();
}finally{
conn.close();
stmt.close();
}
}
}
和更新jsp ..
<form action="${pageContext.request.contextPath}/Controller" >
<table>
<tbody>
<tr>
<td><label>User name</label></td>
<td><input type="text" name="username" value="${user.Username }"></td>
</tr>
<tr>
<td><label>Authority</label></td>
<td><input type="text" name="authority" value="${user.Authority }"><td>
</tr>
<tr>
<td><input type="submit" value="Update"></tr>
<tr>
<input type="hidden" name="page" value="update">
<input type="hidden" name="action" value="submit">
<input type="hidden" name="username" value="${user.Username }">
</tbody>
</table>
</form>
会出现什么问题......