首先一个登陆页面代码如下
String action=(String)request.getAttribute("action");
String id=(String)request.getAttribute("id");
String name=(String)request.getAttribute("name");
String englishName=(String)request.getAttribute("englishName");
String age=(String)request.getAttribute("age");
String sex=(String)request.getAttribute("sex");
String birthday=(String)request.getAttribute("birthday");
String description=(String)request.getAttribute("description");
boolean isEdit="edit".equals(action);
%>
"/>
"/>
姓名 | "/>< |
英文名 | "/> |
性别 | />男 />女 |
年龄 | "/> |
生日 | "/>![]() |
描述 | |
"/> |
处理页面代码如下
My JSP 'operatePerson.jsp' starting pagerequest.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String englishName=request.getParameter("englishName");
String age=request.getParameter("age");
String birthday = request.getParameter("birthday").trim();;
String sex=request.getParameter("sex");
String description=request.getParameter("description");
String action=request.getParameter("action");
if("add".equals(action)){
String sql="insert into tb_person (name,english_name,age,sex,birthday,description) values('"+name+"','"+englishName+"','"+age+"','"+sex+"','"+birthday+"','"+description+"')";
Connection conn=null;
Statement stmt=null;
int result=0;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseWeb?characterEncoding=utf-8","root","");
stmt = conn.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException e){
out.println("执行sql\""+sql+"\"是发生异常:"+e.getMessage());
return;
}finally{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
out.println("
");out.println(result+"条记录被添加到数据库中");
out.println("返回人员列表");
out.println("
执行的sql语句为:
"+sql);
}
%>