JDBC系列之JdbcUtil--封装类(四)

来源:互联网 发布:淘宝店铺联盟报名入口 编辑:程序博客网 时间:2024/06/05 07:18

前三个版本一直说JDBC的流程是四步 现在在这个封装类里面 我们又加了一个步骤 释放资源
//有了这个封装类的话 之前的东西我们知道它的实现原理就可以了 最常用的肯定还是我们把之前的代码统统封装好到一个类里面 成为一个封装的工具 接下来就要说一下这个封装类的实现原理

不知道大家还记不记得前三个版本我们一直要写四个总是重复的东西 1,连接驱动的路径 2, 连接数据库使用的url 3, 用户名 4, 密码 接下来我们就用一个方便的快捷的方法来解决代码重复的问题

首先 我们先创建一个配置文件 —- 在我们得工程里的src文件夹下右键新建一个File文件, 我的文件名叫–jdbcfg.properties 因为我们在系列三中用到过properties这个类 现在我们就用这个配置文件来代替.properties的使用

//jdbcfg.properties//在这个配置文件里写下自己的相关信息driverClass=com.mysql.jdbc.Driverurl = jdbc:mysql://localhost:3306/jdbcuser= rootpassword=111111//然后我们就要对我们的JdbcUtil的封装类进行封装操作//JdbcUtilpublic class JdbcUtil {    //驱动类的路径    private static String driverClass = null;    //连接数据库使用的url    private static String url = null;    //用户名和密码    private static String user = null;    private static String password = null;    private static Connection connection = null;    static {        try {            ClassLoader cl = JdbcUtil.class.getClassLoader();            InputStream stream = cl.getResourceAsStream("jdbcfg.properties");            //得到流中的数据            //创建一个Properties对象            Properties properties = new Properties();            //通过创建Properties对象的load方法,加载流中的数据            properties.load(stream);            //根据key,获得value            driverClass = properties.getProperty("driverClass");            url = properties.getProperty("url");            password = properties.getProperty("password");            user = properties.getProperty("user");            Class.forName(driverClass);        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    //写一个静态方法,获取连接对象    public static Connection getConnection() throws SQLException {        return DriverManager.getConnection(url, user, password);    }    //写一个静态方法,获得statement对象    public static Statement getStatement() throws SQLException {        connection = getConnection();        return connection.createStatement();    }    public static void release() {        if (connection != null) {            try {                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    //方法重载    public static void release(Statement statement) {        release(statement, null);    }    //写一个静态方法 用来释放资源    //如果想要释放Connection对象,需要调用Connection对象的close方法    //然后在release方法中调用该对象的close方法    //释放资源    //Statement对象与ResultSet对象同理    public static void release(Statement statement, ResultSet resultSet) {        release();        if (statement != null) {            try {                statement.close();            } catch (SQLException e) {                e.printStackTrace();            }            if (resultSet != null) {                try {                    resultSet.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }}

这样一个JbdcUtil的封装类就封装完了 我们想要使用的话只需要去调用就可以了

//这是我创建的一个新的jdbc类用来测试一下我的封装是不是对的public class Jdbc6Util {    public static void main(String[] args) throws SQLException {       Connection connection = JdbcUtil.getConnection();      Statement statement = connection.createStatement();        statement.executeUpdate("INSERT INTO stu VALUES (22, 'demo', 22);");        //最后不要忘记释放资源哦        JdbcUtil.release(statement);    }}
原创粉丝点击