代码改变世界

JdbcUtil

2012-10-26 00:33  coffeescript  阅读(534)  评论(0)    收藏  举报
View Code
 1 package com.myTooYoung;
 2 import java.sql.*;
 3 
 4 public class JdbcUtil {
 5     static{
 6         String d="com.mysql.jdbc.Driver";
 7         try{
 8             Class.forName(d);
 9         }catch(Exception e){
10             e.printStackTrace();
11         }
12     }
13     
14     public static Connection getConnection(){
15         String url="jdbc:mysql://localhost:3306/jdbc";
16         String username="root";
17         String password="root";
18         Connection con=null;
19         try{
20             con=DriverManager.getConnection(url,username,password);
21         }catch(Exception e){
22             e.printStackTrace();
23         }
24         return con;
25     }
26     
27     public static void release(ResultSet rs,Statement stmt,Connection con){
28         if(rs!=null){
29             try{
30                 rs.close();
31             }catch(Exception e){
32                 e.printStackTrace();
33             }
34         }
35         if(stmt!=null){
36             try{
37                 stmt.close();
38             }catch(Exception e){
39                 e.printStackTrace();
40             }
41         }
42         if(con!=null){
43             try{
44                 con.close();
45             }catch(Exception e){
46                 e.printStackTrace();
47             }
48         }
49     }
50     
51     public static void release(Object o){
52         try{
53             if(o instanceof ResultSet){
54                 ((ResultSet)o).close();
55             }else if(o instanceof Statement){
56                 ((Statement)o).close();
57             }else if(o instanceof Connection){
58                 ((Connection)o).close();
59             }
60         }catch(Exception e){
61             e.printStackTrace();
62         }
63     }
64 }

工具类而已