Tuesday 29 November 2016

Fetch Record Using Struts2 & Maven

Step 1:index.jsp

         <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      
         "http://www.w3.org/TR/html4/loose.dtd">
        <html>
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
       <title>Main Page</title>
       </head>
      <body>
      <a href="view">View  Records</a>
      </body>
      </html>

Step 2: Customer.java

public class Customer {
private int customerId;
private String customerName;
private int forevenId;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getForevenId() {
return forevenId;
}
public void setForevenId(int forevenId) {
this.forevenId = forevenId;
}
}


Step 3:Fetch.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Fetch
{
ArrayList<customer> list=new ArrayList<customer>();
public ArrayList<customer> getList() {
   return list;
}
public void setList(ArrayList<customer> list) {
   this.list = list;
}
public String execute(){
try{
 Class.forName("com.mysql.jdbc.Driver");
 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mapping","root","root");
      PreparedStatement ps=con.prepareStatement("select * from customer");
 ResultSet rs=ps.executeQuery();

 while(rs.next()){
 customer user=new customer();
 user.setCustid(rs.getInt(1));
   user.setCustname(rs.getString(2));
 user.setForevenid(rs.getInt(3));
 
  list.add(user);
 }

 con.close();
}catch(Exception e){e.printStackTrace();}
       
return "success";
}
}


Step 4:displayrecords.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h3>All Records:</h3>
<s:iterator  value="list">
<fieldset>
<s:property value="custid"/><br/>
<s:property value="custname"/><br/>
<s:property value="forevenid"/><br/>
</fieldset>
</s:iterator>
</body>

</html>


Step 5:struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation
//DTD Struts Configuration 2.1//EN"  
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="anbc" extends="struts-default">
<action name="viewrecords" class="Fetch">
<result name="success">displayrecords.jsp</result>
</action>
</package>

</struts>  

Step 6: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>StrutsDemo</display-name>
  <display-name>StrutsValidation</display-name>
     <filter>
  <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
    <filter-mapping>
   <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Step 7: pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my</groupId>
  <artifactId>StrutsDemo</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>
 
  <dependencies>
  <dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-core</artifactId>
  <version>2.5.5</version>
  </dependency>
  <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
  </dependency>
  </dependencies>

</project>

Friday 18 November 2016

HibernateUtil Class

1.Create HibernateUtil Class Externally

package persistance;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().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;
    }

    public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
    }

}




2.Main Class

package FeesCategories;

import org.hibernate.Session;

import java.sql.*;

import persistance.HibernateUtil;
public class AddFessCategory {
public static void send(fees_category fc)
{
try{
Session session=HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String name=fc.getFees();
// session.save(name);
PreparedStatement ps=(PreparedStatement) session.connection().prepareStatement("insert into fees_category values(null,?)");
ps.setString(1, name);
//System.out.println("success");
int a=ps.executeUpdate();
  if(a>0)
  {
  System.out.println("inserted");
  }
     
 session.getTransaction().commit();
     
}
catch(Exception e)

{
System.out.println(e);
}
}

}