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

Tuesday, July 29, 2008

Spring Framework

The Spring Framework provides solutions to many technical challenges faced by Java developers and organizations wanting to create applications based on the Java platform. Because of the size and complexity of the functionality offered, it can be hard to distinguish the major building blocks from which the framework is composed.[neutrality disputed] The Spring Framework is not exclusively linked to the Java Enterprise platform, although its far-reaching integration in this area is an important reason for its popularity.

The Spring Framework is probably best known for offering features required to create complex business applications effectively outside of the programming models which have been dominant historically in the industry.[neutrality disputed] Next to that, it is also credited for introducing previously unfamiliar functionalities into today's mainstream development practices, even beyond the Java platform.

This amounts to a framework which offers a consistent model and makes it applicable to most application types created on top of the Java platform.

Wednesday, June 11, 2008

THE J2EE ARCHITECTURE

The J2EE Architecture allows the programmers to divide their work into two major categories:

  • Business Logic

  • Presentation Logic

Presentation Logic:

Presentation Logic consists of all the program (JSP and Servlets), images and html files that are used to interact with the client. These files are archived into .war file. These files are installed on the web server and these interacts with the users.

Business Logic:

These are EJB applications which implements the business logic of the system and are archived into .jar file. Business logic runs on the application server.

These two types of archives are bundled into complete J2EE applications, and delivered as Enterprise Archive (EAR) file.

Let's took an example of form processing. In this example J2EE application could have and HTML form, which prompts the user to input the data, a servlet to receive the data from the form and process it, and also an Enterprise Bean to store the data in a database. In this example the HTML form and servlet are archived in a WAR file, and the Enterprise Java Bean is archived into JAR file. These two archive files (WAR and JAR) both are added to the EAR file, which is finally deployed on the server.

Tuesday, June 10, 2008

Enterprise beans

An enterprise bean is a non-visual component of a distributed, transaction-oriented enterprise application. Enterprise beans are typically deployed in EJB containers and run on EJB servers. You can customize them by changing their deployment descriptors and you can assemble them with other beans to create new applications. There are three types of enterprise beans: session beans, entity beans, and message-driven beans.
  • Session beans: Session beans are non-persistent enterprise beans. They can be stateful or stateless. A stateful session bean acts on behalf of a single client and maintains client-specific session information (called conversational state) across multiple method calls and transactions. It exists for the duration of a single client/server session. A stateless session bean, by comparison, does not maintain any conversational state. Stateless session beans are pooled by their container to handle multiple requests from multiple clients.
  • Entity beans: Entity beans are enterprise beans that contain persistent data and that can be saved in various persistent data stores. Each entity bean carries its own identity. Entity beans that manage their own persistence are called bean-managed persistence (BMP) entity beans. Entity beans that delegate their persistence to their EJB container are called container-managed persistence (CMP) entity beans.
  • Message-driven beans: Message-driven beans are enterprise beans that receive and process JMS messages. Unlike session or entity beans, message-driven beans have no interfaces. They can be accessed only through messaging and they do not maintain any conversational state. Message-driven beans allow asynchronous communication between the queue and the listener, and provide separation between message processing and business logic.
Remote client view
The remote client view specification became available beginning with EJB 1.1. The remote client view of an enterprise bean is location independent. A client running in the same JVM as a bean instance uses the same API to access the bean as a client running in a different JVM on the same or different machine.
  • Remote interface: The remote interface specifies the remote business methods that a client can call on an enterprise bean.
  • Remote home interface: The remote home interface specifies the methods used by remote clients for locating, creating, and removing instances of enterprise bean classes.
Local client view
The local client view specification is available in EJB 2.0 or later. Unlike the remote client view, the local client view of a bean is location dependent. Local client view access to an enterprise bean requires both the local client and the enterprise bean that provides the local client view to be in the same JVM. The local client view therefore does not provide the location transparency provided by the remote client view. Local interfaces and local home interfaces provide support for lightweight access from enterprise bean that are local clients. Session and entity beans can be tightly couple with their clients, allowing access without the overhead typically associated with remote method calls.
  • Local interface: The local interface is a lightweight version of the remote interface, but for local clients. It includes business logic methods that can be called by a local client.
  • Local home interface: The local home interface specifies the methods used by local clients for locating, creating, and removing instances of enterprise bean classes.
Web service client view
In the EJB 2.1 specification, the EJB architecture introduced the support for Web services. A client for a session bean can be a Web service client. A Web service client can make use of the Web service client view of a stateless session bean, which has a corresponding service endpoint interface..
Service endpoint interface
The service endpoint interface for a stateless session bean exposes the functionality of the session bean as a Web service endpoint. The Web Service Description Language (WSDL) document for a Web service describes the Web service as a set of endpoints operating on messages. A WSDL document can include the service endpoint interface of a stateless session bean as one of its endpoints. An existing stateless session bean can be modified to include a Web service client view, or a service endpoint interface can be mapped from an existing WSDL to provide the correct interface.

A Web service client view is independent of location and can be accessed through remote calls.

EJB client JAR file
An EJB client JAR file is an optional JAR file that can contain the client interfaces that a client program needs to use the client views of the enterprise beans that are contained in the EJB JAR file. If you decide not to create an EJB client JAR file for an EJB module, all of the client interface classes will be in the EJB JAR file. By default, the workbench creates EJB client JAR projects for each corresponding EJB project.
EJB container
An EJB container is a runtime environment that manages one or more enterprise beans. The EJB container manages the life cycles of enterprise bean objects, coordinates distributed transactions, and implements object security. Generally, each EJB container is provided by an EJB server and contains a set of enterprise beans that run on the server.
Deployment descriptor
A deployment descriptor is an XML file packaged with the enterprise beans in an EJB JAR file or an EAR file. It contains metadata describing the contents and structure of the enterprise beans, and runtime transaction and security information for the EJB container.
EJB server
An EJB server is a high-level process or application that provides a runtime environment to support the execution of server applications that use enterprise beans. An EJB server provides a JNDI-accessible naming service, manages and coordinates the allocation of resources to client applications, provides access to system resources, and provides a transaction service. An EJB server could be provided by, for example, a database or application server.