Pages

Saturday, May 15, 2010

JNDI Resource Configuration in Tomcat 5.5

JNDI Resource Configuration to maintain database connections. Follow the the steps below :

1. Open Context.xml file, which is located under tomcat/conf/context.xml
2. Add Following Resource configuration under tag.



3. Open your applications web.xml file and add resource-reference in that.


DB Connection
jdbc/test
javax.sql.DataSource
Container


4. Look at the following code to lookup JNDI

private static InitialContext ctx = null;
private static DataSource ds = null;
try
{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
if(ds != null){
log.info("DbUtils: datasource setup successfully.");
}
}catch (Exception e)
{
System.out.println("Error is .. "+e.getMessage());
}


5. Finally, deploy your application and use it.

Tuesday, April 6, 2010

Java - Date & Time

This code can helps us to separate Time from Date.
String d="1-2-2010 3:43:57 PM";
DateFormat formatterDateFormat;
Date date;
formatterDateFormat=new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
date=(Date)formatterDateFormat.parse(d);
DateFormat df=DateFormat.getTimeInstance(DateFormat.SHORT);
String time=df.format(date);
System.out.println("Time is ... "+time);

Output of this code is like...
Time is ... 3:43 PM