JDBC——封装数据库连接操作

创建 JDBCUtils 类


package Test;

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

/**
 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 11:28 2021/9/14
 */
public class JDBCUtils {
    // 定义相关属性(4个),以为只需要一份,所以我们定义成 static 变量
    private static String user;
    private static String password;
    private static String url;
    private static String driver;

    // 在静态块种初始化以上属性
    static {
        try {
            // 通过 Properties 获取配置文件的值
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));

            user = properties.getProperty("user");
            password = properties.getProperty("password");
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
        } catch (IOException e) {
//            e.printStackTrace();

            // 在实际开发中进行可以进行如下处理
            // 1. 将编译异常转为运行异常
            // 2. 这样,调用者可以选择捕获该异常,也可以选择默认抛出,比较方便
            throw new RuntimeException(e);
        }
    }

    // 获取数据库连接,返回 Connection 对象
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
//            e.printStackTrace();

            // 在实际开发中进行可以进行如下处理
            // 1. 将编译异常转为运行异常
            // 2. 这样,调用者可以选择捕获该异常,也可以选择默认抛出,比较方便
            throw new RuntimeException(e);
        }
    }

    // 关闭相关资源
    /*
        ResultSet
        Statement 或这 PreparedStatement
        Connection
     */
    public static void close(ResultSet rs, Statement st, Connection conn){
        // 判断是否为 null
        try{
            if(rs != null){
                rs.close();
            }
            if(st != null){
                st.close();
            }
            if(conn != null){
                conn.close();
            }
        }catch (SQLException e){
//            e.printStackTrace();

            // 在实际开发中进行可以进行如下处理
            // 1. 将编译异常转为运行异常
            // 2. 这样,调用者可以选择捕获该异常,也可以选择默认抛出,比较方便
            throw new RuntimeException(e);
        }
    }


}



DML 语句测试



package Test;

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

/**
 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 11:42 2021/9/14
 */
public class Test11 {
    public static void main(String[] args){
        // 获取连接
        Connection conn = null;
        // SQL 语句
        String sql = "update `admin` set password = ? where name = ?";
        PreparedStatement ps = null;

        try {
            // 得到连接
            conn = JDBCUtils.getConnection();
            // 创建 PreparedStatement
            ps = conn.prepareStatement(sql);

            // 给占位符赋值
            ps.setString(1, "356");
            ps.setString(2, "Sherry");
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, ps, conn);
        }


    }
}



DQL 语句测试



package Test;

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

/**
 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 14:27 2021/9/14
 */
public class Test12 {
    public static void main(String[] args) {

        // 连接
        Connection conn = null;
        // SQL 语句
        String sql = "select `name`, password from admin";
        PreparedStatement ps = null;
        ResultSet rs = null;

        try{
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
//                String name = rs.getString("name");
//                String password = rs.getString("password");
                String name = rs.getString(1);
                String password = rs.getString(2);
                System.out.println(name + "\t" + password);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs, ps, conn);
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值