鎶藉彇jdbc宸ュ叿绫籎dbcUtil

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎶藉彇jdbc宸ュ叿绫籎dbcUtil相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/driver' title='driver'>driver   resource   on()   void   ssl   static   rgba   

1.鍦╯rc涓嬪垱寤轰竴涓猨dbc.properties鏂囦欢

url=jdbc:mysql:///demo
user=root
password=123
driver=com.mysql.jdbc.Driver

2.缂栧啓宸ュ叿绫?/p>

public class JdbcUtil {
    private static Connection con = null;
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    static{
        Properties p = new Properties();//鍒涘缓Properties闆嗗悎瀵硅薄
        InputStream res = JdbcUtil.class  //鍒╃敤绫诲姞杞藉櫒鑾峰彇鍒拌閰嶇疆鏂囦欢锛屼粬鐨勯粯璁よ矾寰勬槸浠巗rc涓嬪鎵撅紝杩斿洖涓€涓緭鍏ユ祦
                .getClassLoader()
                .getResourceAsStream("jdbc.properties");
        try {
            p.load(res);  //鍔犺浇閰嶇疆鏂囦欢
            url = p.getProperty("url"); //鑾峰彇閰嶇疆鏂囦欢涓殑鍊?            user = p.getProperty("user");
            password = p.getProperty("password");
            driver = p.getProperty("driver");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 鑾峰彇杩炴帴
     * @return
     */
    public static Connection getConnection(){
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 鍏抽棴杩炴帴
     * @param stmt
     * @param con
     */
    public static void close(Statement stmt,Connection con){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

以上是关于鎶藉彇jdbc宸ュ叿绫籎dbcUtil的主要内容,如果未能解决你的问题,请参考以下文章

Shell鍐欑郴缁熷皬宸ュ叿

moment.js 鏃ユ湡杞崲宸ュ叿

linux rsync 杩滅▼鍚屾宸ュ叿

澶у巶鍓嶇寮€鍙戯紝蹇呭宸ュ叿闆嗗悎

鎶撳寘宸ュ叿 tcpdump 鐢ㄦ硶璇存槑

鎺ュ彛娴嬭瘯&甯哥敤宸ュ叿