简体   繁体   中英

connecting database mysql using c3p0 pooling

ComboPooledDataSource cpds = new ComboPooledDataSource(); 
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver 

cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/dragon" ); 
cpds.setUser("root"); 
cpds.setPassword("password");
cpds.setMaxPoolSize(50);

I've created a java file containing the following code to configure a ComboPooledDataSource object. Now is this code enough to establish a pooled connection with the database?

If not, What else should I do ?

Also please tell how can I can implement JNDI here.

Please explain it since I am a beginner.

At First...Create the code to initiate the connection in a class containing static methods or variables like this the following..

        private static ComboPooledDataSource cpds = new ComboPooledDataSource();
        public static void MakePool()
        {
               try 
               {
                 cpds=new ComboPooledDataSource();
                 cpds.setDriverClass("com.mysql.jdbc.Driver");
                 cpds.setJdbcUrl("jdbc:mysql://localhost:3306/att_db");
                 cpds.setUser("root");
                 cpds.setPassword("dragon");
                 cpds.setMaxPoolSize(MaxPoolSize);
                 cpds.setMinPoolSize(MinPoolSize);
                 cpds.setAcquireIncrement(Accomodation);
             } 
             catch (PropertyVetoException ex) 
             {
                 //handle exception...not important.....
              }

}
public static Connection getConnection()
{
           return cpds.getConnection();
}

Once ur done Create another class meant for server operations....

and get the Connections from the Pool...

         try{

               con=DatabasePool.getConnection();
               // DatabasePool is the name of the Class made earlier....
               .
               .
               .
               .  // Server operations as u wanted.....
               .
               .
             }
             catch(SQL EXCEPTION HERE)
             {
                  .....
             }
             finally
             {     
               if(con!=null)
               {
                      con.close();      
               }
             }

I'm not familiar with JNDI so I won't address that (you probably want a separate question for that with more detail about your goal), but I believe you do have your ComboPooledDataSource configured properly.

You should be able to test your DataSource using code like this (exception handling excluded for simplicity):

ArrayList<Object[]> data = new ArrayList<Object[]>();

Connection connection = cpds.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select a_column from a_table;");

int columnCount = resultSet.getMetaData().getColumnCount();

while (resultSet.next()) {
    Object[] rowData = new Object[columnCount];

    for (int i = 0; i < columnCount; i++) {
        rowData[i] = resultSet.getObject(i + 1);
    }

    data.add(rowData);
}

You use the c3p0 to manage the jdbc connection and should not use the jdni. If you want use the jndi , you need config the connection in the web container. Tomcat like

<Context>   
    <Resource name="jdbc/springflow" auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000" username="root"
        password="" driverClassName="com.mysql.jdbc.Driver"     
        url="jdbc:mysql://localhost:3306/test" />
</Context>

And you can use jndi like context.lookup("java:jdbc/springflow") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM