码迷,mamicode.com
首页 > 数据库 > 详细

JDBC的工具类

时间:2017-02-19 12:58:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:成员变量   .sql   cti   turn   curl   exce   blog   class   statement   

A: 抽取两个方法,一个获取Connection对象,一个是释放资源


import java.io.FileReader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

 

public class JdbcUtil {        

         private JdbcUtil() {}                                       // 私有化构造方法      

         // 定义成员变量

         private static String jdbcUrl= null ;

         private static String userName = null ;

         private static String password = null ;

         // 加载数据

         static {                   

                   try {                            

                            Properties prop = new Properties() ;

                            prop.load(new FileReader("jdbc.properties")) ;

                            jdbcUrl = prop.getProperty("jdbcUrl") ;

                            userName = prop.getProperty("userName") ;

                            password = prop.getProperty("password") ;

                            Class.forName(prop.getProperty("driverClassName")) ;                   

                   } catch (Exception e) {

                            e.printStackTrace();

                   }                    

         }         

/**

          * 获取连接

          */

         public static Connection getConnection() throws SQLException {

                   return DriverManager.getConnection(jdbcUrl, userName, password) ;

         }        

         /**

          * 释放资源

          */

public static void close(Connection conn , Statement st , ResultSet rs) throws SQLException {

                   if(conn != null) conn.close() ;

                   if(st != null) st.close() ;

                   if(rs != null) rs.close() ;

         }

}

 


 

 

JDBC的工具类

标签:成员变量   .sql   cti   turn   curl   exce   blog   class   statement   

原文地址:http://www.cnblogs.com/loaderman/p/6415303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!