jdbc练习的一个工具类

时间:2012-08-11 11:25:17
【文件属性】:

文件名称:jdbc练习的一个工具类

文件大小:1KB

文件格式:JAVA

更新时间:2012-08-11 11:25:17

jdbc练习的一个工具类

package util; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JdbcUtil { private static final String URL = "url"; private static final String DBINFO_FIIE_NAME = "dbinfo.properties"; private static Properties infos = new Properties(); private static String url = null; static{ InputStream is = JdbcUtil.class .getClassLoader() .getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("属性文件加载错误"); } finally { try { is.close(); } catch (Exception e) { } } url = infos.getProperty(URL); } public static Connection getConnection() { try { return DriverManager.getConnection(url, infos); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void close(ResultSet rs, Statement stat, Connection con) { try { rs.close(); } catch (Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); } try { stat.close(); } catch (Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); } try { con.close(); } catch (Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); } } public static void main(String[] args){ System.out.println(JdbcUtil.getConnection()); } }


网友评论