| package com.henu.dao.Impl;
import com.henu.bean.Student; import com.henu.dao.StudentDAO; import com.henu.utils.JDBCUtil;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
public class StudentDAOImpl implements StudentDAO { ????@Override ????public int insert(Student stu) { ????????Connection con = JDBCUtil.getCon();
????????int res = 0; ????????String sql = "insert into student values (null,?,?,?)"; ????????PreparedStatement ps = null; // ???????ps.setString(); ????????try { ????????????ps = con.prepareStatement(sql); ????????????ps.setObject(1,stu.getName()); ????????????ps.setObject(2,stu.getAge()); ????????????ps.setObject(3,stu.getDate()); ????????????res = ps.executeUpdate(); ????????} catch (SQLException e) { ????????????e.printStackTrace(); ????????}finally { ????????????JDBCUtil.close(con,ps,null); ????????} ????????return res; ????}
????@Override ????public int delect(int id) { ????????Connection con = JDBCUtil.getCon(); ????????PreparedStatement ps = null; ????????String sql = "delete from student where id=?"; ????????int res = 0; ????????try { ????????????ps = con.prepareStatement(sql); ????????????ps.setInt(1,id); ????????????res = ps.executeUpdate(); ????????} catch (SQLException e) { ????????????e.printStackTrace(); ????????}finally { ????????????JDBCUtil.close(con,ps,null); ????????} ????????return res; ????}
????@Override ????public void update(Student stu) { ????????Connection con = JDBCUtil.getCon(); ????????PreparedStatement ps = null; ????????String sql = " update student set name=?,age=?,birthday=? where id=? "; ????????try { ????????????ps = con.prepareStatement(sql); ????????????ps.setObject(1,stu.getName()); ????????????ps.setObject(2,stu.getAge()); ????????????ps.setObject(3,stu.getDate()); ????????????ps.setObject(4,stu.getId()); ????????????int res = ps.executeUpdate(); ????????????System.out.println(res >0 ? "更新成功" : "更新失敗"); ????????} catch (SQLException e) { ????????????e.printStackTrace(); ????????}finally{ ????????????JDBCUtil.close(con,ps,null); ????????} ????}
????@Override ????public Student findStuById(int id) { ????????Connection con = JDBCUtil.getCon(); ????????PreparedStatement ps = null; ????????ResultSet rs = null; ????????Student stu = null; ????????String sql = " select *?from student where id=? "; ????????try { ????????????ps = con.prepareStatement(sql); ????????????ps.setInt(1,id); ????????????rs = ps.executeQuery(); ????????????while(rs.next()){ ????????????????stu = new Student(rs.getInt("id"),rs.getString("name"), ????????????????????????+rs.getInt("age"),rs.getDate("birthday")); ????????????} ????????} catch (SQLException e) { ????????????e.printStackTrace(); ????????}finally{ ????????????JDBCUtil.close(con,ps,rs); ????????} ????????return stu; ????}
????@Override ????public Student findStuByName(String name) { ????????Connection con = JDBCUtil.getCon(); ????????PreparedStatement ps = null; ????????ResultSet rs = null; ????????Student stu = null; ????????String sql = " select *?from student where name=? "; ????????try { ????????????ps = con.prepareStatement(sql); ????????????ps.setString(1,name); ????????????rs = ps.executeQuery(); ????????????while(rs.next()){ ????????????????stu = new Student(rs.getInt("id"),rs.getString("name"), ????????????????????????+rs.getInt("age"),rs.getDate("birthday")); ????????????} ????????} catch (SQLException e) { ????????????e.printStackTrace(); ????????}finally{ ????????????JDBCUtil.close(con,ps,rs); ????????} ????????return stu; ????}
????@Override ????public List<Student> findAll() { ????????Connection con = JDBCUtil.getCon(); ????????PreparedStatement ps = null; ????????ResultSet rs = null; ????????Student stu = null; ????????List<Student> list = new ArrayList<>(); ????????String sql = " select *?from student"; ????????try { ????????????ps = con.prepareStatement(sql); ????????????rs = ps.executeQuery(); ????????????while(rs.next()){ ????????????????stu = new Student(rs.getInt("id"),rs.getString("name"), ????????????????????????+rs.getInt("age"),rs.getDate("birthday")); ????????????????list.add(stu); ????????????} ????????} catch (SQLException e) { ????????????e.printStackTrace(); ????????}finally{ ????????????JDBCUtil.close(con,ps,rs); ????????} ????????return list; ????} } ? |