packagecom.hjf.dao;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importcom.hjf.entity.Course;importcom.hjf.util.DBUtil;/*** 璇剧▼Dao
* Dao灞傛搷浣滄暟鎹�
*@authorHu
**/
public classCourseDao {/*** 娣诲姞
*@paramcourse
*@return
*/
public booleanadd(Course course) {
String sql= "insert into course(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";//鍒涘缓鏁版嵁搴撻摼鎺�
Connection conn =DBUtil.getConn();
Statement state= null;boolean f = false;int a = 0;try{
state=conn.createStatement();
state.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}finally{//鍏抽棴杩炴帴
DBUtil.close(state, conn);
}if (a > 0) {
f= true;
}returnf;
}/*** 鍒犻櫎
*
*@paramid
*@return
*/
public boolean delete (intid) {boolean f = false;
String sql= "delete from course where id='" + id + "'";
Connection conn=DBUtil.getConn();
Statement state= null;int a = 0;try{
state=conn.createStatement();
a=state.executeUpdate(sql);
}catch(SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(state, conn);
}if (a > 0) {
f= true;
}returnf;
}/*** 淇敼
*@paramname
*@parampass*/
public booleanupdate(Course course) {
String sql= "update course set name='" + course.getName() + "', teacher='" + course.getTeacher() + "', classroom='" +course.getClassroom()+ "' where id='" + course.getId() + "'";
Connection conn=DBUtil.getConn();
Statement state= null;boolean f = false;int a = 0;try{
state=conn.createStatement();
a=state.executeUpdate(sql);
}catch(SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(state, conn);
}if (a > 0) {
f= true;
}returnf;
}/*** 楠岃瘉璇剧▼鍚嶇О鏄惁鍞竴
* true --- 涓嶅敮涓�
*@paramname
*@return
*/
public booleanname(String name) {boolean flag = false;
String sql= "select name from course where name = '" + name + "'";
Connection conn=DBUtil.getConn();
Statement state= null;
ResultSet rs= null;try{
state=conn.createStatement();
rs=state.executeQuery(sql);while(rs.next()) {
flag= true;
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(rs, state, conn);
}returnflag;
}/*** 閫氳繃ID寰楀埌璇剧▼淇℃伅
*@paramid
*@return
*/
public Course getCourseById(intid) {
String sql= "select * from course where id ='" + id + "'";
Connection conn=DBUtil.getConn();
Statement state= null;
ResultSet rs= null;
Course course= null;try{
state=conn.createStatement();
rs=state.executeQuery(sql);while(rs.next()) {
String name= rs.getString("name");
String teacher= rs.getString("teacher");
String classroom= rs.getString("classroom");
course= newCourse(id, name, teacher, classroom);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(rs, state, conn);
}returncourse;
}/*** 閫氳繃name寰楀埌Course
*@paramname
*@return
*/
publicCourse getCourseByName(String name) {
String sql= "select * from course where name ='" + name + "'";
Connection conn=DBUtil.getConn();
Statement state= null;
ResultSet rs= null;
Course course= null;try{
state=conn.createStatement();
rs=state.executeQuery(sql);while(rs.next()) {int id = rs.getInt("id");
String teacher= rs.getString("teacher");
String classroom= rs.getString("classroom");
course= newCourse(id, name, teacher, classroom);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(rs, state, conn);
}returncourse;
}/*** 鏌ユ壘
*@paramname
*@paramteacher
*@paramclassroom
*@return
*/
public Listsearch(String name, String teacher, String classroom) {
String sql= "select * from course where ";if (name != "") {
sql+= "name like '%" + name + "%'";
}if (teacher != "") {
sql+= "teacher like '%" + teacher + "%'";
}if (classroom != "") {
sql+= "classroom like '%" + classroom + "%'";
}
List list = new ArrayList<>();
Connection conn=DBUtil.getConn();
Statement state= null;
ResultSet rs= null;try{
state=conn.createStatement();
rs=state.executeQuery(sql);
Course bean= null;while(rs.next()) {int id = rs.getInt("id");
String name2= rs.getString("name");
String teacher2= rs.getString("teacher");
String classroom2= rs.getString("classroom");
bean= newCourse(id, name2, teacher2, classroom2);
list.add(bean);
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(rs, state, conn);
}returnlist;
}/*** 鍏ㄩ儴鏁版嵁
*@paramname
*@paramteacher
*@paramclassroom
*@return
*/
public Listlist() {
String sql= "select * from course";
List list = new ArrayList<>();
Connection conn=DBUtil.getConn();
Statement state= null;
ResultSet rs= null;try{
state=conn.createStatement();
rs=state.executeQuery(sql);
Course bean= null;while(rs.next()) {int id = rs.getInt("id");
String name2= rs.getString("name");
String teacher2= rs.getString("teacher");
String classroom2= rs.getString("classroom");
bean= newCourse(id, name2, teacher2, classroom2);
list.add(bean);
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(rs, state, conn);
}returnlist;
}
}