packagexust.demo.stu.dao;importjava.sql.*;importjava.util.ArrayList;importxust.stu.Result;importxust.stu.ConnectionUtil;importxust.demo.stu.domain.Student;publicclassStudentDaoImplimplementsStudentDao{publicResultinit(){Resultres=newResult(false);Connectioncon=null;Statementstmt=null;Stringsql="create table Student("+"no TEXT primary key,"+"name TEXT not null,"+"gender TEXT,"+"age INTEGER,"+"dept TEXT"+");";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.createStatement();ResultSetrs=stmt.executeQuery(String.format("select count(*) from sqlite_master where type = 'table' and Upper(name) = '%s'","Student".toUpperCase()));while(rs.next()){intcount=rs.getInt(1);if(count==0&&stmt.executeUpdate(sql)>0){res.code=0;}}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closeStmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}/** * 对象填充到关系 */privatevoidtoR(Studento,PreparedStatementstmt)throwsException{if(o!=null&&stmt!=null){Stringno=o.getNo();if(no!=null){stmt.setString(1,no);}Stringname=o.getName();if(name!=null){stmt.setString(2,name);}Stringgender=o.getGender();if(gender!=null){stmt.setString(3,gender);}Integerage=o.getAge();if(age!=null){stmt.setInt(4,age);}Stringdept=o.getDept();if(dept!=null){stmt.setString(5,dept);}}}/** * 关系填充到对象 */privateStudenttoO(ResultSetrs)throwsException{Studentres=null;if(rs!=null){res=newStudent();res.setNo(rs.getString("no"));res.setName(rs.getString("name"));res.setGender(rs.getString("gender"));res.setAge(rs.getInt("age"));res.setDept(rs.getString("dept"));}returnres;}publicResultcreate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="insert into Student values (?, ?, ?, ?, ?)";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultdelete(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="delete from Student where no = ?;";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);res.code=0;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultupdate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);stmt.setString(6,o.getNo());if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultget(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student where no = ?";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);ResultSetrs=stmt.executeQuery();Studentlast=null;while(rs.next()){last=newStudent();last.setNo(rs.getString("no"));last.setName(rs.getString("name"));last.setGender(rs.getString("gender"));last.setAge(rs.getInt("age"));last.setDept(rs.getString("dept"));}res.code=0;res.data=last;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultgetAll(){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);ResultSetrs=stmt.executeQuery();ArrayList<Student>data=newArrayList<Student>();while(rs.next()){Studentlast=toO(rs);data.add(last);}res.code=0;res.data=data;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}}
packagexust.demo.stu.dao;importjava.sql.*;importjava.util.ArrayList;importxust.stu.Result;importxust.stu.ConnectionUtil;importxust.demo.stu.domain.Student;publicclassStudentDaoImplimplementsStudentDao{publicResultinit(){Resultres=newResult(false);Connectioncon=null;Statementstmt=null;Stringsql="create table Student("+"no TEXT primary key,"+"name TEXT not null,"+"gender TEXT,"+"age INTEGER,"+"dept TEXT"+");";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.createStatement();ResultSetrs=stmt.executeQuery(String.format("select count(*) from sqlite_master where type = 'table' and Upper(name) = '%s'","Student".toUpperCase()));while(rs.next()){intcount=rs.getInt(1);if(count==0&&stmt.executeUpdate(sql)>0){res.code=0;}}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closeStmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}/** * 对象填充到关系 */privatevoidtoR(Studento,PreparedStatementstmt)throwsException{if(o!=null&&stmt!=null){Stringno=o.getNo();if(no!=null){stmt.setString(1,no);}Stringname=o.getName();if(name!=null){stmt.setString(2,name);}Stringgender=o.getGender();if(gender!=null){stmt.setString(3,gender);}Integerage=o.getAge();if(age!=null){stmt.setInt(4,age);}Stringdept=o.getDept();if(dept!=null){stmt.setString(5,dept);}}}/** * 关系填充到对象 */privateStudenttoO(ResultSetrs)throwsException{Studentres=null;if(rs!=null){res=newStudent();res.setNo(rs.getString("no"));res.setName(rs.getString("name"));res.setGender(rs.getString("gender"));res.setAge(rs.getInt("age"));res.setDept(rs.getString("dept"));}returnres;}publicResultcreate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="insert into Student values (?, ?, ?, ?, ?)";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultdelete(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="delete from Student where no = ?;";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);res.code=0;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultupdate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);stmt.setString(6,o.getNo());if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultget(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student where no = ?";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);ResultSetrs=stmt.executeQuery();Studentlast=null;while(rs.next()){last=newStudent();last.setNo(rs.getString("no"));last.setName(rs.getString("name"));last.setGender(rs.getString("gender"));last.setAge(rs.getInt("age"));last.setDept(rs.getString("dept"));}res.code=0;res.data=last;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultgetAll(){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);ResultSetrs=stmt.executeQuery();ArrayList<Student>data=newArrayList<Student>();while(rs.next()){Studentlast=toO(rs);data.add(last);}res.code=0;res.data=data;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}}