JDBC programming-optimization program (6)

Source: Internet
Author: User

JDBC programming-optimization program (6)
Write the DTO class first

The DTO class is a data tranfer object, that is, a data transmission class. DTO is mainly used for data transmission operations, including attribute values, constructor methods, getter methods, and setter methods, and does not contain business logic.
The first is the Identity class, which is an abstract class.

public abstract class IdEntity {    protected long id;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }}

The Address and User classes correspond to the tables tbl_user and tbl_address in the database respectively.
The Code is as follows:

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 class code:

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 class implementation

DAO is the abbreviation of data access object.
The PreparedStatement class is used. placeholders can be used to set parameters.
Execute at last.
The Code is as follows:
First, set the excuse 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;}

Specific implementation class code:

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(); }}

Write a test class to insert a row of data into tbl_user.
The Code is as follows:

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(); } } }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.