关闭

JDBC-数据库连接代码优化二

标签: 数据库优化
146人阅读 评论(0) 收藏 举报
分类:

1.创建DTO类

创建IdEntity抽象实体类

package com.jdbc.entity;

public abstract class IdEntity {
    protected Long id;

    public Long getId() {
        return id;
    }

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

创建User 实体类

package com.jdbc.entity;

public class User extends IdEntity {

    private String name;
    private String password;
    private String 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 实体类

package com.jdbc.entity;

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 + "]";
    }

}

2.创建DAO类

创建接口UserDao 类

package com.jdbc.dao;

import java.sql.Connection;
import java.sql.SQLException;

import com.jdbc.entity.User;

public interface UserDao {
    public void save(Connection con, User user) throws SQLException;

    public void update(Connection con, Long id, User user) throws SQLException;

    public void delete(Connection con, User user) throws SQLException;
}

创建接口实现类

package com.jdbc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.jdbc.dao.UserDao;
import com.jdbc.entity.User;

public class UserDaoImpl implements UserDao {

    /*
     * 保存用户信息
     */
    @Override
    public void save(Connection con, User user) throws SQLException {
        PreparedStatement ps = con
                .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();
    }

    /*
     *根据用户指定的ID更新用户信息
     */
    @Override
    public void update(Connection con, Long id, User user) throws SQLException {
        String updatesql = "update tbl_user set name=?,password=?,email=? where id=?";
        PreparedStatement ps = con.prepareStatement(updatesql);
        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 con, User user) throws SQLException {
        String deletesql = "delete from tbl_user where id=?";
        PreparedStatement ps = con.prepareStatement(deletesql);
        ps.setLong(1, user.getId());
        ps.execute();
    }

}

创建测试类UserDaoTest

package com.jdbc.test;

import java.sql.Connection;

import com.jdbc.dao.UserDao;
import com.jdbc.dao.impl.UserDaoImpl;
import com.jdbc.entity.User;
import com.jdbc.util.ConnectionFactory;

public class UserDaoTest {

    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.setName("Tom");
            tom.setPassword("123456");
            tom.setEmail("tom@gmail.com");

            userDao.save(conn, tom);
            conn.commit();

        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17443次
    • 积分:490
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    最新评论