<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'add.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">
-->
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript">
$(function (){
$("#for").validate({
rules:{
sname:{
required:true,
minlength:2
},
age:{
required:true,
digits:true,
range:[18,99]
},
sex:{
required:true
},
hobby:{
required:true
},
birthday:{
required:true,
dateISO:true
}
},
messages:{
sname:{
required:"必须填",
minlength:"最短长度为2"
},
age:{
required:"必须填",
digits:"必须为整数",
range:"18-99之间"
},
sex:"必须填",
hobby:"必须填",
birthday:{
required:"必须填",
dateISO:"例:2000-12-12"
}
},
submitHandler:function (){
$.post(
"student?m=add",
$("#for").serialize(),
function(msg){
if(msg==1){
alert("添加成功");
location="student?m=show";
}else{
alert("添加失败");
location.reload();
}
},
"text"
)
}
});
});
</script>
</head>
<body>
<form id="for">
sname:<input type="text" name="sname" ><br>
age:<input type="text" name="age" ><br>
sex:<input type="radio" name="sex" value="男" >男
<input type="radio" name="sex" value="女">女<br>
hobby:<input type="checkbox" value="睡觉" name="hobby" >睡觉
<input type="checkbox" value="唱歌" name="hobby">唱歌
<input type="checkbox" value="跳舞" name="hobby">跳舞<br>
birthday:<input type="text" name="birthday"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
package com.www.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.www.entity.Student;
public class StudentDao {
//JDBC连接数据库
public List show() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///exam", "root", "root");
PreparedStatement pst = con.prepareStatement("select * from student");
ResultSet rs = pst.executeQuery();
ArrayList list = new ArrayList();
while(rs.next()){
Student s = new Student(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
list.add(s);
}
return list;
}
public int add(String sname, String age, String sex, String hobb,
String birthday) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///exam", "root", "root");
PreparedStatement pst = con.prepareStatement("insert into student values(null,?,?,?,?,?)");
pst.setString(1, sname);
pst.setString(2, age);
pst.setString(3, sex);
pst.setString(4, hobb);
pst.setString(5, birthday);
System.out.println(sname+age+sex+hobb+birthday);
int i = pst.executeUpdate();
System.out.println(i);
return i;
}
public int del(String id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///exam", "root", "root");
PreparedStatement pst = con.prepareStatement("delete from student where sid = ?");
pst.setString(1, id);
int i = pst.executeUpdate();
return i;
}
public int delall(String ids) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///exam", "root", "root");
PreparedStatement pst = con.prepareStatement("delete from student where sid in ("+ids+")");
int i = pst.executeUpdate();
return i;
}
public Student reupd(String id) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///exam", "root", "root");
PreparedStatement pst = con.prepareStatement("select * from student where id=?");
pst.setString(1, id);
ResultSet rs = pst.executeQuery();
Student s = null;
while(rs.next()){
s = new Student(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
}
return s;
}
}
生活是一面镜子,你对它笑,它就对你笑,你对它哭,它就对你哭。
——拉伯雷
576

被折叠的 条评论
为什么被折叠?



