맞춤형 구성 데이터 소스 DataSource

13882 단어 연못
1 . 직접 코드 보기 사용자 정의 데이터 원본 클래스
package com.qf.ds;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;

import javax.sql.DataSource;

import com.qf.utils.DBUtils;


/**
 *        :       。          
 * 
 *               (LinkedList)
 * @author Administrator
 */
public class MyDataSource  implements DataSource{

    //          ,   10 
    private  static  LinkedList pool =new LinkedList();
    static {
        for (int i = 0; i < 10; i++) {
            Connection connection = DBUtils.getConnection();
            pool.add(connection);
        }
    } 
    @Override
    public Connection getConnection() throws SQLException {
        // TODO Auto-generated method stub

        if (pool!=null&&pool.size()>0) {
            System.out.println("=====getConnection=====");
            Connection connection =  pool.removeFirst();
            return connection;
        }
        return null;
    }

    /**
     *               
     */
    public  void release(Connection connection ) {
        try {
            System.out.println("=====release=====");
            pool.addLast(connection);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public  T unwrap(Class iface) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isWrapperFor(Class> iface) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }


}

2 . 도구류
package com.qf.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class DBUtils {
    private static String driverClass = "";
    private static String url = "";
    private static String user = "";
    private static String pwd = "";
    static {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("dbinfo");
        driverClass = resourceBundle.getString("driverClass");
        url = resourceBundle.getString("url");
        user = resourceBundle.getString("user");
        pwd = resourceBundle.getString("password");
    }

    public static Connection getConnection() {
        Connection connection = null;
        // 1.    
        try {
            Class.forName(driverClass);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("       ");
        }

        // 2.    
        try {
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("       ");
        }
        return connection;
    }


    public  static void closeAll(ResultSet resultSet,Statement  statement,   Connection connection) {
          if (resultSet!=null) {
              try {
                resultSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
          if (statement!=null) {
              try {
                  statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
          if (connection!=null) {
              try {
                 connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

properties 프로필
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/1713_transaction
user=root
password=123

3 . 테스트 클래스
package com.qf.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.qf.ds.MyDataSource;

public class Test01 {

    @Test
    public void test01() {
        //1.             
        MyDataSource  ds = new MyDataSource(); 
        Connection connection = null ;
        try {
            connection = ds.getConnection();
            System.out.println(connection);
            PreparedStatement preparedStatement = connection.prepareStatement("select * from user");
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                 System.out.println(resultSet.getString("name"));;
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (connection!=null) {
                ds.release(connection);
            }
        }   
    }
}

이것은 데이터베이스 구동jar 패키지만 추가하면 됩니다

좋은 웹페이지 즐겨찾기