按照网上的教程和素材制作,第一部分信息管理
源码
ackage com.ischoolbar.programmer.dao; import java.sql.ResultSet; import java.sql.SQLException; import com.ischoolbar.programmer.model.Admin; import com.ischoolbar.programmer.model.Clazz; /** * * @author llq *管理员数据库操作封装 */ public class AdminDao extends BaseDao { public Admin login(String name ,String password){ String sql = "select * from s_admin where name = '" + name + "' and password = '" + password + "'"; ResultSet resultSet = query(sql); try { if(resultSet.next()){ Admin admin = new Admin(); admin.setId(resultSet.getInt("id")); admin.setName(resultSet.getString("name")); admin.setPassword(resultSet.getString("password")); admin.setStatus(resultSet.getInt("status")); return admin; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public boolean editPassword(Admin admin,String newPassword) { // TODO Auto-generated method stub String sql = "update s_admin set password = '"+newPassword+"' where id = " + admin.getId(); return update(sql); } }
package com.ischoolbar.programmer.dao; 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.ischoolbar.programmer.util.DbUtil; /** * * @author llq *基础dao,封装基本操作 */ public class BaseDao { private DbUtil dbUtil = new DbUtil(); /** * 关闭数据库连接,释放资源 */ public void closeCon(){ dbUtil.closeCon(); } /** * 基础查询,多条查询 */ public ResultSet query(String sql){ try { PreparedStatement prepareStatement = dbUtil.getConnection().prepareStatement(sql); return prepareStatement.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** *改变数据库内容操作 */ public boolean update(String sql){ try { return dbUtil.getConnection().prepareStatement(sql).executeUpdate() > 0; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public Connection getConnection(){ return dbUtil.getConnection(); } }
package com.ischoolbar.programmer.dao; 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.ischoolbar.programmer.model.Admin; import com.ischoolbar.programmer.model.Clazz; import com.ischoolbar.programmer.model.Page; import com.ischoolbar.programmer.model.Student; import com.ischoolbar.programmer.util.StringUtil; public class StudentDao extends BaseDao { public boolean addStudent(Student student){ String sql = "insert into s_student values(null,'"+student.getSn()+"','"+student.getName()+"'"; sql += ",'" + student.getPassword() + "'," + student.getClazzId(); sql += ",'" + student.getSex() + "','" + student.getMobile() + "'"; sql += ",'" + student.getQq() + "',null)"; return update(sql); } public boolean editStudent(Student student) { // TODO Auto-generated method stub String sql = "update s_student set name = '"+student.getName()+"'"; sql += ",sex = '" + student.getSex() + "'"; sql += ",mobile = '" + student.getMobile() + "'"; sql += ",qq = '" + student.getQq() + "'"; sql += ",clazz_id = " + student.getClazzId(); sql += " where id = " + student.getId(); return update(sql); } public boolean setStudentPhoto(Student student) { // TODO Auto-generated method stub String sql = "update s_student set photo = ? where id = ?"; Connection connection = getConnection(); try { PreparedStatement prepareStatement = connection.prepareStatement(sql); prepareStatement.setBinaryStream(1, student.getPhoto()); prepareStatement.setInt(2, student.getId()); return prepareStatement.executeUpdate() > 0; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return update(sql); } public boolean deleteStudent(String ids) { // TODO Auto-generated method stub String sql = "delete from s_student where id in("+ids+")"; return update(sql); } public Student getStudent(int id){ String sql = "select * from s_student where id = " + id; Student student = null; ResultSet resultSet = query(sql); try { if(resultSet.next()){ student = new Student(); student.setId(resultSet.getInt("id")); student.setClazzId(resultSet.getInt("clazz_id")); student.setMobile(resultSet.getString("mobile")); student.setName(resultSet.getString("name")); student.setPassword(resultSet.getString("password")); student.setPhoto(resultSet.getBinaryStream("photo")); student.setQq(resultSet.getString("qq")); student.setSex(resultSet.getString("sex")); student.setSn(resultSet.getString("sn")); return student; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return student; } public List<Student> getStudentList(Student student,Page page){ List<Student> ret = new ArrayList<Student>(); String sql = "select * from s_student "; if(!StringUtil.isEmpty(student.getName())){ sql += "and name like '%" + student.getName() + "%'"; } if(student.getClazzId() != 0){ sql += " and clazz_id = " + student.getClazzId(); } if(student.getId() != 0){ sql += " and id = " + student.getId(); } sql += " limit " + page.getStart() + "," + page.getPageSize(); ResultSet resultSet = query(sql.replaceFirst("and", "where")); try { while(resultSet.next()){ Student s = new Student(); s.setId(resultSet.getInt("id")); s.setClazzId(resultSet.getInt("clazz_id")); s.setMobile(resultSet.getString("mobile")); s.setName(resultSet.getString("name")); s.setPassword(resultSet.getString("password")); s.setPhoto(resultSet.getBinaryStream("photo")); s.setQq(resultSet.getString("qq")); s.setSex(resultSet.getString("sex")); s.setSn(resultSet.getString("sn")); ret.add(s); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ret; } public int getStudentListTotal(Student student){ int total = 0; String sql = "select count(*)as total from s_student "; if(!StringUtil.isEmpty(student.getName())){ sql += "and name like '%" + student.getName() + "%'"; } if(student.getClazzId() != 0){ sql += " and clazz_id = " + student.getClazzId(); } if(student.getId() != 0){ sql += " and id = " + student.getId(); } ResultSet resultSet = query(sql.replaceFirst("and", "where")); try { while(resultSet.next()){ total = resultSet.getInt("total"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return total; }
package com.ischoolbar.programmer.util; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import javax.management.loading.PrivateClassLoader; /** * * @author llq *数据库连util */ public class DbUtil { private String dbUrl = "jdbc:mysql://localhost:3306/db_student_manager_web?useUnicode=true&characterEncoding=utf8"; private String dbUser = "root"; private String dbPassword = ""; private String jdbcName = "com.mysql.jdbc.Driver"; private Connection connection = null; public Connection getConnection(){ try { Class.forName(jdbcName); connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword); System.out.println("数据库链接成功!"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("数据库链接失败!"); e.printStackTrace(); } return connection; } public void closeCon(){ if(connection != null) try { connection.close(); System.out.println("数据库链接已关闭!"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub DbUtil dbUtil = new DbUtil(); dbUtil.getConnection(); } }