Skip to main content


Showing posts from July, 2019

jdbc mysql example in java

Here’s an example to show you how to connect to MySQL database via a JDBC driver.To connect to MySQL from Java, you have to use the JDBC driver from MySQL. You find the latest MySQL JDBC driver under the following URL:
Create a example database and named it test.Now create a a table name it user. Create table user ((userid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL, PRIMARY KEY (userid)); Now add the record in your table; Now create a project in eclipse and mysql-connector jar in its classpath.package com.test;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; publicclass JavaJDBCExample { private Connection connect = null; private Statement statement = null; private ResultSet resultSet = null; publicvoid readDatabase() { try { // this will load the MySQL driver Class.forName("com.mysql.jdbc.Driver"); // setup the connection wi…

singleton design pattern

Java Singleton design pattern is one of the design pattern which suggest that only one instance of a Singleton object is created by the JVM. This is useful when exactly one object is needed to coordinate actions across the system Example: public class Singleton{ privatestatic Singleton singleton = null; private Singleton(){ } publicstaticsynchronized Singleton getInstance( ) { if(singleton == null) { singleton = new Singleton(); } returnsingleton; } publicvoid test( ) { System.out.println("test"); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub returnnew CloneNotSupportedException(); } } publicclass SingletonTest { publicstaticvoid main(String[] args){ Singleton singleton = Singleton.getInstance(); try{ Singleton

Tomcat configuration

1. How to deploy web application in tomcat 
 Create a war file and deploy it in tomcat->webapps folder.Restart tomcat and write localhost:8080/YOURWEBAPPNAME. 
 2. Add jndi in tomcat 
 <Resource name="jdbc/myDB" auth="Container"
 type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
 username="admin" password="admin"
 in web.xml 
 <description>DB Connection</description>
 3. Add user with manager role in tomcat   Tomcat-users.xml 
 <role rolename="manager"/>
 <user username="admin"…

jaxb example

Download <strong>eclipselink.jar</strong>,

 Create a java project and add a class in it.Now create a properties file




 package pck;

 import javax.xml.bind.JAXBContext;

 import javax.xml.bind.Marshaller;

 import javax.xml.bind.annotation.XmlElement;

 import javax.xml.bind.annotation.XmlRootElement;

 import net.sf.json.JSONException;

 @XmlRootElement (name="Person")

 public class Person {

 @XmlElement (name="age")

 public int age;

 @XmlElement (name="name")

 public String name;

 public static void main(String[] args) throws JSONException, Exception {

 Person me = new Person();

 me.age = 27; = "Biswajit";


 JAXBContext jc = JAXBContext.newInstance(Person.class);

 Marshaller marshaller = jc.createMar…

lucene example

In simple words: it parses a text and without saving the text the framework can search inside it. It keeps track of terms used inside the text. Of course you can optionally save the text into your <a href="" data-cke-saved-href="">lucene index</a>.

 Lets take an example.

 I have created a database and a table named it 'city'.

 create table `city` (

 `id` bigint (20),

 `city_name` varchar (765)


 Now I have added two record Kolkata,Delhi in the database.

 Now create a java project and add lucene-analyzers-common-4.0.0.jar,lucene-core-4.0.0.jar,lucene-queries-4.0.0.jar,lucene-queryparser-4.0.0.jar and mysql-connector-java-5.1.22-bin.jar in the project.

 create a java class named LuceneTest  and write the following.

LuceneTest .java

 package com.test;

sample jasper report

Sample Jasper Report 
Tools:   Java jdk 1.5 or above Eclipse
commons-beanutils-1.7.0.jar ,commons-collections-3.2.1.jar ,commons-digester-1.7.jar, commons-io-1.4.jar, iText-2.1.7.jar,
jasperreports-4.5.0.jar,jfreechart-1.0.12.jar, log4j.jar servlet-api.jar 
Create a Dynamic Web Project in eclipse and add a servlet name and a class. 
 package util; 
 import java.util.ArrayList;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import pojo.Person; import pojo.Phone;
 public class MyServlet  extends HttpServlet{ 
 private void process(HttpServletRequest request, HttpServletResponse response){ 
 ArrayList<Person> persons = new ArrayList<Person>();
 ArrayList<Phone> phones = null; 
 phones = new ArrayList<Phone>();
 phones.add(new Phone("Mobile 1"…

apache poi example

what is apache poi?
 Apache POI is a Java library to work with different Microsoft Office file formats such as Excel, Power point, Visio, MS Word etc.

 Lets take an example.

 Java JDK 1.5 or above,apache poi latest jar,mysql connector jar and eclipse.

 create a table name it city and insert two data example:

 create table `city` (

 `id` bigint (20),

 `city_id` varchar (12),

 `city_name` varchar (60)


 insert into `city` (`id`, `city_id`, `city_name`) values('1','KOL','Kolkata');

 insert into `city` (`id`, `city_id`, `city_name`) values('2','SIL','Siliguri');

 Now create a java project in eclipse and write a class where data retrieve from database and create a excel file to show the data.

 package com.test;


 import java.sql.Connection;

 import java.sql.DriverManager;

 import java.sql.PreparedSt…

Quartz scheduler example

Quartz scheduler example


 Step 1:
 Create a HelloJob class which implements Job Interface

 import org.quartz.Job;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;

 public class HelloJob implements Job {

 public void execute(JobExecutionContext arg0) throws JobExecutionException{

 //System.out.println("Hello World Quartz Scheduler: " + new Date());

 try {

 System.out.println("trigger Start");

 URL url=new URL("http://localhost:8080/QuartzServletExample/MyServlet");

 URLConnection conn = url.openConnection();


 BufferedReader in = new BufferedReader(new InputStreamReader(


 String inputLine;

 while ((inputLine = in.readLine()) != null)


Starting spring roo

How to create a spring roo application and deploy it in Jboss 7 Database: Mysql

 I am using jdk1.6 and java compliance 1.5 and spring IDE.  

 Step 1: create new spring roo project jpa set up

 roo> jpa setup --provider HIBERNATE --database MYSQL

 Now create entity object

 roo> entity jpa --class --testAutomatically

 Create field in entity object

 roo> field string --cityname message --notNull web mvc set up in roo shell

 roo> web mvc setup roo> web mvc all --package ~.web  
 deployment in Jboss 7  
 change persistence.xml 
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <persistence xmlns="" xmlns:xsi="" version="2.0" xsi:schemaLocation="">
 <persistence-unit name="persistenceUnit" transaction-type=&qu…