zoukankan      html  css  js  c++  java
  • 获取数据库连接对象的方法

    public class DBHelper {
    private static final String url="jdbc:oracle:thin:@localhost:1521:orcl";//连接字符串
    private static final String user="nga";//用户名
    private static final String password="123";//密码
    private static Connection con =null;
    static{
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    con = DriverManager.getConnection(url,user,password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    /**
    * 每次在做数据库操作之前都先获取连接对象
    * @throws SQLException
    */
    public static void getConnection() throws SQLException{
    if(con!=null){
    if(con.isClosed()){
    con = DriverManager.getConnection(url,user,password);
    }
    }else{
    con = DriverManager.getConnection(url,user,password);
    }
    }
    /**
    * 每次用完连接之后就需要关掉
    * @throws SQLException
    */
    public static void closeConnection() throws SQLException{
    if(con !=null){
    con.close();
    }
    }

  • 相关阅读:
    事件基础
    DOM
    GoWeb编程之多路复用
    GoWeb编程之HelloWorld
    Linux libtins 库安装教程
    模式串匹配KMP详解
    树的重心
    Light OJ 1064
    Light OJ 1060
    1057
  • 原文地址:https://www.cnblogs.com/tutuwowo/p/5831920.html
Copyright © 2011-2022 走看看