采用Java Web编写项目的时候遇到了这个问题,写下来!
<%@ page language="java" import="java.util.*,cn.qm.db.*,java.sql.*" pageEncoding="GB2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'adduser.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> .adduser{ text-align:center; font-size:12px; line-height:30px; color:#5A5A5A;position:absolute; top:20px; left:30px; width:600px; } .table{ text-align:center; font-size:12px; line-height:30px; color:#5A5A5A;position:absolute; top:50px; left:30px; width:600px; } .user{ text-align:center; font-size:12px; line-height:30px; color:#5A5A5A; position:absolute; left:50px; top:20px; } .info{ text-align:center; font-size:12px; line-height:30px; color:#5A5A5A; position:absolute; left:50px; top:100px; } .table2{ text-align:center; font-size:12px; line-height:30px; color:#5A5A5A;position:absolute; top:120px; left:30px; width:600px; } .STYLE3 {font-size: 12px} .STYLE9 {color: #FF0000; font-size: 12; } .STYLE10 {font-size: 10} </style> <script type="text/javascript" src="JQuery/jquery-1.5.2.min.js"></script> <% DataBase db = new DataBase(); ResultSet rs; %> </head> <body> <form action="adduser.do" method="post"> <div id="lee" class="adduser"> <div class="user">用户登录信息</div> <div class="table"> <hr> <table cellpadding="0" cellspacing="0" border="0"> <tr> <!-- 用户名采用输入框的形式 --> <td width="100"><div align="center"><span class="STYLE3">用户名:</span></div></td> <td width="100"><input name="username" type="text"></td> <td width="100"><div align="center"><span class="STYLE3">默认密码:</span></div></td> <td width="100">123456</td> </tr> <tr> </table> </div> <div class="info">用户个人信息</div> <div class="table2"> <hr> <table border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="30" width="50"><span class="STYLE3">员工号:</span></td> <td width="100"><input name="eno" type="text"></td> <td width="100"><span class="STYLE9">*必填</span></td> <td width="150"><span class="STYLE3"></span></td> </tr> <!-- 用户名采用输入框的形式 --> <tr> <td height="30" width="50"><span class="STYLE3">姓名:</span></td> <td width="100"><input name="name" type="text"></td> <td width="100"><span class="STYLE9">*必填</span></td> <td width="150"><span class="STYLE3"></span></td> </tr> <tr> <!-- 用户名采用输入框的形式 --> <td height="30"><span class="STYLE3">性别:</span></td> <td> <span class="STYLE3"> <input type="radio" name="sex" value="man" checked> 男 <input type="radio" name="sex" value="woman"> 女 </span></td> <td> </td> <td><span class="STYLE3"></span></td> </tr> <tr> <!-- 用户名采用输入框的形式 --> <td height="30"><span class="STYLE3">年龄:</span></td> <td><input name="age" type="text"></td> <td><span class="STYLE9">*必填</span></td> <td><span class="STYLE3"></span></td> </tr> <tr> <!-- 用户名采用输入框的形式 --> <td height="30"><span class="STYLE3">电话:</span></td> <td><input name="telephone" type="text"></td> <td><span class="STYLE10"></span></td> <td><span class="STYLE3"></span></td> </tr> <tr> <!-- 用户名采用输入框的形式 --> <td height="30"><span class="STYLE3">地址:</span></td> <td><input name="address" type="text"></td> <td><span class="STYLE9">*必填</span></td> <td><span class="STYLE3"></span></td> </tr> <tr> <!-- 用户名采用输入框的形式 --> <td height="30"><span class="STYLE3">部门号:</span></td> <td> <span class="STYLE3"> <% String si = "select BMBH,BMMC from BMB"; %> <select name="bmh" size="1"> <% rs = db.resultset(si); while(rs.next()){ %> <option value="<%=rs.getString("BMBH") %>"><%=rs.getString("BMBH") %> <%}; %> </select> </span></td> <td><span class="STYLE9">*必选</span></td> <td><span class="STYLE3"></span></td> </tr> <tr> <td><input type="submit" value="添加"></td> <td></td> <td><input type="reset" value="清空"></td> <td></td> </tr> </table> </div> </div> <script type="text/javascript"> var target = $("#lee"); target.height(400) .width(1000) .css("border" , "1px solid black") </script> </form> </body> </html>
package cn.qm.actionform; import org.apache.struts.action.ActionForm; public class adduseractionform extends ActionForm { private String username; private String eno; private String name; private String sex; private int age; private String telephone; private String address; private String bmh; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getBmh() { return bmh; } public void setBmh(String bmh) { this.bmh = bmh; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEno() { return eno; } public void setEno(String eno) { this.eno = eno; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package cn.qm.action; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import cn.qm.actionform.adduseractionform; import cn.qm.db.DataBase; public class adduseraction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { adduseractionform uaf = (adduseractionform)form; String username = uaf.getUsername(); String eno = uaf.getEno(); String name = new String(uaf.getName().getBytes("iso8859-1"),"gbk"); //String name = uaf.getName(); String sex = uaf.getSex(); if(sex.equals("man")){ sex = "男"; }else{ sex = "女"; } int age = uaf.getAge(); String telephone = uaf.getTelephone(); String address = new String(uaf.getAddress().getBytes("iso8859-1"),"gbk"); String bmh = uaf.getBmh(); System.out.println(name+" "+address); DataBase db = new DataBase(); PreparedStatement pstmt; ResultSet rs; String si = "Select id from YGXXB"; rs = db.resultset(si); int number = 0; while(rs.next()){ number = rs.getInt("id"); } String sql = "insert into YGXXB values(?,?,?,?,?,?,?,?)"; String check = "feild"; if(username.length()==0||eno.length()==0||name.length()==0||telephone.length()==0||address.length()==0){ request.setAttribute("check", check); return mapping.findForward("add"); }else{ pstmt = db.preparedstatement(sql); pstmt.setInt(1, number+1); pstmt.setString(2, eno); pstmt.setString(3, name); pstmt.setString(4, sex); pstmt.setInt(5, age); pstmt.setString(6, telephone); pstmt.setString(7, address); pstmt.setString(8, bmh); pstmt.execute(); check = "success"; request.setAttribute("check", check); return mapping.findForward("add"); } } }