连接数据库的工具JdbcUtil

 1 public class JdbcUtil {
 2     private static String driver=null;//驱动
 3     private static String url=null;//连接地址
 4     private static String username=null;//用户名
 5     private static String password=null;//密码
 6     static {
 7         try {
 8             Properties props=new Properties();
 9             InputStream ins = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
10             props.load(ins);
11             driver = props.getProperty("driver");
12             url = props.getProperty("url");
13             username = props.getProperty("username");
14             password = props.getProperty("password");
15             Class.forName(driver);
16         } catch (Exception e) {
17             e.printStackTrace();
18         }
19     }
20     /**
21      * Description:获取数据连接<br/>
22      * @return yuanQiSheng
23      * @throws Exception
24      */
25     public static Connection getConnection() throws Exception {
26         return DriverManager.getConnection("url","username","password");
27     }
28     /**
29      * Description:释放数据库资源<br/>
30      * @param conn:Connection
31      * @param pres:PreparedStatement
32      * @param rs:ResultSet
33      */
34     public static void release(Connection conn,PreparedStatement pres,ResultSet rs) {
35         try {
36             if(rs!=null) {
37                 rs.close();
38             }
39         } catch (Exception e) {
40             e.printStackTrace();
41         }
42         try {
43             if(pres!=null) {
44                 pres.close();
45             }
46         } catch (Exception e) {
47             e.printStackTrace();
48         }
49         try {
50             if(conn!=null) {
51                 conn.close();
52             }
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }

猜你喜欢

转载自www.cnblogs.com/yuanqisheng/p/12343359.html