2.编写工具类
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 //利用类加载器获取到该配置文件,他的默认路径是从src下寻找,返回一个输入流 .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工具类JdbcUtil
标签:driver resource 类加载 文件中 on() void ssl static rgba
查看更多关于抽取jdbc工具类JdbcUtil的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did118354