| package com.dkx.jdbcutils; |
| |
| import java.io.FileInputStream; |
| import java.sql.*; |
| import java.util.Properties; |
| |
| public class Jdbcutils { |
| |
| private static String driver; |
| private static String url; |
| private static String user; |
| private static String password; |
| |
| static { |
| try{ |
| Properties properties = new Properties(); |
| properties.load(new FileInputStream("src//re.Properties")); |
| |
| driver = properties.getProperty("driver"); |
| url = properties.getProperty("url"); |
| user = properties.getProperty("user"); |
| password = properties.getProperty("password"); |
| |
| Class.forName("driver"); |
| }catch(Exception e){ |
| |
| |
| throw new RuntimeException(e); |
| } |
| } |
| |
| public static Connection getConnection () { |
| try{ |
| return DriverManager.getConnection(url,user,password); |
| }catch(Exception e){ |
| throw new RuntimeException(e); |
| } |
| } |
| |
| |
| 1.ResultSet 结果集 |
| 2.Statement 或者 PreparedStatement |
| 3.Connection |
| 4. 如果需要关闭资源,就传入对象,否则传入 null |
| */ |
| public static void close (ResultSet set, PreparedStatement preparestatement, Connection connection) { |
| |
| try{ |
| if(set != null){ |
| set.close(); |
| } |
| if(preparestatement != null){ |
| preparestatement.close(); |
| } |
| if(connection != null){ |
| connection.close(); |
| } |
| }catch(Exception e){throw new RuntimeException(e);} |
| } |
| } |