1、首先新建基础连接类BaseDao,在这里配置链接的数据库名称,用户名以及密码,以及执行读与写操作的父方法,代码如下:
package com.demo.dao; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; /** * 数据库操作类 * @author zhangdi * */ public class BaseDao { //数据库地址“jdbc:mysql://服务器域名:端口号/数据库名称” private String url = "jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf-8"; //用户名 private String user = "root"; //用户密码 private String pwd = "zhangdi"; //数据库链接对象 private java.sql.Connection conn; //数据库命令执行对象 private PreparedStatement pstmt; //数据库返回结果 private java.sql.ResultSet rs; //静态代码块 static{ //1、加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //2、创建连接 private void getConnection(){ if(conn == null){ try { conn = DriverManager.getConnection(url, user, pwd); } catch (SQLException e) { e.printStackTrace(); } } } //执行读操作方法 public java.sql.ResultSet executeQuery(String query, List<Object> params){ getConnection(); try { //3、创建命令执行对象 pstmt = conn.prepareStatement(query); //4、执行 if(params!=null && params.size()>0){ for(int i=0;i<params.size();i++){ pstmt.setObject(i+1, params.get(i)); } } rs = pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } //执行写操作方法 public int executeUpdate(String query, List<Object> params){ int result = 0; getConnection(); try { //3、创建命令执行对象 pstmt = conn.prepareStatement(query); //4、执行 if(params!=null && params.size()>0){ for(int i=0;i<params.size();i++){ pstmt.setObject(i+1, params.get(i)); } } //5、处理结果 result = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ //6、释放资源 this.close(); } return result; } //关闭资源 public void close(){ try { if(rs!=null){ rs.close(); rs = null; } if(pstmt!=null){ pstmt.close(); pstmt = null; } if(conn!=null){ conn.close(); conn = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2、第二步,就创建一个数据库操作接口类,采用面向接口的开发思想以便于后期维护,代码如下:
package com.demo.dao; import java.util.List; import com.demo.entity.User; public interface UserDao { public int addUser(User user); public List<User> findUsers(); public List<User> findUsers(String name); public List<User> findUsersByDept(String dept); public List<User> findUsersByRole(String role); public int delUserById(int id); public int updateUserById(int id,User role); public boolean checkUser(String name); }
3、第三步创建数据库操作对象接口实现类并继承数据库操作基础类:
package com.demo.daoimpl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.demo.dao.BaseDao; import com.demo.dao.DeptDao; import com.demo.dao.RoleDao; import com.demo.dao.UserDao; import com.demo.entity.User; public class UserDaoImpl extends BaseDao implements UserDao { DeptDao deptDao = new DeptDaoImpl(); RoleDao roleDao = new RoleDaoImpl(); //添加一条用户信息 @Override public int addUser(User user) { String update = "insert into users(account,pwd,NAME,dept,role,phone,qq,email,remark)values(?,?,?,?,?,?,?,?,?)"; List<Object> params = new ArrayList<Object>(); params.add(user.getAccount()); params.add(user.getPwd()); params.add(deptDao.returnDeptIdByName(user.getDept())); params.add(roleDao.returnRoleIdByName(user.getRole())); params.add(user.getRole()); params.add(user.getPhone()); params.add(user.getQq()); params.add(user.getEmail()); params.add(user.getMark()); return this.executeUpdate(update, params); } @Override public List<User> findUsers() { List<User> result = new ArrayList<User>(); String query = "select id,account,pwd,NAME,dept,role,phone,qq,email,remark from users"; ResultSet rs = this.executeQuery(query, null); try { while(rs.next()){ int id = rs.getInt("id"); String account = rs.getString("account"); String pwd = rs.getString("pwd"); String name = rs.getString("NAME"); String dept = deptDao.returnDeptNameById(rs.getInt("dept")); String role = roleDao.returnRoleNameById(rs.getInt("role")); String phone ="11";//+ rs.getInt("phone"); String qq = "22";//+rs.getInt("qq"); String email = rs.getString("email"); String mark = rs.getString("remark"); User user = new User(id, account,pwd,name,dept,role,phone,qq,email,mark); result.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.close(); } return result; } @Override public List<User> findUsersByDept(String dept) { List<User> result = new ArrayList<User>(); List<Object> params = new ArrayList<Object>(); String query = "select id,account,pwd,NAME,dept,role,phone,qq,email,remark from users where dept =?"; if(dept!=null&&!"".equals(dept)){ int d = deptDao.returnDeptIdByName(dept); params.add(d); } ResultSet rs = this.executeQuery(query, params); try { while(rs.next()){ int id = rs.getInt("id"); String account = rs.getString("account"); String pwd = rs.getString("pwd"); String name = rs.getString("NAME"); String deptName = deptDao.returnDeptNameById(rs.getInt("dept")); String role = roleDao.returnRoleNameById(rs.getInt("role")); String phone = Integer.toString(rs.getInt("phone")); String qq = Integer.toString(rs.getInt("qq")); String email = rs.getString("email"); String mark = rs.getString("remark"); User user = new User(id, account,pwd,name,deptName,role,phone,qq,email,mark); result.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.close(); } return result; } @Override public List<User> findUsersByRole(String role) { List<User> result = new ArrayList<User>(); List<Object> params = new ArrayList<Object>(); String query = "select id,account,pwd,NAME,dept,role,phone,qq,email,remark from users where role =?"; if(role!=null&&!"".equals(role)){ int d = roleDao.returnRoleIdByName(role); params.add(d); } ResultSet rs = this.executeQuery(query, params); try { while(rs.next()){ int id = rs.getInt("id"); String account = rs.getString("account"); String pwd = rs.getString("pwd"); String name = rs.getString("NAME"); String deptName = deptDao.returnDeptNameById(rs.getInt("dept")); String roleName = roleDao.returnRoleNameById(rs.getInt("role")); String phone = Integer.toString(rs.getInt("phone")); String qq = Integer.toString(rs.getInt("qq")); String email = rs.getString("email"); String mark = rs.getString("remark"); User user = new User(id, account,pwd,name,deptName,roleName,phone,qq,email,mark); result.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.close(); } return result; } @Override public int delUserById(int id) { String query = "delete from users where id = ?"; List<Object> params = new ArrayList<Object>(); params.add(id); return this.executeUpdate(query, params); } @Override public int updateUserById(int id, User role) { // TODO Auto-generated method stub return 0; } @Override public boolean checkUser(String name) { List<User> list = new ArrayList<User>(); list = this.findUsers(); for(User u:list){ if(u.getName().equals(name)){ return true; } } return false; } @Override public List<User> findUsers(String name) { List<User> result = new ArrayList<User>(); List<Object> params = new ArrayList<Object>(); String query = "select id,account,pwd,NAME,dept,role,phone,qq,email,remark from users where 1=1"; if(name!=null&&!"".equals(name)){ query = query+" and NAME like ?"; params.add("%"+name+"%"); } ResultSet rs = this.executeQuery(query, params); try { while(rs.next()){ int id = rs.getInt("id"); String n = rs.getString("name"); String desc = rs.getString("desc"); User user = new User(); result.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.close(); } return result; } }
这里面role与dept也是两个数据库操作类,大家可以忽略,其中User为实体类,代码如下:
package com.demo.entity; public class User { private int id; private String account; private String pwd; private String name; private String dept; private String role; private String phone; private String qq; private String mark; private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getMark() { return mark; } public void setMark(String mark) { this.mark = mark; } public User() { super(); // TODO Auto-generated constructor stub } public User(int id, String account, String pwd, String name, String dept, String role, String phone, String qq, String mark, String email) { super(); this.id = id; this.account = account; this.pwd = pwd; this.name = name; this.dept = dept; this.role = role; this.phone = phone; this.qq = qq; this.mark = mark; this.email = email; } public User(String account, String pwd, String name,int id) { super(); this.account = account; this.pwd = pwd; this.name = name; this.id = id; } }
至此,web后台与mysql数据库的链接就完成了,如果要执行对数据库的操作就调用数据库操作类即可。
注:这里需要引用的jar包为:
这些为项目基础jar包: