JDBC工具类

方式一、

public class JDBCUtil_v1 {

    /*
     * 获取连接方法
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            /* user是数据库名
            *  MySQL在高版本需要指明是否进行SSL连接
            */
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8",
            //数据库
             "root",
             //密码 
             "123456");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

//释放资源
    public static void release(Connection conn, PreparedStatement ps, ResultSet rs) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

db配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=utf8
username=root
password=123456

每一行后面不要留空格

方法二、

public class JDBCUtil_v2 {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

/*      
 * 静态代码块加载配置文件信息
 */
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver=bundle.getString("driver");
        url=bundle.getString("url");
        username=bundle.getString("username");
        password=bundle.getString("password");
    }
    /*
     * 获取连接方法
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
}

方法三、

public class JDBCUtil_v3 {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    /*
     * 静态代码块加载配置文件信息
     */
    static {
        try {
            // 1.通过当前类获取类加载器
            ClassLoader classLoader = JDBCUtil_v3.class.getClassLoader();
            // 2.通过类加载器的方法获得一个输入流
            InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");
            // 3.创建一个properties对象
            Properties prop = new Properties();
            // 4.加载输入流
            prop.load(resourceAsStream);
            // 5.获取相关参数
            driver = prop.getProperty("driver");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    /*
     * 获取连接方法
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

Junit测试类

/*
 * 测试工具类
 */
public class TestUtils {

    @Test
    public void TestDelById() {
        Connection connection = null;
        PreparedStatement pStatement = null;
        ResultSet executeQuery = null;

        try {
            // 1.获取连接
            connection = JDBCUtil_v3.getConnection();
            // 2.编写sql
            //  String sql="delete from student where id=?";
            String sql = "update student set name=? , age=?  where id=?";
            // 3.获取执行sql语句对象
            pStatement = connection.prepareStatement(sql);
            // 4.设置参数
            pStatement.setString(1, "曹操");
            pStatement.setInt(2, 15);
            pStatement.setInt(3, 2);
            // 5.执行
            int executeUpdate = pStatement.executeUpdate();
            if (executeUpdate>0) {
                System.out.println("success");
            }else {
                System.out.println("err");
            }           

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            JDBCUtil_v3.release(connection, pStatement, executeQuery);
        }
    }

    @Test
    public void TestFindUserById() {
        Connection connection = null;
        PreparedStatement pStatement = null;
        ResultSet executeQuery = null;

        try {
            // 1.获取连接
            connection = JDBCUtil_v2.getConnection();
            // 2.编写sql
            String sql = "select * from student where id=?";
            // 3.获取执行sql语句对象
            pStatement = connection.prepareStatement(sql);
            // 4.设置参数
            pStatement.setInt(1, 2);
            // 5.执行
            executeQuery = pStatement.executeQuery();

            while (executeQuery.next()) {
                System.out.println(executeQuery.getString(2) + "-----" + executeQuery.getString("age"));
            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            JDBCUtil_v2.release(connection, pStatement, executeQuery);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值