标签:
1,数据库连接类
package com.pr.connection; import java.sql.*; public class DataBaseConnection { private final String DBDRIVER = "com.mysql.jdbc.Driver"; private final String DBURL = "jdbc:mysql://localhost:3306/javaweb"; private final String DBUSER = "root"; private final String DBPASSWORD = "123456"; private Connection conn = null; public DataBaseConnection(){ try{ Class.forName(DBDRIVER); this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); } catch(Exception e){ System.out.println("加载驱动失败"); } } public Connection getConnection(){ return conn; } public void close(){ try{ conn.close(); } catch(Exception e){ System.out.println("数据库连接关闭失败"); } } } |
2,VO类(VALUE OBJECT / VIEW OBJECT)
package com.pr.vo; public class User { private int userid; private String username; private String password; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
3,DAO接口
package com.pr.dao; import java.util.List; import com.pr.vo.User; public interface IUser { public void insert(User user) throws Exception; public void update(User user) throws Exception; public void delete(int userId) throws Exception; public User queryById(int userId) throws Exception; public List<User> queryAll() throws Exception; } |
4,DAO实现类
package com.pr.dao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.pr.vo.User; import com.pr.connection.DataBaseConnection; public class UserImplement implements IUser { public void insert(User user) throws Exception { String sql = "insert into user(username,password) values (?,?)"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; try{ dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setString(1,user.getUsername()); pstmt.setString(2,user.getPassword()); pstmt.executeUpdate(); pstmt.close(); } catch(Exception e){ throw new Exception("操作出现异常"); } finally{ dbc.close(); } } public void update(User user) throws Exception { String sql = "update user set username=?,password=? where userid=?"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; try{ dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setString(1,user.getUsername()); pstmt.setString(2,user.getPassword()); pstmt.setInt(3,user.getUserid()); pstmt.executeUpdate(); pstmt.close(); } catch(Exception e){ throw new Exception("操作出现异常"); } finally{ dbc.close(); } } public void delete(int userId) throws Exception { String sql = "delete from user where userid=?"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; try{ dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setInt(1,userId); pstmt.executeUpdate(); pstmt.close(); } catch(Exception e){ throw new Exception("操作出现异常"); } finally{ dbc.close(); } } public User queryById(int userId) throws Exception { User user = null; String sql = "select * from user where userid=?"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; try{ dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); pstmt.setInt(1,userId); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ user = new User(); user.setUserid(rs.getInt(1)); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); } rs.close(); pstmt.close(); } catch(Exception e){ throw new Exception("操作出现异常"); } finally{ dbc.close(); } return user; } public List<User> queryAll() throws Exception { List<User> all = new ArrayList<User>(); String sql = "select * from user"; PreparedStatement pstmt = null; DataBaseConnection dbc = null; try{ dbc = new DataBaseConnection(); pstmt = dbc.getConnection().prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ User user = new User(); user.setUserid(rs.getInt(1)); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); all.add(user); } rs.close(); pstmt.close(); } catch(Exception e){ throw new Exception("操作出现异常"); } finally{ dbc.close(); } return all; } } |
5,DAO工厂类
package com.pr.dao; public class DAOFactory { public static UserImplement getUserDAOInstance(){ return new UserImplement(); } } |
标签:
原文地址:http://www.cnblogs.com/joesphos/p/4482364.html