Tuesday, October 12, 2010

Wednesday, September 8, 2010

codes struts jpa

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

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author DELL
*/
public class OrderEntity {

Connection con;
private int id;
private String firstName;
private String lastName;
private int age;
public OrderEntity(){

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/orderprocessing",
"root", "");
}
catch (Exception e) {
System.out.println(e);
System.exit(0);
}

}

public void save(String fName, String lName, int age){
try {
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement("INSERT INTO `person`(`firstName` ,`lastName` ,`age`) VALUES (?, ?, ?);");
stmt.setString(1, fName);
stmt.setString(2, lName);
stmt.setInt(3, age);
stmt.executeUpdate();
con.commit();
} catch (SQLException ex) {
try {
con.rollback();
} catch (SQLException ex1) {
Logger.getLogger(OrderEntity.class.getName()).log(Level.SEVERE, null, ex1);
}
Logger.getLogger(OrderEntity.class.getName()).log(Level.SEVERE, null, ex);
}
finally{
try {
con.setAutoCommit(true);
} catch (SQLException ex) {
Logger.getLogger(OrderEntity.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

public double averageAge(String lName){
try {
PreparedStatement stmt = con.prepareStatement("SELECT AVG( `age` ) FROM `person` WHERE `lastName` LIKE ?;");
stmt.setString(1, "%"+lName+"%");
ResultSet result = stmt.executeQuery();
result.next();
return result.getDouble(1);
} catch (SQLException ex) {
System.out.println("$$$$$$$$$$$$ "+ex.getMessage());
return -1;
}

}

public List getCustomers(){
List orders = new ArrayList();
OrderEntity entity = null;
try {
PreparedStatement stmt = con.prepareStatement("SELECT * FROM `person`;");
ResultSet result = stmt.executeQuery();

while(result.next()){
entity = new OrderEntity();
entity.setId(result.getInt(1));
entity.setFirstName(result.getString(2));
entity.setLastName(result.getString(3));
entity.setAge(result.getInt(4));
orders.add(entity);
}
System.out.println("$$$$$$$$$$$$ "+orders.size());
return orders;
} catch (SQLException ex) {
System.out.println("$$$$$$$$$$$$ "+ex.getMessage());
return null;
}
}

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}

/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}

/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* @return the age
*/
public int getAge() {
return age;
}

/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}


---------------------------------------------------------------------------------

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

package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Person;

/**
*
* @author DELL
*/
public class PersonServiceImpl implements PersonService{

Connection con;

public void save(Person person) {
try {
con = DbConnection.getInstance();
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement("INSERT INTO `person`(`firstName` ,`lastName` ,`age`) VALUES (?, ?, ?);");
stmt.setString(1, person.getFirstName());
stmt.setString(2, person.getLastName());
stmt.setInt(3, person.getAge());
stmt.executeUpdate();
con.commit();
} catch (Exception e) {
try {
con.rollback();
} catch (SQLException ex) {
System.out.println("************************ "+ex.getMessage());
}
}finally{
try {
con.setAutoCommit(true);
} catch (SQLException ex) {
System.out.println("************************ "+ex.getMessage());
}
}
}

}

Struts with netbeans - part 1


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
person

struts2

org.apache.struts2.dispatcher.FilterDispatcher




struts2
/*




index.jsp





"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">





/pages/insertSuccessful.jsp






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

package action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import model.Person;
import service.PersonService;
import service.PersonServiceImpl;

/**
*
* @author DELL
*/
public class PersonAction extends ActionSupport{

private PersonService service;
private List persons;
private Person person;
private int id;
private String firstName;
private String lastName;
private int age;

public String save(){
service = new PersonServiceImpl();
person = new Person();
person.setAge(age);
person.setFirstName(firstName);
person.setLastName(lastName);
service.save(person);
return SUCCESS;
}

/**
* @return the service
*/
public PersonService getService() {
return service;
}

/**
* @param service the service to set
*/
public void setService(PersonService service) {
this.service = service;
}

/**
* @return the persons
*/
public List getPersons() {
return persons;
}

/**
* @param persons the persons to set
*/
public void setPersons(List persons) {
this.persons = persons;
}

/**
* @return the person
*/
public Person getPerson() {
return person;
}

/**
* @param person the person to set
*/
public void setPerson(Person person) {
this.person = person;
}

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}

/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}

/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* @return the age
*/
public int getAge() {
return age;
}

/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}



<%--
Document : input
Created on : Sep 7, 2010, 6:41:26 PM
Author : DELL
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
"http://www.w3.org/TR/html4/loose.dtd">




JSP Page













<%--
Document : response
Created on : Sep 7, 2010, 6:41:41 PM
Author : DELL
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
"http://www.w3.org/TR/html4/loose.dtd">




JSP Page


Insert Successful






Average Age





List of Customers





<%--
Document : input
Created on : Sep 7, 2010, 6:41:26 PM
Author : DELL
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
"http://www.w3.org/TR/html4/loose.dtd">




JSP Page






">







Home
















commons-fileupload-1.2.1
commons-logging-1.0.4
freemarker-2.3.13
mysql-connector-java-5.0.7-bin
ognl-2.6.11
struts2-core-2.1.6
xwork-2.1.2

web-inf/classes ------- struts.xml
web-inf/lib libraries



download

http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html