Search This Blog

Follow by Email

Monday, October 28, 2019

Jasper Reports export to EXCEL, CSV and PDF Using JRXML Java and My SQL



This Post will explain , How to export different formats using Jasper and Jrxml using java

Step 1: Download Download Jasper Studio
or I Report Designer


Step 2 : Design your page by providing connection details and query details and what columns needs to be display in PDF/excel/CSV


Step 3: Your design Page details can be mentioned in Summary Band.






 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
  
   
  
  
   
   
  
  
   
   
  
  
   
   
  
  
   
   
  
  
   
  
  
   
  
  
   
  
  
   
  
 
 
  
 
 
  
  
 
 
  
  
 
 
  
  
 
 
  
  
 
 
  
   
   
    
     
     
     
     
     
    
    
     
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
    
   
  
 






Step 4: Once design completes , then it is time to write java class to export into different formats


Step 5: Excel report Generation





package arrayListTest;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.export.SimpleCsvExporterConfiguration;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;

public class GenerateMultipleReportsFromJasper {
 
  public static void main(String[] args) {

  //Jrxml file source
  String sourceFileName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\csv_export.jrxml";
  //destination file location
  String outXlsName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.xls";
  //If we want to pass any parameters to jasper report, then we can use this map
  HashMap xlsParams = new HashMap();
  Connection con = null;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    // here employee is database name, root is username and password
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root", "root");
    JasperReport report = JasperCompileManager.compileReport(sourceFileName );
    JasperPrint xlsPrint = JasperFillManager.fillReport(report, xlsParams, con);
    JRXlsExporter xlsExporter = new JRXlsExporter();
    xlsExporter.setExporterInput(new SimpleExporterInput(xlsPrint));
    xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outXlsName));
    SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
    xlsReportConfiguration.setOnePagePerSheet(false);
    xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(true);
    xlsReportConfiguration.setDetectCellType(false);
    xlsReportConfiguration.setWhitePageBackground(false);
    xlsExporter.setConfiguration(xlsReportConfiguration);
    xlsExporter.exportReport();
  } catch (Exception e) {
      System.out.println(e);
    }
    finally{
      try{
 if(con != null){
    con.close();
 }
 
      }
    catch(Exception ex){
 }
    }
  }

}


Step 6: CSV Generation




HashMap csvParamsMap = new HashMap();
String outcsvName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.csv";
JasperReport report1 = JasperCompileManager.compileReport(sourceFileName );
JasperPrint jasperPrint = JasperFillManager.fillReport(report1, csvParamsMap, con);
JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(outcsvName)));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
configuration.setWriteBOM(Boolean.TRUE);
configuration.setRecordDelimiter("\r\n");
exporter.setConfiguration(configuration);
exporter.exportReport();


Step 7: PDF Generation




HashMap pdfParamsMap = new HashMap();
String outPdfName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.pdf";
JasperReport report2 = JasperCompileManager.compileReport(sourceFileName );
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, pdfParamsMap, con);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint2));
pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outPdfName));
SimplePdfExporterConfiguration pdfConfiguration = new SimplePdfExporterConfiguration();
pdfConfiguration.setCreatingBatchModeBookmarks(true);
pdfExporter.setConfiguration(pdfConfiguration);




Step 8: Required jar file to execute this program



1. Itext 2.1.7.jar
2. commons-beanutils-1.9.2.jar
3. commons-logging-1.2.jar
4. commons-collections-3.2.1.jar
5. commons-lang-2.6.jar
6. commons-digester-1.6.jar
7. jasperreports-6.2.0.jar
8. mysql-connector-java-5.1.40.jar
9. poi-ooxml-3.16.jar
10. poi-3.12.jar



Step 9: Once We downloaded all jars then add into java build path.

Step 10: This is how we can export different format using jasper jrxml and Java.


Saturday, August 10, 2019

Getting started with Kubernetes and Install Kubernetes on Windows , Tools for Kubernetes Cluster



1. What is Kubernetes?
It is an open source system for automating deployment, scaling and managing containerized applications

How to install Kubernetes?

1. Install Kube control(kubectl) on Windows
2. kubectl (kubernet command control tool) , used to run the commands in kubernetes clusters.
3. Before install, we have to make sure each client version has to work with minor difference with master
For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master

4. Download from the given link Download Kubernetes
5. Add the binary in to your PATH.(System - Environment Variables)
6.Check the version using kubectl version

We can create sample cluster online using following link

Online interactive kubernetes cluster

Popular tools for kubernetes clusters
1. (Mini Kube)
2. Google Kubernetes Engine (GKE)
3. Amazon Elastic Kubernetes Service (EKS):
4. Azure Kubernetes Service (AKS)

Wednesday, August 8, 2018

Getting started Machine Learning Using R on Windows environment Step By Step Process.


This post will explain us, how to install R on windows environment and how to work with Machine learning project using R with simple dataset

First Download R https://cran.r-project.org/bin/windows/base/ from this link.
You can download latest version of R.
1. Once download completes, then install the same in your machine. This is like any other software installation. There is no special instructions required for this.
2. After successful installation we need to setup the path- go to MyComputer-RightClick- environment variables- System variables-
C:\Program Files\R\R-3.5.1\bin


3. After setting up the path, Now we need to start the R
4. Go to command prompt and Type R
5. Now we can see the simple R terminal


6. Now we will understand what is machine learning? and what is datasets?
7. When we are applying machine learning to our own datasets, we are working on a project.
The process of a machine learning project may not be linear, but there are a number of well-known steps:

Define Problem.
Prepare Data.
Evaluate Algorithms.
Improve Results.
Present Results.

8.The best way to really come in terms with a new platform or tool is to work through a machine learning project end-to-end and cover the key steps.
Namely, from loading data, summarizing your data, evaluating algorithms and making some predictions.
Machine Learning using R Step By Step
Now this the time to work simple machine learning program using R and inbuilt dataset called iris

We already installed R and it has started.
Install any default packages using following syntax.


Packages are third party add-ons or libraries that we can use in R.

install.packages("caret")
//While installing the package, after typing the above command , it will ask us for select mirror, you can select default one.
  install.packages(“caret”,dependencies=c(“Depends”,”Suggests”))
  install.packages(“ellipse”)
//Load the package, which we are going to use.
libray(caret)
Load the data from inbuilt data and rename the same using following syntax.
// Attach iris dataset to the current environment
  data(iris)
// Rename iris dataset  to dataset
 dataset <- iris
Now iris data loaded in R and accessible with variable called dataset Now we will create validation dataset. We will split the loaded dataset into two, 80% of which we will use to train our models and 20% that we will hold back as a validation dataset.
//  We create a list of 80% of the rows in the original dataset we can use for training
validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)
//select 20% of the data for validation
validation <- dataset[-validation_index,]
//use the remaining 80% of data to training and testing the models
dataset <- dataset[validation_index,]

          
Now we have training data in the dataset variable and a validation set we will use later in the validation variable. Note that we replaced our dataset variable with the 80% sample of the dataset. 1. dim function We can get a quick idea of how many instances (rows) and how many attributes (columns) the data contains with the dim function.
dim(dataset)
2. Attribute types - Knowing the types is important as it will give us an idea of how to better summarize the data we have and the types of transforms we might need to use to prepare the data before we model it.
sapply(dataset,class)
3. head function used to display the first five rows.
head(dataset)
4. The class variable is a factor. A factor is a class that has multiple class labels or levels
levels(dataset$Species)
5. Class Distribution Let’s now take a look at the number of instances (rows) that belong to each class. We can view this as an absolute count and as a percentage. 6. Summary of each Attribute
    summary(dataset)
Visualize Dataset We now have a seen the basic details about the data. We need to extend that with some visualizations. We are going to look at two types of plots: 1. Univariate plots to better understand each attribute. 2. Multivariate plots to better understand the relationships between attributes. First we will see the Univariate plots, this is for each individual variable. Input attributes x and the output attributes y.
  //Split input and output
    x <- dataset[,1:4]
    y <- dataset[,5]
Given that the input variables are numeric, we can create box and whisker plots of each.
   par(mfrow=c(1,4))
   for(i in 1:4) {
   boxplot(x[,i], main=names(iris)[i])
 }
 
We can also create a barplot of the Species class variable to get a graphical representation of the class distribution (generally uninteresting in this case because they’re even).
plot(y)
This confirms what we learned in the last section, that the instances are evenly distributed across the three class: Multivariate Plots First let’s look at scatterplots of all pairs of attributes and color the points by class. In addition, because the scatterplots show that points for each class are generally separate, we can draw ellipses around them.
featurePlot(x=x,y=y,plot=”ellipse”)
We can also look at box and whisker plots of each input variable again, but this time broken down into separate plots for each class. This can help to tease out obvious linear separations between the classes.
featurePlot(x=x,y=y,plot=”box”)
Next we can get an idea of the distribution of each attribute, again like the box and whisker plots, broken down by class value. Sometimes histograms are good for this, but in this case we will use some probability density plots to give nice smooth lines for each distribution.
// density plots for each attribute by class value
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)
Evaluating the Algorithms Set-up the test harness to use 10-fold cross validation. We will split our dataset into 10 parts, train in 9 and test on 1 and release for all combinations of train –test splits. We will also repeat the process 3 times for each algorithm with different splits of the data into 10 groups We are using the metric of “Accuracy” to evaluate models. This is a ratio of the number of correctly predicted instances in divided by the total number of instances in the dataset multiplied by 100 to give a percentage (e.g. 95% accurate). We will be using the metric variable when we run build and evaluate each model next.
control <- tarinControl(method=”csv”,number=10)
     metric <- “Accuarcy”
Build 5 different models to predict species from flower measurements Linear Discriminant Analysis (LDA) Classification and Regression Trees (CART). k-Nearest Neighbors (kNN). Support Vector Machines (SVM) with a linear kernel. Random Forest (RF)
set.seed(7)
fit.lda <- train(Species~., data=dataset, method="lda", metric=metric, trControl=control)
# b) nonlinear algorithms
# CART
set.seed(7)
fit.cart <- train(Species~., data=dataset, method="rpart", metric=metric, trControl=control)
# kNN
set.seed(7)
fit.knn <- train(Species~., data=dataset, method="knn", metric=metric, trControl=control)
# c) advanced algorithms
# SVM
set.seed(7)
fit.svm <- train(Species~., data=dataset, method="svmRadial", metric=metric, trControl=control)
# Random Forest
set.seed(7)
fit.rf <- train(Species~., data=dataset, method="rf", metric=metric, trControl=control)






We reset the random number seed before reach run to ensure that the evaluation of each algorithm is performed using exactly the same data splits. It ensures the results are directly comparable. Select the best model. We now have 5 models and accuracy estimations for each. We need to compare the models to each other and select the most accurate. We can report on the accuracy of each model by first creating a list of the created models and using the summary function.
# summarize accuracy of models
results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))
summary(results)

We can also create a plot of the model evaluation results and compare the spread and the mean accuracy of each model. There is a population of accuracy measures for each algorithm because each algorithm was evaluated 10 times (10 fold cross validation)
dotplot(results)
The results can be summarized. This gives a nice summary of what was used to train the model and the mean and standard deviation (SD) accuracy achieved, specifically 97.5% accuracy +/- 4% How to Predictions using predict and confusion Matrix The LDA was the most accurate model. Now we want to get an idea of the accuracy of the model on our validation set. This will give us an independent final check on the accuracy of the best model. It is valuable to keep a validation set just in case you made a slip during such as overfitting to the training set or a data leak. Both will result in an overly optimistic result. We can run the LDA model directly on the validation set and summarize the results in a confusion matrix.
predictions <- predict(fit.lda,validation)
    confusionMatrix(predictions,validation$Species)
   

Thursday, August 2, 2018

REST, REST Security, REST API Methods, REST annotations



REST - Representational State Transfer

1. REST is Architecture Style implementation

2. REST implemenation is based on Json Over HTTP

3. REST implemented based on simple HTTP protocol

4. REST has better scalability and performance

5. REST permits more data formats like JSON,XML etc..

6. REST emphasizes scalability of component interactions, independent deployments of components.

7. REST is design of HTTP and URI standards

8. REST is follow http methods like GET,POST,PUT,DELETE,PATCH

9. HTTP PATCH requests are to make partial update on a resource.
PUT requests also modify a resource entity so to make more clear –
PATCH method is the correct choice for partially updating an existing resource
and PUT should only be used if we are replacing a resource in it’s entirety.

10. REST impelnetations using JAX-RS and Jersy

11. Annotations of JAX-RS

@Context

Injects information into a class field, bean property, or method parameter

@CookieParam

Extracts information from cookies declared in the cookie request header

@FormParam

Extracts information from a request representation whose content type is application/x-www-form-urlencoded

@HeaderParam

Extracts the value of a header

@MatrixParam

Extracts the value of a URI matrix parameter

@PathParam

Extracts the value of a URI template parameter

@QueryParam

Extracts the value of a URI query parameter

12. HTTP Status codes

200 OK - Response to a successful REST API action. The HTTP method can be GET, POST, PUT, PATCH or DELETE.
400 Bad Request - The request is malformed, such as message body format error.
401 Unauthorized - Wrong or no authentication ID/password provided.
403 Forbidden - It's used when the authentication succeeded but authenticated user doesn't have permission to the request resource.
404 Not Found - When a non-existent resource is requested.
405 Method Not Allowed - The error checking for unexpected HTTP method. For example, the RestAPI is expecting HTTP GET, but HTTP PUT is used.


13. REST security

javax.ws.rs.core.SecurityContext interface to implement security programmatically


   GET
        @Produces("text/plain;charset=UTF-8")
        @Path("/hello")
        public String updateUser(@Context SecurityContext sc) {
                if (sc.isUserInRole("admin"))  return "User will be updated";
                throw new SecurityException("User is unauthorized.");
        }

Applying annotations to your JAX-RS classes

DeclareRoles

Declares roles.

DenyAll

Specifies that no security roles are allowed to invoke the specified methods.

PermitAll

Specifies that all security roles are allowed to invoke the specified methods.

RolesAllowed

Specifies the list of security roles that are allowed to invoke the methods in the application.

RunAs

Defines the identity of the application during execution in a J2EE container.


@Path("/helloUser")
@RolesAllowed({"ADMIN", "DEV"})
public class helloUser {

   @GET
   @Path("updateUser")  
   @Produces("text/plain")
   @RolesAllows("ADMIN")
   public String updateUser() {
      return "User Updated!";
   }
}

Updating the web.xml deployment descriptor to define security configuration


         
             Users
             /user
             GET
             POST
         
         
             admin 
         
    
        
            BASIC
            default
        
    
        admin
    
 
 

Thanks for viewing this post. If you like it don't forget to provide comments


Sunday, July 15, 2018

Given Strings are anagrams or not using java



What is String Anagram?

"An anagram is a type of word, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.

for Example: 'java' can be rearranged as - 'vaaj' or 'vjaa' -all characters are equal in both the words. So this is called string anagram.

Now we will see how to write a java program to check whether given strings are anagram or not.


package com.siva;

import java.util.Arrays;

public class StringAnagramTest {
 
 
  public boolean isStringAnagram(String s1, String s2){
   char[] charArray1 = s1.toCharArray();
   char[] charArray2 = s2.toCharArray();
   Arrays.sort(charArray1);
   Arrays.sort(charArray2);
   boolean stringAnagram = Arrays.equals(charArray1, charArray2);
   return stringAnagram;
  }
  
  public static void main(String[] args) {
   StringAnagramTest test = new StringAnagramTest();
    String str1 ="test";
    String str2 ="sett";
  boolean stringAnagram = test.isStringAnagram(str1, str2);
   if(stringAnagram){
    System.out.println("Given Strings["+str1+"] and ["+str2 +"] are anagrams");
   }else{
    System.out.println("Given Strings["+str1+"] and ["+str2 +"] are not anagrams");
   }
}

}




We can provide any value as input for both the strings.

Detailed Explanation:

Step 1: Convert given Strings into character array.

Step 2: Sort the converted character array using Arrays.sort

Step 3: compare both the strings using Arrays.equals

There are different ways to implement the same logic by writing our own sorting and equals methods. this is simple way to find the result.

output:



Given Strings[test] and [sett] are anagrams


Thursday, March 1, 2018

Java Program to check whether given word is Palindrome or not


This post will explain us, how to implement palindrome logic with different ways, using java.

public class WordPalindrome {
  public static void main(String[] args) {
   String word="LIRIL";
   boolean isPalin = isPalindromeOne(word);
   if(isPalin){
    System.out.println("Given word ["+word+"] is palindrome");
   }else{
    System.out.println("Given word ["+word+"] is not palindrome");
   }
   word = "Hello";
   isPalin = isPalindromeOne(word);
   if(isPalin){
    System.out.println("Given word ["+word+"] is palindrome");
   }else{
    System.out.println("Given word ["+word+"] is not palindrome");
   }
   
  }


  public static boolean isPalindromeOne(String word){
   boolean isPalindrome = false;
   //first way of checking
   char[] array = word.toCharArray();
   char[] newArray = new char[array.length];
   for (int i = 0; i < array.length; i++) {
  char c = array[array.length-i-1];
  newArray[i] =(char)c;
 }
   if(word.equalsIgnoreCase(String.valueOf(newArray))){
    isPalindrome = true;
   }
   //second way of checking
   String reverse ="";
   for (int i = 0; i < word.length(); i++) {
  char c= word.charAt(i);
  reverse = c+reverse;
 }
   if(reverse.equalsIgnoreCase(word)){
    isPalindrome = true; 
   }
   
   
 
   return isPalindrome;
  }
  
  
  
}


Saturday, February 10, 2018

Jersy Rest Webervice with eclipse and Weblogic 12c or tomcat


How to work with jersy rest webservice using eclipse and weblogic server/tomcat

Step 1: Create a dynamic web project in eclipse and provide project name





Step 2: open web.xml and add below configuration code for rest webservices.



  jersyRestService
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
      
        Jersey Web Application
        com.sun.jersey.spi.container.servlet.ServletContainer
    
    
        Jersey Web Application
        /rest/*
    



Step 3: Create one file name called weblogic.xml and add the below code.



12.2
    
        jax-rs
        2.0
        2.22.1.0
        false
    



Step 4: Download rest api related jar -http://repo1.maven.org/maven2/com/sun/jersey/jersey-archive/1.19.1/jersey-archive-1.19.1.zip Unzip the same and place the jar inside lib folder of the project.


Step 5: Create a class name called RestServiceAPI.java add below code.

package com.siva.restservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
 * 
 * @author Siva
 *
 */

@Path("/helloRest")
public class SimpleRestService {
	
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getDetails(){
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice";
	}
	@GET
	@Path("/getDetail/{requestParam}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getDetailsWithParameter(@PathParam("requestParam") String requestParam){
		System.out.println("USer request paramater["+requestParam +"]");
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice you have given input as -"+requestParam;
	}

}



Step 6: Create one config class called- ApplicationConfig.java
Right click on src-> Webservice->RestWebservice->select Classname- which class needs to be configured in ApplicationConfig Click Next-> Finish

package rest.application.config;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
 * 
 * @author Siva
 *
 */
@ApplicationPath("resources")
public class ApplicationConfig  extends Application{

	public Set> getClasses(){
		return getRestClasses();
	}
	
	private Set> getRestClasses(){
	  Set> resources = new HashSet>();	
	  resources.add(com.siva.restservice.SimpleRestService.class);
	  return resources;
	}
	
}



Step 7: This is very important step

Run the weblogic server and open the console
http://localhost:7001/console provide username and password and login the same into weblogic server admin console

Step 8: Click on the Deployments- the install the jax-rs-2.0.war, which is inside-
Oracle_home/wlserver/common/deployable-libraries
If you are working on jsr311 then deploy jsr311-api-111.war which is available on same path.


For Tomcat Deployment Step 3 , 6,7,8 not required

Right click on the Project and Run AS - server- select appropriate server and deploy the same.

if you want to test in browser use-
http://localhost:8080/jersyRestService/rest/helloRest
http://localhost:8080/jersyRestService/rest/helloRest/getDetail/helloRequest


Step 9: Create rest client class which get the test results for rest webservice
package com.siva.restservice.test;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
/**
 * 
 * @author Siva
 *
 */
public class RestServiceClient {
	
	public static void main(String[] args) {
		String uri = "http://localhost:8080/jersyRestService/rest/helloRest";
		try{
			
			Client client = Client.create();
			WebResource resource = client.resource(uri);
			String response = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice["+response+"]");
			String request = "GetRestWithParam";
			uri = uri.concat("/getDetail/").concat(request);
			resource = client.resource(uri);
			String response1 = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice with parameter["+response1+"]");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}

}


Step 10: You can see the output as below

Response from Rest webservice[Hello Welcome to Rest webservice]
Response from Rest webservice with parameter[Hello Welcome to Rest webservice you have given input as -GetRestWithParam]


Thank you very much for viewing this post. If you like, don't forget to share/comment.


Sunday, November 12, 2017

Read and Parse XML using JAXB and write to a flat file with fixed length format using fixedFormat4j and Java




This post will explain, how to read xml ,parse xml and write to a flat file with fixed length format.

Step 1: Create a maven project -< xmltofile>



Step2: paste the below code into pom.xml file.


  4.0.0
  xmltofile
  xmltofile
  0.0.1-SNAPSHOT
  
    src
    
      
        maven-compiler-plugin
        3.5.1
        
          1.8
          1.8
        
      
    
  
  
  
     
    com.ancientprogramming.fixedformat4j
    fixedformat4j
    1.2.2

  
  
  




Step 3: Now we need to create company.xml file under project- create one xml file add the below xml to that file.


  
        
                siva
                34
                Male
                35000
                01/07/1983
                
Present Bangalore Karnataka India
Permanent Kadapa AndharaPradesh India
Sanjay 27 Male 55000 01/07/1988
Present Bangalore Karnataka India
Permanent Hyderabad Telangana India
Madhan 34 Male 800000 01/07/1983
Present and Permanent Bangalore Karnataka India

Step 4: Create pojo classes , which need to be parsed as per above xml.

i have created 3 classes - Company.java -> List of Employee.java -> List Of Address.java


Company.java - xmlroot elment is company which is there in xml.

package xmltofile;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="company")
public class Company {
 
 private List employee = new ArrayList();

 @XmlElement(name="employee")
 public List getEmployee() {
  return employee;
 }

 public void setEmployee(List employee) {
  this.employee = employee;
 }

}



Employee.java

package xmltofile;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
public class Employee {
 
 private String name;
 private int age;
 private String gender;
 private Double salary;
 private String dob;
 List
address = new ArrayList
(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @XmlElement public List
getAddress() { return address; } public void setAddress(List
address) { this.address = address; } public String getDob() { return dob; } public void setDob(String dob) { this.dob = dob; } }


Address.java

package xmltofile;

import javax.xml.bind.annotation.XmlRootElement;

public class Address {
 private String city;
 private String type;
 private String state;
 private String country;
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }

}

Step 5: Now we need to write class using fixedformat4j API-


CompanyHeader.java

package xmltofile.fixedformat;

import com.ancientprogramming.fixedformat4j.annotation.Field;
import com.ancientprogramming.fixedformat4j.annotation.Record;

@Record
public class CompanyHeader {
 
 private String header1;
 private String header2;
 private String header3;
 
 @Field(offset = 1, length = 20)
 public String getHeader1() {
  return header1;
 }
 public void setHeader1(String header1) {
  this.header1 = header1;
 }
 @Field(offset = 30, length = 50)
 public String getHeader2() {
  return header2;
 }
 public void setHeader2(String header2) {
  this.header2 = header2;
 }
 @Field(offset = 60, length = 100)
 public String getHeader3() {
  return header3;
 }
 public void setHeader3(String header3) {
  this.header3 = header3;
 }
 
 

}



EmployeeHeader.java- This class will have - each filed -at what is the starting index and data length of the eachfiled


package xmltofile.fixedformat;

import com.ancientprogramming.fixedformat4j.annotation.Field;
import com.ancientprogramming.fixedformat4j.annotation.Record;

@Record
public class EmployeeHeader {

 private String nameHeader;
 private String ageHeader;
 private String genderHeader;
 private String salaryHeader;
 private String dobHeader;
 private String addressHeader;
 
 @Field(offset = 1, length = 50)
 public String getNameHeader() {
  return nameHeader;
 }
 public void setNameHeader(String nameHeader) {
  this.nameHeader = nameHeader;
 }
 @Field(offset = 55, length = 3)
 public String getAgeHeader() {
  return ageHeader;
 }
 public void setAgeHeader(String ageHeader) {
  this.ageHeader = ageHeader;
 }
 @Field(offset = 60, length = 10)
 public String getGenderHeader() {
  return genderHeader;
 }
 public void setGenderHeader(String genderHeader) {
  this.genderHeader = genderHeader;
 }
 @Field(offset = 75, length = 10)
 public String getSalaryHeader() {
  return salaryHeader;
 }
 public void setSalaryHeader(String salaryHeader) {
  this.salaryHeader = salaryHeader;
 }
 @Field(offset = 90, length = 15)
 public String getDobHeader() {
  return dobHeader;
 }
 public void setDobHeader(String dobHeader) {
  this.dobHeader = dobHeader;
 }
 @Field(offset = 120, length = 200)
 public String getAddressHeader() {
  return addressHeader;
 }
 public void setAddressHeader(String addressHeader) {
  this.addressHeader = addressHeader;
 }
 

}



Step 6: Now we need to write test class to Parse the employee.xml using JAXB context and create the Flat file
and write the xml data into file at specified index.

XMLReaderAndWriteToFile.java

package xmltofile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.ancientprogramming.fixedformat4j.format.FixedFormatManager;
import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl;

import xmltofile.fixedformat.CompanyHeader;
import xmltofile.fixedformat.EmployeeHeader;

public class XMLReaderAndWriteToFile {

 public static void main(String[] args) {
  BufferedWriter bw = null;
  try {
   JAXBContext context = JAXBContext.newInstance(Company.class);
   // Create Unmarshaller using JAXB context
   Unmarshaller unmarshaller = context.createUnmarshaller();
   System.out.println("xml file started to load...");
   Company company = (Company) unmarshaller.unmarshal(new File("employee.xml"));
            System.out.println("xml file loaded and parsed successfully..");
   // Write to file with fixedformat
            System.out.println("Flat file started to create...");
   bw = new BufferedWriter(new FileWriter("fixedLength.txt", true));
   System.out.println("empty flat file created...");
   getCompanyHeader(bw);
   System.out.println("company header written successfully to file.");
   getEmployeeHeader(bw);
   System.out.println("employee header written successfully to file.");
   for (Employee employee : company.getEmployee()) {
    EmployeeHeader employeeDetails = new EmployeeHeader();
    employeeDetails.setNameHeader(employee.getName());
    employeeDetails.setAgeHeader(String.valueOf(employee.getAge()));
    employeeDetails.setGenderHeader(employee.getGender());
    employeeDetails.setDobHeader(String.valueOf(employee.getDob()));
    employeeDetails.setSalaryHeader(String.valueOf(employee.getSalary()));
    String completeAddress = "  ";
    for (Address address : employee.getAddress()) {
     String addressDetails = address.getCity() + " " + address.getState() + " " + address.getCountry()
       + " - " + address.getType();
     completeAddress = completeAddress.concat(addressDetails) + "     ";
    }
    employeeDetails.setAddressHeader(completeAddress);
    FixedFormatManager manager1 = new FixedFormatManagerImpl();
    String data1 = manager1.export(employeeDetails);
    bw.write(data1);
    bw.newLine();

   }
   System.out.println("Employee details written successfully to file.");

  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   try {
    bw.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

 }

 private static void getEmployeeHeader(BufferedWriter bw) throws IOException {
  EmployeeHeader employeeHeader = new EmployeeHeader();
  employeeHeader.setNameHeader("Employee Name");
  employeeHeader.setAgeHeader("Age");
  employeeHeader.setGenderHeader("Gender");
  employeeHeader.setDobHeader("DOB");
  employeeHeader.setSalaryHeader("Salary");
  employeeHeader.setAddressHeader("Address");
  FixedFormatManager manager = new FixedFormatManagerImpl();
  String data = manager.export(employeeHeader);
  bw.write(data);
  bw.newLine();
  bw.newLine();
  bw.newLine();
 }

 private static void getCompanyHeader(BufferedWriter bw) throws IOException {
  CompanyHeader companyHeader = new CompanyHeader();
  companyHeader.setHeader1("ABC Company");
  companyHeader.setHeader2("Bangalore");
  companyHeader.setHeader3("India");
  FixedFormatManager manager = new FixedFormatManagerImpl();
  String data = manager.export(companyHeader);
  bw.write(data);
  bw.newLine();
  bw.newLine();
  bw.newLine();

 }

}




Output file will be like this.

ABC Company                  Bangalore                                                                                                                         


Employee Name                                         Age  Gender         Salary         DOB                           Address                                                                                                                                                                                                 


siva                                                         34   Male           35000.0        01/07/1983                      Bangalore Karnataka India - Present     Kadapa AndharaPradesh India - Permanent                                                                                                                       
Sanjay                                                     27   Male           55000.0        01/07/1988                      Bangalore Karnataka India - Present     Hyderabad Telangana India - Permanent                                                                                                                         
Madhan                                                   34   Male           800000.0       01/07/1983                     Bangalore Karnataka India - Present and Permanent                                                                                                                                                     



Thank you verymuch for viewing this post. If you like this don't forget to share.



Monday, November 6, 2017

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: Gradle eclipse




sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:



If you are getting this error . While running the gradle build task from eclipse.
cacert might becorrupted.. which is there inside jdk/jre/lib

to change this copy or download the latest cacert from other system or internt.

create gradle.properties file and place it inside your .gradle home, which is configured in eclipse.

systemProp.javax.net.ssl.trustStore=C:\\Users\\ca-certs


Wednesday, November 1, 2017

Getting Started with Spring Boot Application on Pivotal Cloud Foundry Platform On Windows Environment , Pivotal Webservice Console and Microservice





This Post will explain , how to Deploy Spring Boot Application on Pivotal Cloud Foundry Platform



Now a days Cloud Computing and Microservice have become very popular concept and almost all the organizations are investing and adapting it very fast. Currently there are only

few popular cloud providers in the market and Pivotal Cloud Foundry is one of them. It is a PaaS(Platform AS A Servcie) service where we can easily deploy and manage our

applications and the Cloud Foundry will take care of the rest of the cloud based offerings like scalability, high availability etc.

Today we will learn to deploy spring boot application in Pivotal cloud foundry platform called as Pivotal Web Services.


Step 1: What is Cloud Foundry?


Cloud Foundry is an open-source platform as a service (PaaS) that provides you with a choice of clouds, developer frameworks, and application services. It is open source and it is governed by the Cloud Foundry Foundation. The original Cloud Foundry was developed by VMware and currently it is managed by Pivotal

Step 2: Now we need to install Cloud Foundary on Windows

Step 3: Download Cloud Foundary Windows Installer using this link

Step 4: Unzip the file, From the place where you have saved the downloaded file.

Step 5: Double Click on the cf_installer.exe file and click on the next.. next buttons until it completes



Step 6: If you have successfully installed then you can check the CLI version or other details

Enter cf command prompt. It will give list of options which cf will accept.
c:/> cf vesion -  will give full CLI version details
  
  

Step 7: Now we need to be setup Pivotal Webservice Console (PWS)

Step 8: Provide necessary details and sign up the same.


Once sign up is completed, we can log into the Pivotal webservice console through the login screen of the pivotal web service console.

After providing login credentials successfully we will get into the cloud foundry console where we can see all the deployed applications, can monitor the applications and do many more activities. Here we need to add org and space etc...

Step 9: Once we have created org and space successfully.. now we need to deploy the spring boot application in PCF.

Step 10: Before that , Login in to PWS (Pivotal Webservice Console) using CLI


c:/> cf login -a api.run.pivotal.io

Provide username and password which is given , while registering the PWS.

same way we can use Logout
c:/> cf logout

Step 11: Now login and logout is working with out any issues.

Step 12: Now write simple spring boot application and run the same in locally.

If you want to write simple one follow the https://start.spring.io/ - which will create sample spring boot application for you and modify as per your requirment.

i have created one sample arthimetic operations spring boot application.. check in
my previous post for the same.

add the below code into bootstrap.properties under src\main\resources


server.contextPath = /
                management.security.enabled = false
         



Step 13: Once we have completed the Spring boot application and executed successfully in local environment.

Now we need to push the same into Pivotal cloud Foundary.

Step 14: login into PWS using CLI c:/> cf login -a api.run.pivotal.io

Step 15: Go to where spring boot application is saved . Then push to PCF

F:\spring_boot_example\helloworld-springboot>cf push helloworld-springboot -p target\helloworld-springboot-0.0.1-SNAPSHOT.jar










Step 16: Now we have deployed spring boot application into Pivotal Cloud Foundary Successfully.

Step 17: Open the PWS console and check the details. whether pushed application through CLI is available or not in PWS console.



Step 18: Open the below links and you can see the output as follows.

https://helloworld-springboot.cfapps.io/
https://helloworld-springboot.cfapps.io/add/20/30
https://helloworld-springboot.cfapps.io/substract/20/30
https://helloworld-springboot.cfapps.io/multiply/20/30
https://helloworld-springboot.cfapps.io/division/20/30





Step 19: This is how we will implement spring boot application and we can deploy the same in Pivotal Cloud Foundary.

Thank you very much for viewing this post. if you like this please share the same
If you face any issues.. write a comment and you will get reply the same

Tuesday, October 24, 2017

Getting started with Pivotal Gemfire On Windows





This post will explain you about , how to start pivotal Gemfire on Windows
Step 1: What is Gemfire?
In-Memory Data Grid powered by Apache Geode
Scale your data services on demand to support high-performance, real-time apps
you can check more info on Gemfire docs

Step 2: Download - pivotal-gemfire-9.1.0.tar.gz using Download Pivotal Gemfire
Step 3: Unzip the downloaded file and set the PATH in environement variables.
Step 4: check the version of the pivotal gemfire, which is available in your system gfsh version --full

Step 5: Now we need to create a locator - Go to the place which ever the location you want to create locator in command prompt.
Step 6:Start the gfsh.bat file from command prompt.
Step 7: start locator --name=test --J=-Dgemfire.http-service-port=8080 --J=-Dgemfire.http-service.bind-address=localhost or

gfsh>start locator --name=test --J=-Dgemfire.http-service-port=8080 --J=-Dgemfire.http-service-bind-address=LAPTOP-2U8NKC7I
Starting a Geode Locator in F:\softwares\CloudFoundary_apache_gemFire\pivotal-gemfire-9.1.0\bin\test...
.....
Locator in F:\softwares\CloudFoundary_apache_gemFire\pivotal-gemfire-9.1.0\bin\test on LAPTOP-2U8NKC7I[10334] as test is currently online.
Process ID: 11740
Uptime: 3 seconds
Geode Version: 9.1.0
Java Version: 1.8.0_131
Log File: F:\softwares\CloudFoundary_apache_gemFire\pivotal-gemfire-9.1.0\bin\test\test.log
JVM Arguments: -Dgemfire.enable-cluster-configuration=true -Dgemfire.load-cluster-configuration-from-dir=false -Dgemfire.http-service-port=8080 -Dgemfire.http-service-bind-address=LAPTOP-2U8NKC7I -Dgemfire.launcher.registerSignalHandlers=true -Djava.awt.headless=true -Dsun.rmi.dgc.server.gcInterval=9223372036854775806
Class-Path: F:\softwares\CloudFoundary_apache_gemFire\pivotal-gemfire-9.1.0\lib\geode-core-9.1.0.jar;F:\softwares\CloudFoundary_apache_gemFire\pivotal-gemfire-9.1.0\lib\geode-dependencies.jar

Successfully connected to: JMX Manager [host=LAPTOP-2U8NKC7I, port=1099]

Cluster configuration service is up and running.

gfsh>start pulse

Step 8: Open the start pulse
Step 9: Open the browser and enter port as 8080 -> http://laptop-2u8nkc7i:8080/pulse and provide username as admin and password as admin

Step 10 cluster details will display after successful login.


Thank you verymuch for viewing this post. If you like this post please write comment and share.




Saturday, October 14, 2017

Getting started with Spring Boot deployed on Google Cloud. Maven, Google CloudSDK installation



This post will explain you. How to deploy springboot application on google cloud

Step 1: Open Open Google Cloud


Step 2: Login into google cloud.
Step 3: Register Cloud application with free trail - Cloud Application Registration

Step 4:Create a new Project -
Provide necessary details and signup the same.
Step 5: Now you are completed signup process . Spring boot application needs to be deployed.
Step 6: Create a maven java project with name - helloworld-springboot and below code to pom.xml file.


  4.0.0

  com.java.gcloud.springboot
  helloworld-springboot
  0.0.1-SNAPSHOT
  jar

  helloworld-springboot
  Demo project for Spring Boot with google cloud

 

  
    1.8
    ${java.version} 
    ${java.version} 
    UTF-8
    1.3.1
  

  
    
      org.springframework.boot
      spring-boot-starter-web
      1.5.7.RELEASE
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        1.5.7.RELEASE
        
          
            
              repackage
            
          
        
      

      
        com.google.cloud.tools
        appengine-maven-plugin
        ${appengine.maven.plugin}
      

    
  
  
    



Step 7: Now we need to write controller class name HelloworldApplication.java
package com.java.gcloud.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloworldApplication {
  @RequestMapping("/")
  public String home() {
    return "Hello World.. Welcome to Spring Boot.. which is deployed on Google cloud appengine!";
  }

  @RequestMapping(value = "/add/{a}/{b}", method = RequestMethod.GET)
  public String add(@PathVariable("a") int a,@PathVariable("b") int b) {
    return "Addition of a["+a+"] and b ["+b+"]is   ======   " + (a+b) ;
  }
  
  @RequestMapping(value = "/substract/{a}/{b}", method = RequestMethod.GET)
  public String substract(@PathVariable("a") int a,@PathVariable("b") int b) {
    return "Substract of a["+a+"] and b ["+b+"]is   ======   " + (a-b) ;
  }
  
  @RequestMapping(value = "/multiply/{a}/{b}", method = RequestMethod.GET)
  public String multiply(@PathVariable("a") int a,@PathVariable("b") int b) {
    return "Multiply of a["+a+"] and b ["+b+"]is   ======   " + (a*b) ;
  }
  
  @RequestMapping(value = "/division/{a}/{b}", method = RequestMethod.GET)
  public String division(@PathVariable("a") int a,@PathVariable("b") int b) {
    return "Division of a["+a+"] and b ["+b+"]is   ======   " + (a/b) ;
  }

  public static void main(String[] args) {
    SpringApplication.run(HelloworldApplication.class, args);
  }
}


Step 8: Run the pom.xml file using mvn clean install
Step 9: Now we need to run the application. go to the directory , where jar created and run the
java -jar   
another way to execute the application is right click on the HelloWorldApplication Run As- JavaApplication.
Step 10: Once spring boot started successfully then go to browser and hit http://localhost:8080 .
you should get the -Hello World.. Welcome to Spring Boot.. which is deployed on Google cloud appengine!

Step 11: That is done. Now we need to deploy this application on google cloud.
Step 12: Install the Google Cloud SDK for windows version and set the PATH in environment variables.
Step 13: Go to the place where pom.xml file is there in command prompt.
gcloud config set project p7259000552 After this command
mvn appengine:deploy

Step 14: You will get Build Success message. So application has been deployed successfully.
Step 15: Verify, whether it has been deployed or not - https://.appspot.com/
Step 16: This application will have simple math operations like add,sustarct,multiply,division if you click any of this links you will get appropriate results.
https://p7259000552.appspot.com/add/20/30
   https://p7259000552.appspot.com/substract/20/30
   https://p7259000552.appspot.com/multiply/20/30
   https://p7259000552.appspot.com/division/20/30
 


Thank you verymuch for viewing this post. If you like please share and comment.

Thursday, October 12, 2017

CRUD (Create, Read,Update,Delete) operations with Spring Boot , Mysql, JPA , Hibernate with in built Tomcat server.



This post will explain you. How to work with Spring boot and Mysql CRUD(create,read,update,delete) operations
Step 1: Open eclipse and create a java project.. more details please check my previous post getting started with spring boot.

Step 2: create a new pom.xml file under project and the below code to that file.

       

    4.0.0

    org.springframework
    spring_boot_first
    0.1.0

  
        org.springframework.boot
        spring-boot-starter-parent
        1.5.1.RELEASE

 
    
     
                   org.springframework.boot
                   spring-boot-starter-web
            
     
                   org.springframework.boot
                   spring-boot-starter-tomcat
             
      
                    org.springframework.boot
                    spring-boot-starter-security
       
       
                     org.springframework.boot
                     spring-boot-starter-data-jpa
        
                             
                                     org.apache.tomcat
                                     tomcat-jdbc
                              
                        
  
  
                       mysql
                       mysql-connector-java
                
   
                         commons-dbcp
                         commons-dbcp
  
  
    
      
        1.8
    



   


Step 3:Create new model object called User.
 package springboot.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class User {
 @Id
 @Column
 private long id;
 @Column
 private String name;
 @Column
 private String gender;
 @Column
 private String country;
 @Column
 private String password;
 
 
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }


}
Step 4: Create UserService and UserServiceImpl under package springboot.service
  package springboot.service;

import java.util.List;

import springboot.model.User;

public interface UserService {

   public List getUsers();
   public List createOrUpdateUser( User user);
   public List deleteUser( User user);
   
 
}
Step 5: Add below code in UserServiceImpl.

  package springboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import springboot.dao.UserDAO;
import springboot.model.User;

@Component
public class UserServiceImpl implements UserService{

 @Autowired
 private UserDAO userDAO;
 
 @Override
 public List getUsers() {
  return userDAO.getUsers();
 }
 
 public List createOrUpdateUser( User user){
  return userDAO.createOrUpdateUser(user);
 }
   public List deleteUser( User user){
    return userDAO.deleteUser(user);
   }

 public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
 }

}



Step 6: Create UserDAO and UserDAOImpl under package springboot.dao as mentioned below
 package springboot.dao;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.repository.CrudRepository;

import springboot.model.User;

@Transactional
public interface UserDAO {
 public List getUsers();
 public List createOrUpdateUser( User user);
 public List deleteUser( User user);

}

Step 7: Add below code inside UserDAOImpl
 package springboot.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import springboot.model.User;

@Component
public class UserDAOImpl implements UserDAO{
 
 @Autowired
 private SessionFactory sessionFactory;

 public List getUsers(){
  Criteria criteria = sessionFactory.openSession().createCriteria(User.class);
  return criteria.list();
 }
 
 public List createOrUpdateUser( User user){
  Session session = sessionFactory.openSession();
  User oldUser = session.get(User.class, user.getId() );
  //System.out.println("oldUser id["+ oldUser.getId()+"]");
  if(oldUser == null){
   session.save(user);
   session.flush();
   System.out.println("Created or Update Successful");
    }
  Criteria criteria = sessionFactory.openSession().createCriteria(User.class);
  return criteria.list();
 }
 public List deleteUser( User user){
  Session session = sessionFactory.openSession();
  User oldUser = session.get(User.class, user.getId() );
  if(oldUser != null){
   session.delete(oldUser);
   session.flush();
   System.out.println("Deleted successfully");
  }
  Criteria criteria = sessionFactory.openSession().createCriteria(User.class);
  return criteria.list();
 }

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

}

Step 8: Create BeanConfig class. which will create EntityManagerFactory.
 package springboot;

import javax.persistence.EntityManagerFactory;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

 @Autowired
 private EntityManagerFactory entityManagerFactory;

 @Bean
 public SessionFactory getSessionFactory() {
     if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
         throw new NullPointerException("factory is not a hibernate factory");
     }
     return entityManagerFactory.unwrap(SessionFactory.class);
 }

 public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
  this.entityManagerFactory = entityManagerFactory;
 }

}

Step 9: Now we need to add code related to database details called application.properties
  # ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost:3306/employee?useSSL=false

# Username and password
spring.datasource.username = root
spring.datasource.password = root

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Step 10: Create a RestController class. Name it as UserController
package springboot;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import springboot.model.User;
import springboot.service.UserService;

@Controller
public class UserController {
 
 @Autowired
 private UserService userService;
 
 @RequestMapping(value = "/list", method = RequestMethod.GET)
 public ResponseEntity userDetails() {
        
  List userDetails = userService.getUsers();
  return new ResponseEntity(userDetails, HttpStatus.OK);
 }
 
 @RequestMapping(value = "/create/{id}/{name}/{password}/{gender}/{country}/", method = RequestMethod.GET)
 public ResponseEntity createOrUpdateUserDetails(@PathVariable("id") long id, @PathVariable("name") String name,
   @PathVariable("password") String password,@PathVariable("gender") String gender,@PathVariable("country") String country) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setCountry(country);
        user.setGender(gender);
        user.setPassword(password);
  List userDetails = userService.createOrUpdateUser(user);
  return new ResponseEntity(userDetails, HttpStatus.OK);
 }
 @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
 public ResponseEntity deleteUserDetails(@PathVariable("id") long id) {
        User user = new User();
        user.setId(id);
  List userDetails = userService.deleteUser(user);
  return new ResponseEntity(userDetails, HttpStatus.OK);
 }


 public void setUserService(UserService userService) {
  this.userService = userService;
 }

}

Step 11: Now we need to run this application through spring boot. So we have to write Application class.
  package springboot;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}


Step 12: Now we need to build and run this application.
Right click on the pom.xml file and Run As - clean install
Once that is done. Right click on the Application.java and Run As-> Java Application. Below details will be printed in Console
     .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.1.RELEASE)

2017-10-12 17:17:15.600  INFO 13036 --- [           main] springboot.Application                   : Starting Application on LAPTOP-2U8NKC7I with PID 13036 (F:\eclipse_workspace\spring_boot_first\target\classes started by Siva in F:\eclipse_workspace\spring_boot_first)
2017-10-12 17:17:15.626  INFO 13036 --- [           main] springboot.Application                   : No active profile set, falling back to default profiles: default
2017-10-12 17:17:16.033  INFO 13036 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@23bb8443: startup date [Thu Oct 12 17:17:15 IST 2017]; root of context hierarchy

   

Step 13: Use http://localhost:8080/list -- it will display all the records which is there inside user table.
If it askes username and password - then username will be - user and password will be check Using default security password: in console. that is your password.


Step 14: Use http://localhost:8080/create/16/sanju/sam/M/India/ - it will insert record inside DB and will display all the records from user table.

Step 15 : http://localhost:8080/delete/16 - it will delete where id is 16 and display all the results from user table.

Step 16 : Now we will learn how to create database and create a table.
Step 17: Install mysql and create database with name- employee. and create a table name called User with columns as mentioned below.
     CREATE TABLE user (
  id int(11) NOT NULL,
  name varchar(45) DEFAULT NULL,
  gender varchar(10) DEFAULT NULL,
  password varchar(45) DEFAULT NULL,
  country varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)) 
     

Step 18: If you want to use another database. simply change the application.properties respective DB details and dialect along with respective connecter in pom.xml

Thanks for viewing this post. If you like this please share and write comments.





AddToAny

Contact Form

Name

Email *

Message *