JDBC编程-优化程序(六)

时间:2023-12-31 17:20:08

首先完成DTO类的编写

DTO类是data tranfer object也就是数据传输类,DTO主要用于数据的传输操作,其中包含属性值,以及构造方法和getter ,setter方法等,不会包含业务逻辑。

首先是Identity类,这个是抽象类。

public abstract class IdEntity {
    protected long id;

    public long getId() {
        return id;
    }

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

Address和User类分别对应数据库中的表格tbl_user和tbl_address.

代码如下:

public class User extends IdEntity {
    private String name;
    private String password;
    private String email;
    public User(String name, String password, String email) {
        this.name = name;
        this.password = password;
        this.email = email;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", password=" + password + ", email="
                + email + ", id=" + id + "]";
    }

}

Address类的代码:

public class Address extends IdEntity {
    private String city;
    private String country;
    private String userid;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    @Override
    public String toString() {
        return "Address [city=" + city + ", country=" + country + ", userid="
                + userid + ", id=" + id + "]";
    }
}

DAO类的实现

DAO 是data access object即是数据访问对象的缩写。

使用了PreparedStatement这个类,这个类可以使用占位符,然后使用方法再来设置参数。

最后execute执行。

具体代码如下:

首先定了借口UserDao

public interface UserDao {
    public void save(Connection conn,User user) throws SQLException;
    public void update(Connection conn,Long id,User user)throws SQLException;
    public void delete(Connection conn,User user) throws SQLException;
}

具体实现类代码:

public void save(Connection conn, User user) throws SQLException {
        // TODO Auto-generated method stub
        PreparedStatement ps = conn.prepareCall("INSERT INTO tbl_user(name,password,email) VALUES (?,?,?)");
        ps.setString(1, user.getName());
        ps.setString(2, user.getPassword());
        ps.setString(3, user.getEmail());
        ps.execute();
    }

    @Override
    public void update(Connection conn, Long id, User user) throws SQLException {
        // TODO Auto-generated method stub
        String sql = "UPDATE tbl_user SET name=?,password=?,email=? WHERE id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, user.getName());
        ps.setString(2, user.getPassword());
        ps.setString(3, user.getEmail());
        ps.setLong(4, id);
        ps.execute();
    }

    @Override
    public void delete(Connection conn, User user) throws SQLException {
        // TODO Auto-generated method stub
        PreparedStatement ps = conn.prepareStatement("DELETE FROM tbl_user WHERE id=?");
        ps.setLong(1, user.getId());
        ps.execute();
    }

}

写了一个一个测试类往tbl_user里面插入一行数据。

具体代码如下:

public class DaoTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Connection conn = null;
        try {
            conn = ConnectionFactory.getInstance().makeConnection();
            conn.setAutoCommit(false);
            UserDao userDao = new UserDaoImpl();
            User tom = new User("Tom", "123456", "tom@163.com");
            userDao.save(conn, tom);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

}