Tuesday, November 16, 2010

Hibernate Full Web Application in NetBeans

New->Hibernate Mapping Wizard(This file should in default package)

Select->MySQL Wizard->Finish(Click on finish button)


Double click on Hibernate.cfg.xml->click on XML tab(View like this)
then add mapping class in to this XML(last section in XML file)



After this add Hibernate mapping wizard


















set Hibernate mapping wizard as Users.hbm

















class to map :com.Users(package_name.class_name)
select configure file:hibernate.cfg.xml
database table:Users(table_name)

Then click on xml view for Users.hbm add those steps
















ADD Java file( Users.java)

package com;


public class Users {

private int id;
private String user;
private String pass;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getPass() {
return pass;
}

public void setPass(String pass) {
this.pass = pass;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}
}


Then create Table

create table users
(
userid int auto_increment primary key,
user_name varchar(40),
pasword varchar(40)
)


Then Create servlet file(saveUser.java)

















import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;


public class saveUser extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();

String user=request.getParameter("userName");
String pass=request.getParameter("passName");
Users Objectuser = new Users();

Objectuser.setUser(user);
Objectuser.setPass(pass);

try
{
Transaction tx=null;

SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();

Session session = sf.openSession();

tx=session.beginTransaction();
session.save(Objectuser);
session.getTransaction().commit();
session.close();
out.println("Inserted");
}
catch (Exception e) {
System.out.println("Error :"+e.getMessage());
out.println("Error :"+e.getMessage());
}
}
}

Finally create JSP Page




Saturday, February 6, 2010

Jasper Reports(Reports in Java)for Java Developers in netbeans


Embedding SQL Queries into a Report Template save as customerReport1.jrxml
(put into src/reports directories)
(Please send your feedback)












Add libraries files in Net-beans

  • jasperreports-3.1.3.jar
  • commons-beanutils-1.7.jar
  • commons-collections-2.1v
  • commons-digester-1.7.jar

Finally run this program

JasperView.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Gowthaman
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
public class JasperView {
Connection connection;
public void generateReport() throws FileNotFoundException, IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection ("Jdbc:mysql://localhost/samples", "root", "password");
System.out.println("Connected to the database");
System.out.println("Filling reports...");

JasperReport jasperReport;
JasperPrint jasperPrint;
jasperReport=JasperCompileManager.compileReport("src/reports/customerReport1.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(),connection);
JasperExportManager.exportReportToPdfFile(jasperPrint, "src/reports/ProvaReport.pdf");
//automatically will created if you want PDF add library itext.jar
String reportDest = "src/reports/report.html";//automatically will created
JasperViewer jv = new JasperViewer(jasperPrint);
jv.setVisible(true);
JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest);
System.out.println("Done!Html and PDF");
connection.close();
}
catch (JRException e)
{
e.printStackTrace();
System.out.println("Jasper Exception:"+e.getMessage());
System.out.println("Jasper Exception:"+e.getStackTrace());
System.out.println("Jasper Exception:"+e.getLocalizedMessage());
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("There is exception here"+ e);
}
}
public static void main(String[] args) throws FileNotFoundException, IOException
{
new JasperView().generateReport();
}

}


Database Table

create table employee(
firstname varchar(40),
lastname varchar(40)
)