Related Links 1. Getting started with Apache Tomcat application server by deploying a simple JEE Application. … Read more ›...
Related Links 1. Getting started with Apache Tomcat application server by deploying a simple JEE Application. … Read more ›...
Related Links 1. Java Web App with Tomcat & Maven beginner video tutorial. 2. Getting started with Apache Tomcat application server by deploying a simple JEE Application. … Read more ›...
Industry strength code needs to statically & dynamically capture code quality. Also, more and more organizations are using “production quality” home assignments to shortlist candidates for job interviews. So, it really pays to set up code quality tools like SonarQube on your home development environment to get feedback on your code quality with the view to learm &
…
Related Links 1. JUnit with Mockito step by step. … Read more ›...
All Java developers not only must know “SQL”, but also a database is required for “JDBC tutorials”, “Hibernate Tutorials”, and so on. Any decent Java application will require a database. So, your self-taught Java projects must perform CRUD (i.e. Create Read, Update and Delete) operations on the database.
…
In Maven, there are build and the reporting plugins. Build plugins will be executed during the build, and they should be configured in the “build” element.
What is a “Plugin Architecture”?
In this architecture, a core application defines an interface,
…
Performance testing is an important process of any software development. You can bring this up yourself to many open-ended questions like — Tell me about yourself? What are some of your recent accomplishments that you are proud of? Setting up a performance/load test script is always the first step in...
This extends the Apache HTTPClient Tutorial to invoke RESTFul service with a POST request.
The pom.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<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.mytutorial</groupId> <artifactId>httpRestClient</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>httpRestClient</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.1</version> </dependency> </dependencies> </project> |
The client using HttpClient & JAXBContext
The “JAXBContext” is from the Java 6 onwards. The sample code includes
1) Exception handling.
2) “BasicCredentialsProvider”
…
Apache HttpClient to invoke a RESTful Web Service. This is a GET request, and in the next post will extend this to use an HTTP POST with basic authentication.
Step 1: Create a Maven project say “httpRestClient”. Just press enter for all the prompts.
…
Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. This tutorial shows how to read a xlsx file using Apache POI.
Difference between HSSF and XSSF?
HSSF is the POI Project’s pure Java implementation of the Excel ’97(-2007) file format.
…
This extends the tutorial Apache POI reading an xlsx file tutorial.
Step 1: The “WriteToExcelFile” Java code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.mytutorial; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteToExcelFile { public static void main(String[] args) throws IOException { List<Employee> employees = new ArrayList<Employee>(); employees.add(new Employee("P-123", "John", 24)); employees.add(new Employee("C-456", "Peter", 36)); // create work book and sheets XSSFWorkbook workBook = new XSSFWorkbook(); XSSFSheet sheet = workBook.createSheet("simple sheet"); // create rows and columns Row rowHeader = sheet.createRow(0); rowHeader.createCell(0).setCellValue("Employee NO"); rowHeader.createCell(1).setCellValue("Employe Name"); rowHeader.createCell(2).setCellValue("Employee Age"); int rowNum = 1; // header is already created for (Employee emp : employees) { Row rowDetail = sheet.createRow(rowNum); rowDetail.createCell(0).setCellValue(emp.getId()); rowDetail.createCell(1).setCellValue(emp.getName()); rowDetail.createCell(2).setCellValue(emp.getAge()); ++rowNum; } FileOutputStream fos = null; try { fos = new FileOutputStream(new File("C:\\temp\\employee-out.xlsx")); workBook.write(fos); } finally { fos.close(); } } // to store data static class Employee { private String id; private String name; private int age; public Employee(String id, String name, int age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]"; } } } |
The xlsx file “employee-out.xlsx” will be created without any formatting.
Step 2: Adding styles
1) The columns are auto sized.
…
This extends the previous tutorial of Apache POI writing to an excel file by using a blank template employee-blank.xlsx file to be filled with data. The blank xlsx brings in the style and labels.
Step 1: employee-template.xlsx

Step 2: writing to xlsx file using poi
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
package com.mytutorial; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteToExcelFile { public static void main(String[] args) throws IOException { List<Employee> employees = new ArrayList<Employee>(); employees.add(new Employee("P-123", "John", 24)); employees.add(new Employee("C-456", "Peter", 36)); InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream("com/mytutorial/employee-blank.xlsx"); XSSFWorkbook workBook = new XSSFWorkbook(is); Sheet sheet = workBook.getSheet("Employees"); //Font Font defaultFont = workBook.createFont(); defaultFont.setFontHeightInPoints((short) 12); //Style XSSFCellStyle style = workBook.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN); style.setWrapText(true); style.setFont(defaultFont); //cell values int rowNum = 1; // header is already created for (Employee emp : employees) { Row rowDetail = sheet.createRow(rowNum); Cell col1 = rowDetail.createCell(0); col1.setCellValue(emp.getId()); col1.setCellStyle(style); Cell col2 = rowDetail.createCell(1); col2.setCellValue(emp.getName()); col2.setCellStyle(style); Cell col3 = rowDetail.createCell(2); col3.setCellValue(emp.getAge()); col3.setCellStyle(style); ++rowNum; } FileOutputStream fos = null; try { fos = new FileOutputStream(new File("C:\\temp\\employee-out.xlsx")); workBook.write(fos); } finally { fos.close(); } } // to store data static class Employee { private String id; private String name; private int age; public Employee(String id, String name, int age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]"; } } } |
Step 3: employee-out.xlsx

…
Triggers give you control just before data is changed and just after the data is changed. This allows for: 1) Auditing tables for changes like update, insert, and delete. 2) Data validation and column formatting before and after inserts into database. Q. … Read more ›...