Observer and Observable Example
Posted by joydeepdas on August 18, 2009
observer and observable example
Posted in Java | Leave a Comment »
How to read Excel Sheet using Java?
Posted by joydeepdas on July 10, 2009
How to read Excel Sheet using Java?
Posted in Java | Leave a Comment »
How to create Chart in Java? A JFreeChart Example.
Posted by joydeepdas on July 10, 2009
How to create chart in Java? A JFreeChart Example.
Posted in Java | Leave a Comment »
How to read XML file using java?
Posted by joydeepdas on July 10, 2009
How to read XML file using Java?
Posted in Java, XML | Leave a Comment »
Hibernate Search Example
Posted by joydeepdas on July 5, 2009
//—Employee.java—————————
package jd.hs.bdemo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Entity
@Table(name = “EmployeeProfile”)
@Indexed
public class Employee implements Serializable
{
@Id
@Column(name = “id”)
@GeneratedValue
@DocumentId
Integer id;
@Column(name = “name”)
@Field(index=Index.TOKENIZED, store=Store.NO)
String name;
@Column(name = “skills”)
@Field(index=Index.TOKENIZED, store=Store.NO)
String skills ;
@Column(name = “summary”)
@Field(index=Index.TOKENIZED, store=Store.NO)
String summary ;
public Employee()
{
}
public String toString()
{
return “” + getId() + ” : ” + getName() + ” : ” + getSkills () + ” : ” + getSummary() ;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSkills()
{
return skills ;
}
public void setSkills( String skills)
{
this.skills = skills ;
}
public String getSummary()
{
return summary ;
}
public void setSummary( String summary)
{
this.summary = summary ;
}
}
//— hibernate.cfg.xml——————
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd“>
<hibernate-configuration>
<session-factory name=”factory name”>
<!– Database connection settings –>
<property name=”hibernate.connection.driver_class”>org.gjt.mm.mysql.Driver</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/jd68994</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”>joy</property>
<!– SQL dialect –>
<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>
<!– JDBC connection pool (use the built-in) –>
<property name=”connection.pool_size”>1</property>
<!– Echo all executed SQL to stdout –>
<property name=”show_sql”>true</property>
<property name=”hibernate.format_sql”>true</property>
<!– Drop and re-create the database schema on startup –>
<property name=”hbm2ddl.auto”>update</property>
<!– Enable Hibernate’s automatic session context management –>
<property name=”current_session_context_class”>thread</property>
<!– Disable the second-level cache –>
<property name=”cache.provider_class”>org.hibernate.cache.NoCacheProvider</property>
<!– Mapping files –>
<mapping class=”jd.hs.bdemo.Employee”/>
</session-factory>
</hibernate-configuration>
//—- HibernateUtil.java————-
package jd.hs.bdemo;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println(“Initial SessionFactory creation failed.” + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
//—-EmployeeDAO———–
package jd.hs.bdemo;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class EmployeeDAO
{
public void save( Employee employee )
{
/** Getting the Session Factory and session */
SessionFactory sessionFactory = HibernateUtil.getSessionFactory() ;
Session session = sessionFactory.openSession() ;
/** Starting the Transaction */
Transaction tx = session.beginTransaction() ;
/** Saving POJO */
session.save( employee ) ;
/** Commiting the changes */
tx.commit() ;
System.out.println(“Record Inserted”) ;
/** Closing Session */
session.close() ;
}
public List<Employee> retrive()
{
List<Employee> listEmployee = null ;
/** Getting the Session Factory and session */
SessionFactory sessionFactory = HibernateUtil.getSessionFactory() ;
Session session = sessionFactory.openSession() ;
/** Starting the Transaction */
Transaction tx = session.beginTransaction() ;
Criteria criteria = session.createCriteria(Employee.class) ;
listEmployee = criteria.list();
/** Commiting the changes */
tx.commit() ;
System.out.println(“Records Retrived”) ;
/** Closing Session */
session.close() ;
return listEmployee ;
}
}
//——-EmployeeSearch.java———–
package jd.hs.bdemo;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
public class EmployeeSearch
{
// for indexing———————-
public void search()
{
Session session = HibernateUtil.getSessionFactory().openSession() ;
FullTextSession fullTextSession = Search.getFullTextSession(session) ;
Transaction tx = fullTextSession.beginTransaction() ;
List<Employee> employees = session.createQuery(“from Employee as employee”).list();
for (Employee employee : employees)
{
fullTextSession.index(employee);
System.out.println( “>Employee : ” + employee);
}
tx.commit(); //index is written at commit time
}
// for searching———————-
public List<Employee> searchEmployee( String searchWord ) throws ParseException
{
Session session = HibernateUtil.getSessionFactory().openSession() ;
FullTextSession fullTextSession = Search.getFullTextSession(session) ;
Transaction tx = fullTextSession.beginTransaction() ;
// create native Lucene query
String[] fields = new String[]{“name”, “skills”, “summary”};
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
org.apache.lucene.search.Query query = parser.parse( searchWord );
// wrap Lucene query in a org.hibernate.Query
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Employee.class);
// execute search
List<Employee> listOfEmployee = hibQuery.list();
tx.commit();
session.close();
return listOfEmployee ;
}
}
//———-EmployeeMain.java————-
package jd.hs.bdemo;
import java.util.List;
import org.apache.lucene.queryParser.ParseException;
public class EmployeeMain
{
public static void main(String[] args) throws ParseException
{
// Insert few employee data into database
/*
new EmployeeDAO().save( EmployeeMain.getEmployee(1, “Joy” , “Java,Hibernate,Spring,JSF” , “Natural,Adabas”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(2, “MoniLal” , “C#,ASP,.Net” , ” Fresher”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(3, “Gozo” , “C#,ASP,.Net” , ” Experince”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(4, “Bhuyan” , “Java,Ant” , ” Testing”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(5, “Gyan” , “C++,C,Oracle” , ” Java”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(6, “Gosai” , “Java,JSP,Spring,MySQL” , ” Developer”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(7, “Akthar” , “Java,JSP,MySQL,JBoss” , ” Sr Software Engineer”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(8, “Tamuli” , “Java,JSP,Hibernate,Ant,Ishape,” , ” Sr Software Engineer, BNP Paribas”) ) ;
new EmployeeDAO().save( EmployeeMain.getEmployee(9, “GhanaLK” , “Java,JSP,Struts” , ” Sr Software Engineer”) ) ;
*/
//new EmployeeSearch().search() ;
// search for employee with skill set
List<Employee> listOfEmployee = new EmployeeSearch().searchEmployee(“Ant, C++”) ;
for(Employee employee : listOfEmployee)
{
System.out.println(employee);
}
}
public static Employee getEmployee( Integer id, String name , String skills , String summary)
{
Employee employee = new Employee() ;
employee.setId(id) ;
employee.setName(name) ;
employee.setSkills(skills) ;
employee.setSummary(summary) ;
return employee ;
}
}
Posted in Hibernate | Tagged: hibernate search | Leave a Comment »
resource bundle example
Posted by joydeepdas on May 12, 2009
// TestResourceBundle.java
import java.util.ResourceBundle;
public class TestResourceBundle
{
static ResourceBundle bundle = null ;
static
{
bundle = ResourceBundle.getBundle(“messages”) ;
}
public static void main(String args[])
{
System.out.println( bundle.getString(“message.second”) );
}
}
// messages.properties
message.first=FIRST
message.second=SECOND
Posted in Java | Leave a Comment »
Serialize Java Object into XML
Posted by joydeepdas on March 12, 2009
// Employee.java
public class Employee
{
private String name ;
private double basicSalary ;
//…………
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
public String toString()
{
return getName() + ” “ + getBasicSalary() ;
}
}
// SerializeJavaObjectToXML.java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import com.thoughtworks.xstream.XStream;
public class SerializeJavaObjectToXML
{
public static void main(String args[])
{
String root = “employees” ;
String fileName = “employeeXml” ;
String element = “employee” ;
Employee employee = new Employee() ;
employee.setName(“Joydeep Das”) ;
employee.setBasicSalary(10000.00) ;
saveToXML(root, fileName, element, employee) ;
}
public static void saveToXML( String root, String fileName, String element, Employee employee )
{
try {
//Serialize the object
XStream xs = new XStream();
ObjectOutputStream out = xs.createObjectOutputStream(
new OutputStreamWriter(new FileOutputStream( fileName +“.xml”), “UTF8″), root);
xs.alias(element, ((Employee)employee).getClass()) ;
out.writeObject( employee );
out.close() ;
}
catch ( FileNotFoundException e) { e.printStackTrace(); }
catch ( Exception E ) { System.out.println( E ); }
}
}
Posted in XML | Leave a Comment »
Hello world!
Posted by joydeepdas on February 10, 2009
Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!
Posted in C | Leave a Comment »