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

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

}
阅读更多
文章标签: 数据库 优化
个人分类: mysql
想对作者说点什么? 我来说一句

jdbc-数据库连接和文件配置

2014年01月19日 3KB 下载

传智播客-Jdbc-李勇.ppt

2011年04月21日 196KB 下载

jdbc数据库连接 模板代码

2008年10月17日 1.49MB 下载

数据库连接示例程序二

2009年07月22日 28KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭