Click here to watch in Youtube : https://www.youtube.com/watch?v=Kd1HEjLE5qU&index=1&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
|
JDBC|Servlets : BoneCP DataSource Mysql - Tomcat |
ConnectionManager.java
import com.jolbox.bonecp.BoneCPDataSource;
public class ConnectionManager
{
private static BoneCPDataSource boneCPDataSource = null;
public static BoneCPDataSource getBoneCPDataSource()
{
return boneCPDataSource;
}
public static void setBoneCPDataSource( BoneCPDataSource boneCPDataSource )
{
ConnectionManager.boneCPDataSource = boneCPDataSource;
}
public static BoneCPDataSource configureBoneCPDataSource()
{
try
{
/*
* load the database driver (make sure this is in your classpath!)
*/
Class.forName("com.mysql.jdbc.Driver");
/*
* setup the BoneCPDataSource
*/
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/world");
boneCPDataSource.setUsername("root");
boneCPDataSource.setPassword("root");
boneCPDataSource.setMinConnectionsPerPartition(5);
boneCPDataSource.setMaxConnectionsPerPartition(10);
boneCPDataSource.setPartitionCount(1);
setBoneCPDataSource(boneCPDataSource);
System.out
.println("contextInitialized.....boneCPDataSource is configured");
}
catch( Exception exe )
{
exe.printStackTrace();
}
return boneCPDataSource;
}
public static void closeBoneCPDataSource()
{
try
{
BoneCPDataSource boneCPDataSource = ConnectionManager
.getBoneCPDataSource();
if( boneCPDataSource != null )
{
/*
* This method must be called only once when the application
* stops.
*/
boneCPDataSource.close();
System.out
.println("contextDestroyed.....boneCPDataSource is closed!");
}
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
ContextListener.java
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener
{
@Override
public void contextInitialized( ServletContextEvent arg0 )
{
System.out.println("\n---------------------------------------------");
System.out.println("ContextInitialized Method has been Called......");
ConnectionManager.configureBoneCPDataSource();
System.out.println("---------------------------------------------\n");
}
@Override
public void contextDestroyed( ServletContextEvent arg0 )
{
System.out.println("\n---------------------------------------------");
System.out.println("contextDestroyed Method has been Called......");
ConnectionManager.closeBoneCPDataSource();
System.out.println("---------------------------------------------\n");
}
}
CityInfoServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jolbox.bonecp.BoneCPDataSource;
public class CityInfoServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
BoneCPDataSource boneCPDataSource = null;
public void init( ServletConfig config )
{
try
{
System.out
.println("-----------------------------------------------------");
System.out
.println("init method has been called and servlet is initialized");
boneCPDataSource = ConnectionManager.getBoneCPDataSource();
System.out.println("Got BoneCPDataSource : " + boneCPDataSource);
System.out
.println("-----------------------------------------------------");
}
catch( Exception exe )
{
exe.printStackTrace();
}
}
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
System.out
.println("-----------------------------------------------------");
System.out.println("doGet method has been called");
System.out
.println("-----------------------------------------------------");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "City Information From Mysql Database";
out.print("<html><body bgcolor=\"#f0f0f0\">");
out.print("<h1 align=\"center\">" + title + "</h1>\n");
showCityInformation(out);
out.print("</body></html>");
}
public void destroy()
{
System.out
.println("-----------------------------------------------------");
System.out
.println("destroy method has been called and servlet is destroyed");
System.out
.println("-----------------------------------------------------");
}
private void showCityInformation( PrintWriter out )
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
String sql = "select * from city limit ?";
/*
* Get connection from the DataSource
*/
connection = boneCPDataSource.getConnection();
/*
* Execute the query
*/
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 10);
ResultSet rs = preparedStatement.executeQuery();
while( rs.next() )
{
int id = rs.getInt(1);
String name = rs.getString(2);
String countryCode = rs.getString(3);
String district = rs.getString(4);
int population = rs.getInt(5);
/*
* Display values
*/
out.print("ID: " + id + "<br>");
out.print("Name: " + name + "<br>");
out.print("CountryCode: " + countryCode + "<br>");
out.print("District: " + district + "<br>");
out.println("Population: " + population + "<br>");
out.println("--------------------------------------" + "<br>");
}
rs.close();
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
/*
* finally block used to close resources
*/
try
{
if( preparedStatement != null )
{
preparedStatement.close();
}
}
catch( SQLException sqlException )
{
sqlException.printStackTrace();
}
try
{
if( connection != null )
{
connection.close();
}
}
catch( SQLException sqlException )
{
sqlException.printStackTrace();
}
}
}
}
No comments:
Post a Comment