程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> JDBC編程-優化程序(六)

JDBC編程-優化程序(六)

編輯:DB2教程

JDBC編程-優化程序(六)


首先完成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", "[email protected]");
            userDao.save(conn, tom);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved