JDBC的简单封装

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

/**
 * 数据库连接工具
 * @author 2017
 *
 */
public class DbTool {
    /**
     * 数据库的类
     */
    private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
    /**
     * 主机地址和接口
     */
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE";
    /**
     * 账户
     */
    private static final String USER = "";
    /**
     * 密码
     */
    private static final String PASSWORD = "";
    /**
     * 数据库的链接通道方法-Connection_conn
     * 
     */
    public static Connection getConnection(){
        try {
            //加载oracle数据驱动类
            Class.forName(ORACLE_DRIVER);
            //返回数据库通道对象
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
        
    }
    
    /**
     * 数据库的释放资源方法
     * Connection_conn
     * PreparedStatement_ps
     * ResultSet_rs
     */
    public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
        try {
            if (rs!=null) {
                rs.close();
            }
            if (ps!=null) {
                ps.close();
            }
            if (conn!=null) {
                conn.close();
            }
        } catch (Exception e) {
            //打印错误
            e.printStackTrace();
        }
    }
    /**
     * 数据库的释放资源方法
     *  Connection_conn
     *  PreparedStatement_ps
     */
    public static void close(Connection conn,PreparedStatement ps){
        try {
            
            if (ps!=null) {
                ps.close();
            }
            if (conn!=null) {
                conn.close();
            }
        } catch (Exception e) {
            //打印错误
            e.printStackTrace();
        }
    }
    
}

 

posted @ 2017-08-11 15:39  汪强胜  Views(226)  Comments(0Edit  收藏  举报