----------------------------------login.jsp----------------------------------
<body>
<h1 style="color: orange;font-style: italic;">学生注册:</h1>
<div style="margin-left:100px ">
<form action="check.jsp" method="post">
<p><span>姓名: </span><input type="text" name="name"/></p>
<p><span>学号: </span><input type="text" name="code"/></p>
<p><span>邮箱: </span><input type="text" name="email"/></p>
<p>
<span>兴趣爱好: </span>
<label><input type="checkbox" name="habit" value="sport"/><span>运动</span></label>
<label><input type="checkbox" name="habit" value="swim"/><span>游戏</span></label>
<label><input type="checkbox" name="habit" value="read"/><span>阅读</span></label>
<label><input type="checkbox" name="habit" value="travel"/><span>旅游</span></label>
</p>
<p>
<span>性别: </span>
<label><input type="radio" name="sex" value="1"/><span>男</span></label>
<label><input type="radio" name="sex" value="2"/><span>女</span></label>
</p>
<p><span>个人介绍: </span></p>
<p><textarea rows="5" cols="50" name="produce" style="margin-left: 70px;"></textarea></p>
<p><input type="hidden" name="clazz" value="java88"/></p>
<div><hr style="border: 1px dashed green;width: 50%;margin-left: -10px;"/></div>
<p><input type="submit" value="注 册" style="width: 80px;height: 30px;font-size: 18px;"/></p>
</form>
</div>
</body>
----------------------------------check.jsp----------------------------------
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String code = request.getParameter("code");
String email = request.getParameter("email");
String[] hobit = request.getParameterValues("habit");
if(hobit == null) hobit=new String[]{};
String sex = request.getParameter("sex");
String produce = request.getParameter("produce");
String clazz = request.getParameter("clazz");
Student stu = new Student(name,code,Integer.parseInt(sex),email,Arrays.toString(hobit),produce,clazz);
if(new StudentService().log(stu)){
request.setAttribute("stu", stu);
%>
<jsp:forward page="success.jsp"/>
<%
} else {
%>
<jsp:forward page="error.jsp"/>
<%
}
%>
</body>
----------------------------------success.jsp----------------------------------
<body>
<%
request.setCharacterEncoding("UTF-8");
Student stu = (Student)request.getAttribute("stu");
%>
<%=stu.toString() %>
</body>
----------------------------------error.jsp----------------------------------
<body>
<h1 style="color: red;">Error</h1>
</body>
----------------------------------student.java----------------------------------
package com.ntqn;
// 学生类
public class Student {
private Integer id;
private String name;
private String code;
private int sex;
private String email;
private String habit;
private String introduce;
private String clazz;
public Student() {
super();
}
public Student(String name, String code, int sex, String email, String habit, String introduce, String clazz) {
super();
this.name = name;
this.code = code;
this.sex = sex;
this.email = email;
this.habit = habit;
this.introduce = introduce;
this.clazz = clazz;
}
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHabit() {
return habit;
}
public void setHabit(String habit) {
this.habit = habit;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", code=" + code + ", sex=" + sex + ", email=" + email
+ ", habit=" + habit + ", introduce=" + introduce + ", clazz=" + clazz + "]";
}
}
----------------------------------studentDao.java----------------------------------
package com.ntqn;
import java.util.List;
public interface StudentDaoable {
/**
* 添加学生到数据库
* @return
* @throws Exception
*/
public boolean add(Student stu) throws Exception;
/**
* 根据id修改学生信息
* @param stu
* @return
* @throws Exception
*/
public boolean update(Student stu, int id) throws Exception;
/**
* 根据id删除学生
* @return
* @throws Exception
*/
public boolean deleteById(int id) throws Exception;
/**
* 根据id批量删除学生
* @param ids
* @return
* @throws Exception
*/
public boolean deleteByIds(int[] ids) throws Exception;
/**
*根据id查找学生
* @param id
* @return
* @throws Exception
*/
public Student findById(int id) throws Exception;
/**
* 根据爱好模糊查询学生
* @param name
* @return
* @throws Exception
*/
public List<Student> findByName(String name) throws Exception;
}
----------------------------------studentImplement.java----------------------------------
package com.ntqn;
import java.sql.*;
import java.util.*;
public class StudentImplement implements StudentDaoable {
@Override
public boolean add(Student stu) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_system","root","");
PreparedStatement ps = con.prepareStatement("insert into t_student_info (name,code,sex,email,habit,introduce,clazz) values (?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
ps.setString(1, new String(stu.getName().getBytes(),"UTF-8"));
ps.setString(2, stu.getCode());
ps.setInt(3, stu.getSex());
ps.setString(4, stu.getEmail());
ps.setString(5, stu.getHabit());
ps.setString(6, stu.getIntroduce());
ps.setString(7, stu.getClazz());
int flag = ps.executeUpdate();
int autoInckey = -1;
ResultSet rs = ps.getGeneratedKeys(); // 获取结果
if (rs.next()) {
autoInckey = rs.getInt(1);// 取得ID
}
stu.setId(autoInckey);
ps.close();
con.close();
return flag == 1;
}
@Override
public boolean deleteById(int id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_system","root","");
PreparedStatement p1 = con.prepareStatement("delete from t_student_info where id = ?");
p1.setInt(1, id);
int flag = p1.executeUpdate();
p1.close();
con.close();
return flag == 1;
}
@Override
public boolean deleteByIds(int[] ids) throws Exception {
for (int i = 0; i < ids.length; i++) {
int id = ids[i];
if(!deleteById(id))
return false;
}
return true;
}
@Override
public Student findById(int id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_system","root","");
PreparedStatement p1 = con.prepareStatement("select name,code,sex,email,habit,introduce,clazz from t_student_info where id = ?");
p1.setInt(1, id);
ResultSet set = p1.executeQuery();
Student stu = null;
if (set.next()) {
stu = new Student(set.getString("name"),set.getString("code"),set.getInt("sex"),set.getString("email"),set.getString("habit"),set.getString("introduce"),set.getString("clazz"));
stu.setId(id);
}
p1.close();
con.close();
return stu;
}
@Override
public List<Student> findByName(String name) throws Exception {
List<Student> list = new ArrayList<>();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_system","root","");
PreparedStatement p1 = con.prepareStatement("select id,name,code,sex,email,habit,introduce,clazz from t_student_info where name like ?");
p1.setString(1, "%"+name+"%");
ResultSet set = p1.executeQuery();
while (set.next()) {
Student stu = new Student(set.getString("name"),set.getString("code"),set.getInt("sex"),set.getString("email"),set.getString("habit"),set.getString("introduce"),set.getString("clazz"));
stu.setId(set.getInt("id"));
list.add(stu);
}
p1.close();
con.close();
return list;
}
@Override
public boolean update(Student stu, int id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_system","root","");
PreparedStatement p1 = con.prepareStatement("update t_student_info set name=?,code=?,sex=?,email=?,habit=?,introduce=?,clazz=? where id = ?");
p1.setString(1, stu.getName());
p1.setString(2, stu.getCode());
p1.setInt(3, stu.getSex());
p1.setString(4,stu.getEmail() );
p1.setString(5, stu.getHabit());
p1.setString(6, stu.getIntroduce());
p1.setString(7, stu.getClazz() );
p1.setInt(8, id);
int flag = p1.executeUpdate();
p1.close();
con.close();
return flag == 1;
}
}
----------------------------------studentService.java----------------------------------
package com.ntqn;
public class StudentService {
private StudentDaoable stuIm = new StudentImplement();
/**
* 学生注册账户到数据库
* @param stu
* @return
* @throws Exception
*/
public boolean log(Student stu) throws Exception {
return stuIm.add(stu);
}
}