17

    技术2022-05-12  3

    package five.domain;import java.sql.Date;

    public class User {

     private int id; private String name; private String password; private Date brithday; private float monney;

     public int getId() {  return id; }

     public void setId(int id) {  this.id = id; }

     public String getName() {  return name; }

     public void setName(String name) {  this.name = name; }

     public Date getBrithday() {  return brithday; }

     public void setBrithday(Date brithday) {  this.brithday = brithday; }

     public float getMonney() {  return monney; }

     public void setMonney(float monney) {  this.monney = monney; }

     public String getPassword() {  return password; }

     public void setPassword(String password) {  this.password = password; }

     public User(int id, String name, String password, Date brithday,   float monney) {  super();  this.id = id;  this.name = name;  this.password = password;  this.brithday = brithday;  this.monney = monney; } public User() {  super(); } public User(String name) {

      this.name = name;

     }public String toString() { return "name = " + this.name +         " id = "  + this.id +         " brithday = " + this.brithday +     " monney = " + this.monney; }}///

    package five.dao;

    import java.util.List;

    import five.domain.User;

    public interface UserDao {

     public void addUser(User user);

     public User getUser(int userId);

     public void updataUser(User user);

     public void deleteUser(User user);

     public List<User> findUser(String name, String password);

    }

    ///

    package five.daoImpl;

    import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;

    import five.dao.UserDao;import five.dao.myException.MyUserException;import five.domain.User;import five.utils.Utils;

    public class UserDaoImpl implements UserDao {

     public void addUser(User user) {

      Connection connection = null;  PreparedStatement preparedStatement = null;

      try {

       connection = Utils.getConnection();   String sqlString = " insert into user(id, name, brithday, monney, password) value(?,?,?,?,?)";   preparedStatement = connection.prepareStatement(sqlString);   preparedStatement.setInt(1, user.getId());   preparedStatement.setString(2, user.getName());   preparedStatement.setFloat(4, user.getMonney());   preparedStatement.setString(5, user.getPassword());   preparedStatement.setDate(3, new java.sql.Date(user.getBrithday()     .getTime()));

       // 4.执行语句   int i = preparedStatement.executeUpdate();

       System.out.println("新建" + i + "条记录");  } catch (SQLException e) {            // 为什么 捕获了异常 不能什么也不做?   // 原因 1 不利于debug    // 原因 2 异常 转移   //       例如 在注册用户 出现异常 没有任何处理   //       那么 注册出现异常时, 这个异常就被隐藏掉了   //       当用户 在检索 更新 这个用户的时候 就可能   //       出现异常了。由 系统就 转移。   // 怎样处理 异常?   // 方法一  捕获解决掉(打印)   //        但是这个异常应该 抛出到业务层, 作处理,提醒用户再注册   // 怎样抛出异常?   //        原封不动的抛出去?那样 就要修改接口的签名, (实现接口类抛出的异常是   //        接口声明异常的子集。)   //        导致的结果是 业务层在调用接口实现业务逻辑时, 就要处理 持久层的逻辑   //        有悖于 MVC 三层构架 的思想 (将jdbc 转换成 hibernate?)   //        解决 捕获到异常(编译异常)转换成的 运行异常 抛出去   //        编译异常 必须要处理 运行异常 不是必须要处理   throw new MyUserException(e.getMessage(), e);  } finally {   Utils.free(connection, preparedStatement, null);  }

     }

     public void deleteUser(User user) {  // TODO Auto-generated method stub

     }

     public List<User> findUser(String name, String password) {  // TODO Auto-generated method stub  return null; }

     public User getUser(int userId) {  // TODO Auto-generated method stub  return null; }

     public void updataUser(User user) {  // TODO Auto-generated method stub

     }}

    ///


    最新回复(0)