时间:2021-07-01 10:21:17 帮助过:3人阅读
package dao; import utils.Dbtuil; import model.Course; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * Created by yinus * Date:2016/4/6 * Time:18:44 */ public class CourseDao { public void addCourse(Course course) throws SQLException { Connection connection= Dbtuil.getConnection(); String sql = "INSERT INTO db_courses" + " (cou_id, cou_title, cou_create_time, cou_update_time)" + " VALUES (?,?,?,?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,course.getId()); ps.setString(2,course.getTitle()); ps.setDate(3,new Date(course.getCreateTime().getTime())); ps.setDate(4,new Date(course.getUpdateTime().getTime())); ps.execute(); ps.close(); } public void delCourse(int id) throws SQLException { Connection connection=Dbtuil.getConnection(); String sql = " DELETE FROM db_courses" + " WHERE cou_id=?" ; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,id); ps.execute(); ps.close(); } public void updateCourse(Course course) throws SQLException { Connection connection= Dbtuil.getConnection(); String sql = "UPDATE db_courses" + " SET cou_title=?, cou_update_time = GETDATE()" + " WHERE cou_id=?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1,course.getTitle()); ps.setInt(2,course.getId()); ps.execute(); ps.close(); } public List<Course> queryCourses(String title) throws SQLException { Course course =null; List<Course> courses = new ArrayList<>(); Connection connection = Dbtuil.getConnection(); String sql = " SELECT * FROM db_courses" + " WHERE courses.cou_title LIKE ?" ; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1,"%"+title+"%"); ResultSet resultSet=ps.executeQuery(); while (resultSet.next()){ course = new Course(); course.setId(resultSet.getInt("cou_id")); course.setTitle(resultSet.getString("cou_title")); course.setCreateTime(resultSet.getDate("cou_create_time")); course.setUpdateTime(resultSet.getDate("cou_update_time")); courses.add(course); } resultSet.close(); ps.close(); return courses; } public Course querySingleCourse(int id) throws SQLException { Course course = null; Connection connection=Dbtuil.getConnection(); String sql = " SELECT * FROM db_courses" + " WHERE cou_id = ?" ; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,id); ResultSet rs=ps.executeQuery(); while (rs.next()) { course=new Course(); course.setId(rs.getInt("cou_id")); course.setTitle(rs.getString("cou_title")); course.setCreateTime(rs.getDate("cou_create_time")); course.setUpdateTime(rs.getDate("cou_update_time")); } rs.close(); ps.close(); return course; } }
package dao; import utils.Dbtuil; import model.Student; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * Created by yinus * Date:2016/4/6 * Time:18:44 */ public class StudentDao { public void addStudent(Student student) throws SQLException { Connection connection=Dbtuil.getConnection(); String sql = "insert into db_students (stu_id,stu_name,stu_create_time,stu_update_time) values (?,?,?,?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,student.getId()); ps.setString(2,student.getName()); ps.setDate(3,new Date(student.getCreateTime().getTime())); ps.setDate(4,new Date(student.getUpdateTime().getTime())); ps.execute(); ps.close(); } public void delStudent(int id) throws SQLException { Connection connection=Dbtuil.getConnection(); String sql = " DELETE FROM db_students WHERE stu_id=?" ; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,id); ps.execute(); ps.close(); } public void updateStudent(Student student) throws SQLException { Connection connection=Dbtuil.getConnection(); String sql = " UPDATE db_students SET stu_name=?,stu_create_time=?,stu_update_time=GETDATE() WHERE stu_id=?"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1,student.getName()); ps.setDate(2,new Date(student.getCreateTime().getTime())); ps.setInt(3,student.getId()); ps.execute(); ps.close(); } public List<Student> queryStudents(String name) throws SQLException { Student student=null; List<Student> students = new ArrayList<>(); Connection connection=Dbtuil.getConnection(); String sql = " SELECT * FROM db_students WHERE stu_name LIKE ?" ; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1,"%"+name+"%"); ResultSet resultSet=ps.executeQuery(); while(resultSet.next()){ student = new Student(); student.setId(resultSet.getInt("stu_id")); student.setName(resultSet.getString("stu_name")); student.setCreateTime(resultSet.getDate("stu_create_time")); student.setUpdateTime(resultSet.getDate("stu_update_time")); students.add(student); } resultSet.close(); ps.close(); return students; } public Student querySingleStudent(int id) throws SQLException { Student student=null; Connection connection=Dbtuil.getConnection(); String sql = " SELECT * FROM db_students WHERE stu_id=?" ; PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1,id); ResultSet resultSet=ps.executeQuery(); while (resultSet.next()){ student=new Student(); student.setId(resultSet.getInt("stu_id")); student.setName(resultSet.getString("stu_name")); student.setCreateTime(resultSet.getDate("stu_create_time")); student.setUpdateTime(resultSet.getDate("stu_update_time")); } resultSet.close(); ps.close(); return student; } }
第二次数据库作业--dao
标签: