Posts

Showing posts from 2016

Converting JSONArray into CSV file in java

package com.loginController; import java.io.File; import org.apache.commons.io.FileUtils; import org.json.CDL; import org.json.JSONArray; import org.json.JSONObject; /**  *  * @author anand  *  */ public class ConvertArray {     public static void main(String myHelpers[]){         String jsonArrayString = "{\"fileName\": [{\"name\": \"Anand\",\"last\": \"Dwivedi\",\"place\": \"Bangalore\"}]}";         JSONObject output;         try {             output = new JSONObject(jsonArrayString);             JSONArray docs = output.getJSONArray("fileName");             File file=new File("JSONSEPERATOR.csv");             String csv = CDL.toString(docs);             FileUtils.writeStringToFile(file, csv);             System.out.println("Data has been Sucessfully Writeen to "+file);         } catch (Exception e) {             e.printStackTrace();         }    

Custom Sorting in Java

How Custom Sorting will work in java # Some time we may got the requirement to Sort generic Class level list into Default Sorting order in that case we will have to write own custom logic for that . below is the code to perform Custom logic in java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package com; import java.util.*; /**  *  * @author anand  */ public class CustomSorting {     private String username;     private String userplace;     private String userdesignation;     static List<CustomSorting> details = null;     public String getUsername() {         return username;     }     public void setUsername(String username) {         this.username = username;     }     public String getUserplace() {         return userplace;     }     public void setUserplace(String userplace) {         this.userpla

How to read CSV File in Java

Here we can learn how to read CSV file comma seprated in java below is the code --> Use apache API to read CSV or use BufferReader --> here i have used Buffer Reader import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /**  *  * @author anand  *  */ public class ReadCSVFile {     public static void main(String[] args) {               BufferedReader br = null;         String line = "";         String cvsSplitBy = ","; //Comma seprated         try {             br = new BufferedReader(new FileReader("Your_CSV_FILE_NAME"));             while ((line = br.readLine()) != null) {                 System.out.println("Content \t"+line);             }         } catch (Exception e) {             e.printStackTrace();         } finally {             if (br != null) {                 try {                     br.close();                 } catch (I

Attribute Override in Hibernate

Image
Model Class /**  *  */ package com.learnjavabyanand; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /**  * @author ananddw  *  */ @Entity @Table(name="student_details") public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int studentrollnumber; @Column(name="student_name") private String studentName; @AttributeOverrides({ @AttributeOverride(name="company_location",column=@Column(name="company1_location")), @AttributeOverride(name="company_name",column=@Column(name="company1_name"))}) private CompanyDetails comany_detalils; public CompanyDetails getComany_detali

Class Embedded In Hibernate

Image
Model Class /**  *  */ package com.learnjavabyanand; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /**  * @author ananddw  *  */ @Entity @Table(name="student_details") public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int studentrollnumber; @Column(name="student_name") private String studentName; @Embedded private CompanyDetails comany_detalils; public CompanyDetails getComany_detalils() { return comany_detalils; } public void setComany_detalils(CompanyDetails comany_detalils) { this.comany_detalils = comany_detalils; } public int getStudentrollnumber() { return studentrollnumber; } public void setStudentrollnumber(int studentrollnumber) { this.studentrollnumber = s

Auto Increment Column Name in Hibernate

Image
Model Class /**  *  */ package com.learnjavabyanand; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /**  * @author ananddw  *  */ @Entity @Table(name="student_details") public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int studentrollnumber; @Column(name="student_name") private String studentName; public int getStudentrollnumber() { return studentrollnumber; } public void setStudentrollnumber(int studentrollnumber) { this.studentrollnumber = studentrollnumber; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } } Client Class:  package com.learnjavabyanand; import org.hibernate.Session; import org

Retrive Obect In Hibernate

Image
Model Class package com.learnjavabyanand; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; /**  * @author ananddw  *  */ //@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity @Entity(name="employee_Details") public class EmployeeDetails { //we can put this annotation above getter as well @Id @Column(name="employee_name") private String employeeName; //ignore this variable and not create column for us private String employeeNumber; @Column(name="employee_location") private String employeeLocation ; //Used because to store Date not full time stamp @Temporal(TemporalType.DATE) private Date todayDate; public String getEmployeeName() { return empl

Transiant ,Temporaland table Annotation in Hibernate

Image
Model Class:  package com.learnjavabyanand; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; /**  * @author ananddw  *  */ //@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity @Entity(name="employee_Details") public class EmployeeDetails { //we can put this annotation above getter as well @Id @Column(name="employee_name") private String employeeName; //ignore this variable and not create column for us @Transient private String employeeNumber; @Column(name="employee_location") private String employeeLocation ; //Used because to store Date not full time stamp @Temporal(TemporalType.DATE) private Date todayDate; public String getEmployeeName()

HiberNate Simple Application Using Annotation

Image
Model Class:  package com.learnjavabyanand; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; /**  * @author ananddw  *  */ //@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity @Entity(name="employee_Details") public class EmployeeDetails { //we can put this annotation above getter as well @Id @Column(name="employee_name") private String employeeName; @Column(name="employee_number") private String employeeNumber; @Column(name="employee_location") private String employeeLocation ; public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeNumber() { return employeeNumber; } public void setEmployeeNumber(String employeeNumber) { this.employeeNum

How to send Mail in Java

package com.sendMail; import java.io.FileInputStream; import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /**  *  * @author ananddw  *  */ public class EmailService { public boolean  SendMail(String voucherID){        boolean flag=false;     Properties props = System.getProperties();     props.put("mail.smtp.host", " smtp.gmail.com "); //SMTP Host     props.put("mail.smtp.port", "587"); //TLS Port     props.put("mail.smtp.auth", "true"); //enable authentication     props.put("mail.smtp.starttl

Action Message and Action Error in Struts2

Image
Find below code to check how action Error and Action Message is working in Struts2 ####login.jsp <%--     Document   : login     Created on : Oct 09, 2016, 2:50:34 PM     Author     : shiboo --%> <!DOCTYPE html> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>Login</title> </head> <body> <center><h3>ActionMessage Example</h3> <s:actionerror /> <s:form action="login"> <s:textfield name="userName"  label="Username" /> <s:password name="password"  label="password" /> <s:submit value="Login" /> </s:form> </center> </body> </html> ####success.jsp <%--     Document   : success     Created on : Oct 09, 2016, 2:41:34 PM     Author     : shiboo --%> <%@ taglib prefix="s" uri="/struts-tags"%> &l

Hibernate Application Java

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight  tool.  An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database. Example Student.java package com.hibernate; /**  *  * @author ananddw  *  */ public class Student {         private int id;     private String studentName;     private String studentPlace;         //Setter and Getters         public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getStudentName() {         return studentName;     }     public void setStudentName(String studentName) {         this.studentName = studentName;     }     public String getStudentPlace() {         return studentPlace;     }     public void setStudentPlace(String studentPlace) {         this