private static final long serialVersionUID = 1L;
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException{
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter pw=res.getWriter();
//获取请求参数id
int id=Integer.valueOf(req.getParameter("id"));
//根据参数id查询数据库获取员工信息
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3308/test", "root", "root");
ps=conn.prepareStatement("select * from sysuser where id=?");
ps.setInt(1, id);
rs=ps.executeQuery();
pw.println("<html><head></head><body style='font-size:30px'>");
if(rs.next()){
String name=rs.getString("name");
double salary=rs.getDouble("salary");
int age=rs.getInt("age");
pw.println("<form action='modify'> method='post'");
pw.println("编号:"+id+"<br>");
pw.println("<input type='hidden' name='id' value='"+id+"'/><br>");
pw.println("姓名:<input name='name' value='"+name+"'/><br>");
System.out.println("load中的name:"+name);
pw.println("薪水:<input name='salary' value='"+salary+"'/><br>");
pw.println("年龄:<input name='age' value='"+age+"'/><br>");
pw.println("<input type='submit' value='修改'/>");
pw.println("</form>");
}
pw.println("</body></html>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public class ModifyEmpServlet(servlet2) extends HttpServlet{
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException{
--//在这里获取name也是乱码,感觉设置的下面这两行没有用
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter pw=res.getWriter();
//获取请求参数
int id=Integer.valueOf(req.getParameter("id"));
double salary=Double.valueOf(req.getParameter("salary"));
int age=Integer.valueOf(req.getParameter("age"));
String name=req.getParameter("name");-------------------------这里就乱码了
//数据库操作
Connection conn=null;
PreparedStatement ps=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3308/test", "root","root");
ps=conn.prepareStatement("update sysuser set name=?,salary=?,age=? where id=?");
ps.setString(1, name);
ps.setDouble(2, salary);
ps.setInt(3, age);
ps.setInt(4, id);
ps.executeUpdate();
res.sendRedirect("list");
} catch (Exception e) {
e.printStackTrace();
pw.print("系统出现问题,稍后重试<br><p><a href='list'>员工信息列表</a></p>");
} finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}拜求各位朋友看看,什么情况如何解决?谢谢啦