DataBase connection in Java Application? or How to get DataSource from TomCat?
>> Thursday, May 26, 2011
The below steps will explain you how to get DataSource from Tomcat.
STEP1:) In Tomcat Folder. Tomcat-->Context.xml 6.0 server.xml 5.0
Open Context.XML file ,under context tag add Resource tag with following attributes.
<Resource
name="jdbc/projectname"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle=300"
maxWait="1000"
username="bhaskar"
password="bhaskar"
driverclassName="com.mysql.jdbc.Driver"
url="jdbc.mysql://localhost:3306/schemanameoftable" />
STEP2:
After configuring in Tomcat we have to get the connection in our application.For This go through belo0w program....
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DbUtility {
private static Context objContext = null;
private static DataSource objDataSource = null;
static
{
System.out.println("inside the static block of dbutility");
try
{
objContext = new InitialContext();
objDataSource =(DataSource) objContext.lookup("java:/comp/env/jdbc/projectname");
System.out.println("objDataSource is: "+objDataSource);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error while in datasource;");
}
}
public static Connection getConnection() throws Exception{
Connection con = null;
if(objDataSource == null)
{
System.out.println("error while getting the datasource");
}
else
{
con= objDataSource.getConnection();
}
return con;
}
public static void closeConnecton(Connection objConnection) throws Exception
{
System.out.println("inside closing the connection");
if(objConnection != null)
{
objConnection.close();
}
}
}
Connection pooling means reusing the connection witout closing the connection.For this go for difference between Jdbc.close and DataSource.close.
STEP1:) In Tomcat Folder. Tomcat-->Context.xml 6.0 server.xml 5.0
Open Context.XML file ,under context tag add Resource tag with following attributes.
<Resource
name="jdbc/projectname"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle=300"
maxWait="1000"
username="bhaskar"
password="bhaskar"
driverclassName="com.mysql.jdbc.Driver"
url="jdbc.mysql://localhost:3306/schemanameoftable" />
After configuring in Tomcat we have to get the connection in our application.For This go through belo0w program....
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DbUtility {
private static Context objContext = null;
private static DataSource objDataSource = null;
static
{
System.out.println("inside the static block of dbutility");
try
{
objContext = new InitialContext();
objDataSource =(DataSource) objContext.lookup("java:/comp/env/jdbc/projectname");
System.out.println("objDataSource is: "+objDataSource);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error while in datasource;");
}
}
public static Connection getConnection() throws Exception{
Connection con = null;
if(objDataSource == null)
{
System.out.println("error while getting the datasource");
}
else
{
con= objDataSource.getConnection();
}
return con;
}
public static void closeConnecton(Connection objConnection) throws Exception
{
System.out.println("inside closing the connection");
if(objConnection != null)
{
objConnection.close();
}
}
}
Connection pooling means reusing the connection witout closing the connection.For this go for difference between Jdbc.close and DataSource.close.
0 comments:
Post a Comment