zoukankan      html  css  js  c++  java
  • DbUtil数据库连接

    DbUtil数据库连接

    package com.zjx.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    
    public class DbUtil {
    
        /**
         * 获取连接
         * @return
         * @throws Exception
         */
        public Connection getCon() throws Exception{
            Class.forName(PropertiesUtil.getValue("jdbcName"));
            Connection con=DriverManager.getConnection(PropertiesUtil.getValue("dbUrl"), PropertiesUtil.getValue("dbUsername"), PropertiesUtil.getValue("dbPassword"));
            return con;
        }
        
        /**
         * 关闭数据连接
         * @param con
         * @throws Exception
         */
        public void closeCon(Connection con) throws Exception{
            if(con!=null){
                con.close();
            }
        }
        
        public static void main(String[] args) {
            DbUtil dbUtil=new DbUtil();
            Connection con=null;
            try {
                con=dbUtil.getCon();
                System.out.println("数据库连接成功");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("数据库连接失败");
            }finally {
                if(con!=null){
                    try {
                        dbUtil.closeCon(con);
                        System.out.println("数据库连接成功关闭");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    find the most comfortable road
    Rank of Tetris
    Segment set
    Codeforces Round #380 (Div. 2)D. Sea Battle
    A Bug's Life
    Is It A Tree?
    N皇后问题
    符号三角形
    2016 ICPC总结
    Sudoku Killer
  • 原文地址:https://www.cnblogs.com/duanwandao/p/9811300.html
Copyright © 2011-2022 走看看