2017-03-14

Connection pooling in java example with oracle

  • Oracle connection pool example
  • Java connection pooling
  • Oracle database connection with JDBC
  • Implementing  own Connection pooling in Java
  • how to implement connection pooling in java with example

Here we are creating a class and declaring all database information. This call will execute only one time at the program beaning. If you are using Web server, this call will execute one time at the beginning of the server start. 


  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;

  6. public class ConnectionPool {
  7. static Connection con;
  8. static Statement st;
  9. static{
  10. try {
  11. Class.forName("oracle.jdbc.driver.OracleDriver");
  12. } catch (ClassNotFoundException e) {
  13. e.printStackTrace();
  14. }
  15. try {
  16. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","system","8040");
  17. st=con.createStatement();
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }

  21. }

  22. }

    After Database connection successfully establish , you can use statement  object for other transaction. 

Example

  1. import java.sql.ResultSet;
  2. import java.sql.SQLException;


  3. public class TestConnectionPoll {

  4. public static void main(String[] args) throws SQLException {
  5. ResultSet rs=ConnectionPool.st.executeQuery("select * from root.website");
  6. while(rs.next()){
  7. System.out.println(rs.getString(1));
  8. }
  9. }

  10. }

No comments:

Post a Comment