时间:2021-07-01 10:21:17 帮助过:13人阅读
//给出工厂类
package cn.code.dao; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class UserDaoFactory { static Properties props =null; static String impclassname =null; static{ props = new Properties(); InputStream in = UserDaoFactory.class.getClassLoader().getResourceAsStream("UserDao.properties"); try { props.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } impclassname = props.getProperty("cn.code.dao.UserDao"); } public static UserDao getUserdaoimp(){ try { Class clazz = Class.forName(impclassname); try { return (UserDao)clazz.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } }
//工厂类配置文件
cn.code.dao.UserDao=cn.code.dao.UserDaoImp
//实现类
package cn.code.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import cn.code.DBUtils1.DBUtils1; import cn.code.domain.User; public class UserDaoImp implements UserDao { @Override public void add(User form) { Connection con = DBUtils1.getConnection(); PreparedStatement ps =null; String sql = "INSERT INTO user values(?,?)"; try { ps = con.prepareStatement(sql); ps.setString(1,form.getUsername()); ps.setString(2,form.getPassword()); ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(); }finally{ if(ps!=null) try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(con!=null) try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public User find(String username) { Connection con = DBUtils1.getConnection(); String sql = "select * from user where username=?"; try { PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,username); ResultSet rs = ps.executeQuery(); if(rs==null){ return null; } if(rs.next()){ User user = new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); return user; }else{ return null; } } catch (SQLException e) { throw new RuntimeException(e); } } }
//service层
package cn.code.service; import cn.code.dao.UserDao; import cn.code.dao.UserDaoFactory; import cn.code.domain.User; public class UserService { private UserDao userDaoImp = UserDaoFactory.getUserdaoimp(); public void regist(User user)throws UserException{ /** * 使用用户名去查询,如果返回null,完成添加 * 如果返回布市null,抛出异常! * */ User u = userDaoImp.find(user.getUsername()); if(u!=null)throw new UserException("用户名"+user.getUsername()+"已经被注册!"); userDaoImp.add(user); } public User login(User user) throws UserException { User u = userDaoImp.find(user.getUsername()); if(u==null)throw new UserException("用户名不存在!"); if(!u.getPassword().equals(user.getPassword())){ throw new UserException("密码错误"); } return u; } }
//domain层
package cn.code.domain; /* * 实体类 * */ public class User { private String username; private String password; private String verifyCode; @Override public String toString() { return "User [username=" + username + ", password=" + password + ", verifyCode=" + verifyCode + "]"; } public User(String username, String password, String verifyCode) { super(); this.username = username; this.password = password; this.verifyCode = verifyCode; } 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; } public String getVerifyCode() { return verifyCode; } public void setVerifyCode(String verifyCode) { this.verifyCode = verifyCode; } public User() { } }
JDBC面向接口编程
标签:this als form 完成 input resource exception ati find