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());
}
}
}

}

0 comments: