Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. package gz.itcast.util;
  2.  
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Properties;
  10.  
  11. /**
  12. * jdbc工具类
  13. * @author APPle
  14. *
  15. */
  16. public class JdbcUtil {
  17. private static String url = null;
  18. private static String user = null;
  19. private static String password = null;
  20. private static String driverClass = null;
  21.  
  22. /**
  23. * 静态代码块中(只加载一次)
  24. */
  25. static{
  26. try {
  27. //读取db.properties文件
  28. Properties props = new Properties();
  29. /**
  30. * . 代表java命令运行的目录
  31. * 在java项目下,. java命令的运行目录从项目的根目录开始
  32. * 在web项目下, . java命令的而运行目录从tomcat/bin目录开始
  33. * 所以不能使用点.
  34. */
  35. //FileInputStream in = new FileInputStream("./src/db.properties");
  36.  
  37. /**
  38. * 使用类路径的读取方式
  39. * / : 斜杠表示classpath的根目录
  40. * 在java项目下,classpath的根目录从bin目录开始
  41. * 在web项目下,classpath的根目录从WEB-INF/classes目录开始
  42. */
  43. InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");
  44.  
  45. //加载文件
  46. props.load(in);
  47. //读取信息
  48. url = props.getProperty("url");
  49. user = props.getProperty("user");
  50. password = props.getProperty("password");
  51. driverClass = props.getProperty("driverClass");
  52.  
  53.  
  54. //注册驱动程序
  55. Class.forName(driverClass);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. System.out.println("驱程程序注册出错");
  59. }
  60. }
  61.  
  62. /**
  63. * 抽取获取连接对象的方法
  64. */
  65. public static Connection getConnection(){
  66. try {
  67. Connection conn = DriverManager.getConnection(url, user, password);
  68. return conn;
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. throw new RuntimeException(e);
  72. }
  73. }
  74.  
  75.  
  76. /**
  77. * 释放资源的方法
  78. */
  79. public static void close(Connection conn,Statement stmt){
  80. if(stmt!=null){
  81. try {
  82. stmt.close();
  83. } catch (SQLException e) {
  84. e.printStackTrace();
  85. throw new RuntimeException(e);
  86. }
  87. }
  88. if(conn!=null){
  89. try {
  90. conn.close();
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. throw new RuntimeException(e);
  94. }
  95. }
  96. }
  97.  
  98. public static void close(Connection conn,Statement stmt,ResultSet rs){
  99. if(rs!=null)
  100. try {
  101. rs.close();
  102. } catch (SQLException e1) {
  103. e1.printStackTrace();
  104. throw new RuntimeException(e1);
  105. }
  106. if(stmt!=null){
  107. try {
  108. stmt.close();
  109. } catch (SQLException e) {
  110. e.printStackTrace();
  111. throw new RuntimeException(e);
  112. }
  113. }
  114. if(conn!=null){
  115. try {
  116. conn.close();
  117. } catch (SQLException e) {
  118. e.printStackTrace();
  119. throw new RuntimeException(e);
  120. }
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement