做了一个插入学生信息的页面,用户填写完相应内容后,调用studentDaoImpl中的addStu方法向数据库写入对应数据
addStu方法
- public int addStu(student stu) {
- // TODO Auto-generated method stub
- sql = "insert into student values(?,?,?,?,?,?)";//int string string int string double
-
- try {
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setInt(1, stu.getId());
- ps.setString(2, stu.getName());
- ps.setDouble(3, stu.getScore());
- ps.setString(4, stu.getSex());
- ps.setInt(5, stu.getAge());
- ps.setString(6, stu.getClassname());
- num = ps.executeUpdate();
-
- ps.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return num;
- }
**Servlet中取页面输入的数据(数据格式已经用js控制了)
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html;charset=utf-8");
-
- int id = Integer.parseInt(request.getParameter("id"));
- String name = request.getParameter("name");
- double score = Double.parseDouble(request.getParameter("score"));
- String sex = request.getParameter("sex");
- int age = Integer.parseInt(request.getParameter("age"));
- String classname = request.getParameter("classname");
- /*添加进数据库前先判断此学生存在了没有
- * :存在,返回页面并提示已存在
- * :不存在,添加
- * */
- studentDaoImpl sdi = new studentDaoImpl();
- if(sdi.findStuById(id)!=null){
- //存在
- request.setAttribute("exist", "<font color='red'>此学生已存在</font><br/>");
- request.getRequestDispatcher("addStu.jsp").forward(request, response);
- }
- else{
- //不存在
- student stu = new student();
- stu.setId(id);
- stu.setName(name);
- stu.setScore(score);
- stu.setSex(sex);
- stu.setAge(age);
- stu.setClassname(classname);
- sdi.addStu(stu);
- request.setAttribute("exist", "添加成功");
- request.getRequestDispatcher("addStu.jsp").forward(request, response);
- }
-
- }
这是js代码,用正则表达式控制input的各个数据格式
- // JavaScript Document
-
- function check(){
- var id = document.getElementsByName('id')[0].value.replace(/(^\s*)|(\s*$)/g, "");
- var score = document.getElementsByName('score')[0].value.replace(/(^\s*)|(\s*$)/g, "");
- var age = document.getElementsByName('age')[0].value.replace(/(^\s*)|(\s*$)/g, "");
- var classname = document.getElementsByName('classname')[0].value.replace(/(^\s*)|(\s*$)/g, "");
- var errors = document.getElementsByName('error');
- var flag = true;
- /*每次点击时都先清空错误提示消息*/
- for(var i=0; i<errors.length; i++){
- errors[i].innerHTML = "";
- }
-
-
- if(/^[1-9]\d{0,1}/.test(id)==false){
- errors[0].innerHTML = "学号输入有误,应为非0开头的1-2位数字";
- flag = false;
- }
- if(/^\d+(\.\d+)?$/.test(score)==false){
- errors[1].innerHTML = "分数格式有误,应为非负小数或整数";
- flag = false;
- }
- if(/^[1-9]\d{0,2}$/.test(age)==false){
- errors[2].innerHTML = "年龄格式有误,应为1-3位不为0开头的数字";
- flag = false;
- }
- if(/^[1-9]{2}[\u4e00-\u9fa5]{2,3}[1-9]{1}[\u73ed]{1}$/.test(classname)==false){
- errors[3].innerHTML = "班级名称格式有误,应为'2位数字+2字专业+1位数字+班'";
- flag = false;
- }
- return flag;
- }
在执行时报错Data truncated for column 'score' at row 1,网上搜大多是说格式没有对应上,可能java里的score是个字符串而数据库里的是double,我也特意检查了一下表格式,发现无误,这是创建表的语句
- mysql> create table student(
- -> id int(10) not null auto_increment,
- -> name varchar(20) not null,
- -> score double(5,2) not null,
- -> sex varchar(2) not null,
- -> age int(3) not null,
- -> classname varchar(20) not null,
- -> primary key(id)
- -> )ENGINE=InnoDB, DEFAULT CHARSET=utf8;
- Query OK, 0 rows affected (0.01 sec)
不太会排版,有点凌乱了,实在抱歉