码迷,mamicode.com
首页 > 数据库 > 详细

JDBC封装工具类

时间:2015-02-13 16:41:44      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:数据库连接   封装   jdbc   

package com.lxf.bean.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库连接类
 * 
 * @author 刘向峰
 * 
 */
public class DbConnection {

    //oracle连接写法
    private static String diver = "oracle.jdbc.driver.OracleDriver";
    private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    private static String username = "lxf";
    private static String password = "lxf";

    static {
        try {
            //加载驱动
            Class.forName(diver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * 
     * @return connection
     */
    public static Connection getConn() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭资源
     * 
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void close(Connection connection, Statement statement,
            ResultSet resultSet) {
        try {
            if (connection != null) {
                connection.close();
                connection = null;
            }
            if (statement != null) {
                statement.close();
                statement = null;
            }
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

JDBC封装工具类

标签:数据库连接   封装   jdbc   

原文地址:http://blog.csdn.net/lxf_44944/article/details/43794571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!