Student.java和Student Dao.java

package student.bean;

import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable{   //序列化接口,表示可以在网络中传输

    private String id;
    private String name;
    private String gender;
    private Date birthday;
    private String cellphone;
    private String email;
    private String hobby;
    private String type;
    private String description;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getCellphone() {
        return cellphone;
    }
    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", gender=" + gender
                + ", birthday=" + birthday + ", cellphone=" + cellphone
                + ", email=" + email + ", hobby=" + hobby + ", type=" + type
                + ", description=" + description + "]";
    }  
}
复制代码

StudentDao.java

复制代码
package student.dao;

import java.util.List;

import student.bean.Student;

public interface StudentDao {

    /**
     * 添加一个学生
     * @param student
     * @return
     */
    public boolean add(Student student);
    
    /**
     * 删除一个学生
     * @param id
     * @return boolean
     */
    public boolean delete(String id);
    
    /**
     * 更新
     * @param student
     * @return
     */
    public boolean update(Student student);
    
    /**
     * 获取所有的学生
     * @return
     */
    public List<Student> getAllStudent();
    
    /**
     * 根据客户的编号查询客户
     * @param id
     * @return 成功则返回此用户,否则返回null
     */
    public Student findStudentById(String id);
}
复制代码

StudentDaoImpl.java

复制代码
package student.dao.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//import com.mysql.jdbc.PreparedStatement;


import student.bean.Student;
import student.dao.StudentDao;
import student.utils.JdbcUtils;

public class StudentDaoImpl implements StudentDao {

    @Override
    public boolean add(Student student) {
        //拿到连接对象
        Connection conn = JdbcUtils.getConnection();
        //创建预处理命令对象
        PreparedStatement pstmt = null;
        int n = 0;
        try {
            pstmt = conn.prepareStatement("insert into student(id,name,gender,birthday,cellphone,email,hobby,type,description) "
                    + "values(?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1, student.getId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getGender());
            pstmt.setDate(4, new java.sql.Date(student.getBirthday().getTime()));
            pstmt.setString(5, student.getCellphone());
            pstmt.setString(6, student.getEmail());
            pstmt.setString(7, student.getHobby());
            pstmt.setString(8, student.getType());
            pstmt.setString(9, student.getDescription());
            
            n = pstmt.executeUpdate();
            System.out.println("插入语句执行结果-----" + n);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //此处是null,没有使用Resultset
            JdbcUtils.release(null, pstmt, conn);
        }
        return n > 0 ? true : false;
    }

    @Override
    public boolean delete(String id) {
        //拿到连接对象
                Connection conn = JdbcUtils.getConnection();
                //创建预处理命令对象
                PreparedStatement pstmt = null;
                int n = 0;
                try {
                    pstmt = conn.prepareStatement("delete from student where id = ?");
                    pstmt.setString(1, id);
                    n = pstmt.executeUpdate();
                    System.out.println("删除语句执行结果-----" + n);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtils.release(null, pstmt, conn);
                }
                return n > 0 ? true : false;
    }

    @Override
    public boolean update(Student student) {
        //拿到连接对象
                Connection conn = JdbcUtils.getConnection();
                //创建预处理命令对象
                PreparedStatement pstmt = null;
                int n = 0;
                try {
                    pstmt = conn.prepareStatement("update student set name=?,gender=?,birthday=?,cellphone=?,email=?,hobby=?,type=?,description=? "
                            + "where id=?");
                    
                    pstmt.setString(1, student.getName());
                    pstmt.setString(2, student.getGender());
                    pstmt.setDate(3, new java.sql.Date(student.getBirthday().getTime()));
                    pstmt.setString(4, student.getCellphone());
                    pstmt.setString(5, student.getEmail());
                    pstmt.setString(6, student.getHobby());
                    pstmt.setString(7, student.getType());
                    pstmt.setString(8, student.getDescription());
                    pstmt.setString(9, student.getId());
                    
                    n = pstmt.executeUpdate();
                    System.out.println("插入语句执行结果-----" + n);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtils.release(null, pstmt, conn);
                }
                return n > 0 ? true : false;
    }

    @Override
    public List<Student> getAllStudent() {
        //拿到连接对象
        Connection conn = JdbcUtils.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Student> list = new ArrayList<Student>();
        //创建预处理命令对象
        
        try {
            pstmt = conn.prepareStatement("select id,name,gender,birthday,cellphone,email,hobby,type,description from student");
            //执行sql语句
            rs = pstmt.executeQuery();
            System.out.println("---rs---" + rs);
            while(rs.next()){
                //封装数据
                Student s = new Student();
                try {
                    //由于通过加密生成的id含有特殊符号,在传输到页面上的时候特殊符号不能正常显示,所以会产生无法删除用户的问题
// 所以需要指定编码
String id = URLEncoder.encode(rs.getString("id"),"UTF-8"); s.setId(id); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } s.setName(rs.getString("name")); s.setGender(rs.getString("gender")); s.setBirthday(rs.getDate("birthday")); s.setCellphone(rs.getString("cellphone")); s.setEmail(rs.getString("email")) ; s.setHobby(rs.getString("hobby")) ; s.setType(rs.getString("type")) ; s.setDescription(rs.getString("description")) ; list.add(s); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JdbcUtils.release(rs, pstmt, conn); } return list; } @Override public Student findStudentById(String id) { //拿到连接对象 Connection conn = JdbcUtils.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; Student s = new Student(); // List<Student> list = new ArrayList<Student>(); //创建预处理命令对象 try { pstmt = conn.prepareStatement("select name,gender,birthday,cellphone,email,hobby,type,description from student where id='" + id +"' "); //执行sql语句 rs = pstmt.executeQuery(); System.out.println("---FindstudentById---" + rs); while(rs.next()){ //封装数据 s.setId(id); s.setName(rs.getString("name")); s.setGender(rs.getString("gender")); s.setBirthday(rs.getDate("birthday")); s.setCellphone(rs.getString("cellphone")); s.setEmail(rs.getString("email")) ; s.setHobby(rs.getString("hobby")) ; s.setType(rs.getString("type")) ; s.setDescription(rs.getString("description")) ; } } catch (SQLException e) { e.printStackTrace(); }finally{ JdbcUtils.release(null, pstmt, conn); } return s; } }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值