鎶藉彇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 rgba1.鍦╯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的主要内容,如果未能解决你的问题,请参考以下文章