Database Connection Pooling

download Database Connection Pooling

of 12

Transcript of Database Connection Pooling

  • 8/3/2019 Database Connection Pooling

    1/12

    Database Connection PoolingA Database Connection Pooling (DBCP) is a framework that reduces the connection timings,

    enrich statement caching, connection validation, and increase the request return timing in a giant

    database network. It is a Java based database connection utility pool used for quicker database

    transaction in a very large application.

    DBCP is extremely useful and applicable where user wants hassle free and quicker response using the

    available connections as making new connection is very time consuming process. On the other hand,

    retrieving request with open connections is hassle free and easy process and give same result in quicker

    time within a giant database application.The database enabled applications needs the database connection for processing the request send by

    the clients and at a single time, myriad numbers of request are send for retrieving the data, at that time

    DBCP works.

    The utility of DBCP enhances where quick database accessing is required for every clients at a given

    time frame on a giant network like internet or any big enterprise network, where millions of requests are

    send within a single point of time.

    In this process, the programmers share a pool of open connections available among them for retrieving

    the quick data. The pool manger of DBCP logs the request into DBMS, process internally and returns to

    relating clients.

    There are several Database Connection Pools already available, both within Apache products and

    elsewhere. But now, DBCP is coming in two different variants ? DBCP 1.4 and DBCP 1.3.

    The DBCP 1.4 supports JDBC 4, compiles and runs under JDK 1.6 while DBCP 1.3 supports JDBC 3 and

    compiles and runs under JDK 1.4-1.5 only. You can choose one appropriate version according to your

    requirements.

    Connecting to MySQL

    This tutorial demonstrate how to connect to MySQL using DBCP.

    Creating database using DBCP

    This tutorial demonstrate how to create database in MySQL using DBCP.

    Creating table using DBCP

    This tutorial demonstrate how to create table in MySQL using DBCP.

    Deleting table using DBCP

    http://www.roseindia.net/tutorial/java/dbcp/ConnectingToMySQL.htmlhttp://www.roseindia.net/tutorial/java/dbcp/ConnectingToMySQL.htmlhttp://www.roseindia.net/tutorial/java/dbcp/creatingdb.htmlhttp://www.roseindia.net/tutorial/java/dbcp/creatingdb.htmlhttp://www.roseindia.net/tutorial/java/dbcp/createtable.htmlhttp://www.roseindia.net/tutorial/java/dbcp/deletetable.htmlhttp://www.roseindia.net/tutorial/java/dbcp/deletetable.htmlhttp://www.roseindia.net/tutorial/java/dbcp/createtable.htmlhttp://www.roseindia.net/tutorial/java/dbcp/creatingdb.htmlhttp://www.roseindia.net/tutorial/java/dbcp/ConnectingToMySQL.html
  • 8/3/2019 Database Connection Pooling

    2/12

    This tutorial demonstrate how to drop table from MySQL using DBCP.

    Retrieving data from table using DBCP

    This tutorial demonstrate how to retrieve records from table using DBCP.

    Insert record into a table using DBCP

    This tutorial demonstrate how to insert record into table using DBCP.

    Display the column name using DBCP

    This tutorial demonstrate how to display column name of the table using DBCP.

    Delete all rows using DBCP

    This tutorial demonstrate how to delete all record from the table using DBCP.

    Add new column to table using DBCP

    This tutorial demonstrate how to add new column to the table using DBCP. If the table already have

    existing column with data inside then the new column will have the null value to all its respective rows.

    Rename column name of table using DBCP

    This tutorial demonstrate how to rename column name of the table using DBCP.

    Natural Join of tables using DBCP

    This tutorial demonstrate natural join of the tables using DBCP.

    Natural Left Join of tables using DBCP

    This tutorial demonstrate natural left join of the tables using DBCP.

    This tutorial demonstrate how to connect to MySQL using DBCP.

    Code:ConnectingToMySQL.java

    import java.sql.*;

    import org.apache.commons.dbcp.BasicDataSource;

    public class ConnectingToMySQL {

    public static void main(String[] args) {BasicDataSource bds = new BasicDataSource();

    bds.setDriverClassName("com.mysql.jdbc.Driver");

    bds.setUrl("jdbc:mysql://localhost:3306/mydb");

    bds.setUsername("root");

    bds.setPassword("");

    try {

    http://www.roseindia.net/tutorial/java/dbcp/deletetable.htmlhttp://www.roseindia.net/tutorial/java/dbcp/getallrows.htmlhttp://www.roseindia.net/tutorial/java/dbcp/insertinto.htmlhttp://www.roseindia.net/tutorial/java/dbcp/displycolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/displycolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/removerows.htmlhttp://www.roseindia.net/tutorial/java/dbcp/removerows.htmlhttp://www.roseindia.net/tutorial/java/dbcp/addcolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/addcolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/renamecolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/renamecolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/naturaljoin.htmlhttp://www.roseindia.net/tutorial/java/dbcp/naturaljoin.htmlhttp://www.roseindia.net/tutorial/java/dbcp/naturalleftjoin.htmlhttp://www.roseindia.net/tutorial/java/dbcp/naturalleftjoin.htmlhttp://www.roseindia.net/tutorial/java/dbcp/naturalleftjoin.htmlhttp://www.roseindia.net/tutorial/java/dbcp/naturaljoin.htmlhttp://www.roseindia.net/tutorial/java/dbcp/renamecolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/addcolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/removerows.htmlhttp://www.roseindia.net/tutorial/java/dbcp/displycolumn.htmlhttp://www.roseindia.net/tutorial/java/dbcp/insertinto.htmlhttp://www.roseindia.net/tutorial/java/dbcp/getallrows.html
  • 8/3/2019 Database Connection Pooling

    3/12

    System.out.println("Trying to connect!!");

    Connection con = bds.getConnection();

    System.out.println("Connected sucessfully !!");

    con.close();

    } catch (SQLException s) {

    System.out.println("SQL code does not execute.");

    }

    }

    }

    Directory structure of files

    In the lib folder place all required file ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar,

    j2ee.jar and mysql-connector-java-5.1.7-bin.jar

    In the pathset.bat write following statement

    set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-

    pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

    Output:

  • 8/3/2019 Database Connection Pooling

    4/12

    Related Tags for Connecting to MySQL:

    This tutorial demonstrate how to create database in MySQL using DBCP.

    Code:CreatingDB.java

    import java.sql.*;

    import java.io.*;

    import org.apache.commons.dbcp.BasicDataSource;

    public class CreatingDB {

    public static void main(String[] args) {

    BasicDataSource bds = new BasicDataSource();

    bds.setDriverClassName("com.mysql.jdbc.Driver");

    bds.setUrl("jdbc:mysql://localhost:3306/");

    bds.setUsername("root");

    bds.setPassword("");

    try {

    System.out.println("Trying to connect!!");

    Connection con = bds.getConnection();

    System.out.println("Connected sucessfully !!");

    try {

    Statement st = con.createStatement();

    BufferedReader bf = new BufferedReader(new InputStreamReader(

  • 8/3/2019 Database Connection Pooling

    5/12

    System.in));

    System.out.println("Enter Database name:");

    String database = bf.readLine();

    st.executeUpdate("CREATE DATABASE " + database);

    System.out.println("1 row(s) affacted");

    con.close();

    } catch (SQLException s) {

    System.out.println("SQL code does not execute.");

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    }}

    Directory structure of files

    In the lib folder place all required file ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar,

    j2ee.jar and mysql-connector-java-5.1.7-bin.jar

    In the pathset.bat write following statement

    set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-

    pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

    Output:

  • 8/3/2019 Database Connection Pooling

    6/12

    Related Tags for Creating database using DBCP:

    This tutorial demonstrate how to create table in MySQL using DBCP.

    Code:CreateTable.java

    import java.sql.*;

    import org.apache.commons.dbcp.BasicDataSource;

    class CreateTable {

    public static void main(String[] args) {

    BasicDataSource bds = new BasicDataSource();

    bds.setDriverClassName("com.mysql.jdbc.Driver");

    bds.setUrl("jdbc:mysql://localhost:3306/mydb");

    bds.setUsername("root");

    bds.setPassword("");

    try {

    Connection con = bds.getConnection();

    try {

    Statement st = con.createStatement();

    String QueryString = "CREATE TABLE user_master1(User_Id INTEGER NOT NULL

    AUTO_INCREMENT, "

    + "User_Name VARCHAR(25), UserId VARCHAR(20), User_Pwd VARCHAR(15), primary

    key(User_Id))";

    st.executeUpdate(QueryString);

  • 8/3/2019 Database Connection Pooling

    7/12

    System.out.println("Table created!!");

    con.close();

    } catch (SQLException s) {

    System.out.println("SQL code does not execute.");

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    Directory structure of files

    In the lib folder place all required file ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar,

    j2ee.jar and mysql-connector-java-5.1.7-bin.jar

    In the pathset.bat write following statement

    set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-

    pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

    Output:

  • 8/3/2019 Database Connection Pooling

    8/12

    Related Tags for Creating table using DBCP :

    This tutorial demonstrate how to drop table from MySQL using DBCP.

    Code:import java.sql.*;import org.apache.commons.dbcp.BasicDataSource;

    class DeleteTable {public static void main(String[] args) {BasicDataSource bds = new BasicDataSource();bds.setDriverClassName("com.mysql.jdbc.Driver");bds.setUrl("jdbc:mysql://localhost:3306/mydb");bds.setUsername("root");bds.setPassword("");try {

    Connection con = bds.getConnection();try {Statement st = con.createStatement();st.executeUpdate("DROP TABLE user_master1");con.close();

    } catch (SQLException s) {System.out.println("SQL code does not execute.");

    }} catch (Exception e) {e.printStackTrace();

    }

    }}Directory structure of files

  • 8/3/2019 Database Connection Pooling

    9/12

    In the lib folder place all required jar files ie. commons-collections.jar, commons-

    dbcp.jar, commons-pool.jar, j2ee.jar and mysql-connector-java-5.1.7-bin.jar

    In the pathset.bat write following statement

    set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-

    pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

    Output:

    Related Tags for Deleting table using DBCP :

    This tutorial demonstrate how to drop table from MySQL using DBCP.

    Code:import java.sql.*;import org.apache.commons.dbcp.BasicDataSource;

    class DeleteTable {public static void main(String[] args) {BasicDataSource bds = new BasicDataSource();bds.setDriverClassName("com.mysql.jdbc.Driver");bds.setUrl("jdbc:mysql://localhost:3306/mydb");

  • 8/3/2019 Database Connection Pooling

    10/12

    bds.setUsername("root");bds.setPassword("");try {Connection con = bds.getConnection();try {

    Statement st = con.createStatement();st.executeUpdate("DROP TABLE user_master1");con.close();

    } catch (SQLException s) {System.out.println("SQL code does not execute.");

    }} catch (Exception e) {e.printStackTrace();

    }}

    }Directory structure of files

    In the lib folder place all required jar files ie. commons-collections.jar, commons-

    dbcp.jar, commons-pool.jar, j2ee.jar and mysql-connector-java-5.1.7-bin.jar

    In the pathset.bat write following statement

    set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-

    pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

    Output:

  • 8/3/2019 Database Connection Pooling

    11/12

    Related Tags for Deleting table using DBCP :

    This tutorial demonstrate how to retrieve records from table using DBCP.

    Code:import java.sql.*;import org.apache.commons.dbcp.BasicDataSource;

    public class GetAllRowsDBCP {public static voidmain(String[] args) {

    System.out.println("Getting All Rows from a table!\n");BasicDataSource bds = new BasicDataSource();bds.setDriverClassName("com.mysql.jdbc.Driver");bds.setUrl("jdbc:mysql://localhost:3306/mydb");bds.setUsername("root");bds.setPassword("");try {

    Connection con = bds.getConnection();try {Statement st = con.createStatement();ResultSet res = st.executeQuery("SELECT * FROM users");System.out.println("ID: " + "\t\t" + "NAME: ");while (res.next()) {

    int i = res.getInt("id");String s = res.getString("name");System.out.println(i + "\t\t" + s);

    }con.close();

    } catch (SQLException s) {System.out.println("SQL code does not execute.");

    }} catch (Exception e) {

    e.printStackTrace();}

    }}Directory structure of files

  • 8/3/2019 Database Connection Pooling

    12/12

    In the lib folder place all required file ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar,

    j2ee.jar and mysql-connector-java-5.1.7-bin.jar

    In the pathset.bat write following statement

    set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-

    pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

    Output: