首页 > 数据库技术 > 详细

连接数据库的工具JdbcUtil

时间:2021-04-29 14:58:20      阅读:13      评论:0      收藏:0      [点我收藏+]
 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     }

连接数据库的工具JdbcUtil

原文:https://www.cnblogs.com/thunders/p/14717255.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!