实现增删改查的封装

获取数据连接

不使用Druid封装
package Utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * 封装JDBC工具类
 * 功能:
 * 1.获取连接
 * 2.释放资源
 */
public class JDBCUtils {
    static String url;
    static String password;
    static String driver;
    static String user;
       static {
           try {
               Properties info = new Properties();
               info.load(new FileInputStream("src\\jdbc.properties"));
                url = info.getProperty("url");
                user = info.getProperty("username");
                driver  = info.getProperty("driverClassName");
                password = info.getProperty("password");
               Class.forName(driver);
           } catch (IOException e) {
               e.printStackTrace();
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
       }
    public static Connection getConnection() throws Exception{
        return DriverManager.getConnection(url, user, password);
    }


    public static void close(Statement st,Connection conn){
        if(st != null){
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }



    public static void close(ResultSet rs,Statement ps,Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(ps != null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

使用Druid封装
package Utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtilsByDruid {
    static DataSource ds;
    static{
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\JDBC.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws Exception {
        return  ds.getConnection();
    }


    public static void close(ResultSet rs,Statement ps,  Connection conn){

        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(ps != null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void close(Statement st,Connection conn){
        if(st != null){
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}
封装怎删改查方法
package Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CRUDUtils {

    /**
     * 执行则增删改
     * @param sql 执行的sql语句
     * @param params 占位符
     * @return
     */
    public static int update(String sql,Object... params){
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            //1.获取连接
            connection = JDBCUtilsByDruid.getConnection();
            //2.执行sql语句
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {//  params.length 获取可变长度的长度
                ps.setObject(i + 1,params[i]);
            }
            int update = ps.executeUpdate();
            return update;
        } catch (Exception e) {
           throw new RuntimeException();
        }finally {
            JDBCUtilsByDruid.close(ps,connection);
        }
    }

    public static user queryString(String sql,Object... params) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1.获取连接
             connection = JDBCUtilsByDruid.getConnection();
            //2.执行查询
             ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1,params[i]);
            }
            rs = ps.executeQuery();
            if(rs.next()){
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String email = rs.getString("email");
                user user = new user(id,username,password,email);
                return user;
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException();
        } finally {
            JDBCUtilsByDruid.close(rs,ps,connection);
        }
    }
    public static List<user> queryAll(String sql, Object... params) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1.获取连接
            connection = JDBCUtilsByDruid.getConnection();
            //2.执行查询
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1,params[i]);
            }
            rs = ps.executeQuery();
            List<user> list = new ArrayList<>();
            while(rs.next()){
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String email = rs.getString("email");
                user user = new user(id,username,password,email);
                list.add(user);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException();
        } finally {
            JDBCUtilsByDruid.close(rs,ps,connection);
        }
    }


}

数据库user表
package Utils;

public class user {

    private Integer id;
    private String username;
    private String password;
    private String email;

    public user() {
    }

    public user(Integer id, String username, String password, String email) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    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{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值