欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【JDBC】工具类的抽取

程序员文章站 2023-11-13 13:55:40
jdbc.properties属性文件 JDBCUtils.java工具类 JDBCDemo3.java测试 ......

jdbc.properties属性文件

driverclass=com.mysql.jdbc.driver
url=jdbc:mysql:///jdbctest
username=root
password=1234

jdbcutils.java工具类

package com.imooc.jdbc.utils;

import java.io.ioexception;
import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.properties;

/**
 * jdbc的工具类
 * @author jt
 *
 */
public class jdbcutils {
    private static final string driverclass;
    private static final string url;
    private static final string username;
    private static final string password;
    
    static{
        // 加载属性文件并解析:
        properties props = new properties();
        // 如何获得属性文件的输入流?
        // 通常情况下使用类的加载器的方式进行获取:
        inputstream is = jdbcutils.class.getclassloader().getresourceasstream("jdbc.properties");
        try {
            props.load(is);
        } catch (ioexception e) {
            e.printstacktrace();
        }
        
        driverclass = props.getproperty("driverclass");
        url = props.getproperty("url");
        username = props.getproperty("username");
        password = props.getproperty("password");
    }

    /**
     * 注册驱动的方法
     * @throws classnotfoundexception 
     */
    public static void loaddriver() throws classnotfoundexception{
        class.forname(driverclass);
    }
    
    /**
     * 获得连接的方法:
     * @throws sqlexception 
     */
    public static connection getconnection() throws exception{
        loaddriver();
        connection conn = drivermanager.getconnection(url, username, password);
        return conn;
    }
    
    /**
     * 资源释放
     */
    public static void release(statement stmt,connection conn){
        if(stmt != null){
            try {
                stmt.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
            conn = null;
        }
    }
    
    public static void release(resultset rs,statement stmt,connection conn){
        if(rs!= null){
            try {
                rs.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
            rs = null;
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
            conn = null;
        }
    }
}

jdbcdemo3.java测试

package com.imooc.jdbc.demo1;

import java.sql.connection;
import java.sql.statement;

import org.junit.test;

import com.imooc.jdbc.utils.jdbcutils;

public class jdbcdemo3 {

    @test
    // 保存记录
    public void demo1(){
        connection conn = null;
        statement stmt  = null;
        try{
            // 获得连接:
            conn = jdbcutils.getconnection();
            // 创建执行sql语句的对象
            stmt = conn.createstatement();
            // 编写sql:
            string sql = "insert into user values (null,'ggg','123','小六')";
            // 执行sql:
            int num = stmt.executeupdate(sql);
            if(num > 0){
                system.out.println("保存成功!");
            }
        }catch(exception e){
            e.printstacktrace();
        }finally{
            // 释放资源:
            jdbcutils.release(stmt, conn);
        }
    }
}