我在发一下我的程序吧- package jike091.Jackey.connect;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Properties;
- import javax.sql.DataSource;
- import org.apache.commons.dbcp.BasicDataSourceFactory;
- public class ConnectDB
- {
- private static DataSource myDataSource=null;
-
- static
- {
- try
- {
- Properties prop=new Properties();
- InputStream inStream=ConnectDB.class.getClassLoader().getResourceAsStream("dbconfig.properties");
- prop.load(inStream);
- myDataSource = BasicDataSourceFactory.createDataSource(prop);
- } catch (Exception e)
- {
- // TODO Auto-generated catch block
- throw new ExceptionInInitializerError();
- }
- }
- public ConnectDB(){}
-
- public static Connection getConnection() throws SQLException
- {
- return myDataSource.getConnection();
- }
-
-
-
- public static void free(ResultSet rs,PreparedStatement pst,Connection con) throws Exception
- {
- try
- {
- if(rs!=null)
- {
- rs.close();
- }
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- throw e;
- }finally
- {
- try
- {
- if(pst!=null)
- {
- pst.close();
- }
- }catch(SQLException e)
- {
- throw e;
- }finally
- {
- try
- {
- if(con!=null)
- {
- con.close();
- }
- }catch(SQLException e)
- {
- throw e;
- }
- }
- }
- }
- }
复制代码 我导入了三个包commons-dbcp-1.4.jar, commons-collections-3.2.1.jar, commons-pool-1.6.jar用以连接,这是我在李勇老师的JDBC视频上学到的,大家可以看一下。 |