
Posted In: Java Core
How to print java object without getting object name like SomeType@
There are three possible ways 1. Override toString method in your class/pojo @Override public String toString() 2. Use org.apache.commons.lang.builder.ToStringBuilder 3, Use Arrays.toString to print values from string array
Read More
Posted In: Java Core, String
How to replace string inside curly brackets
Three possible ways 1. Use replace function. In this case do not escape curly brackets 2. Use replaceAll function. In this case escape curly brackets because replaceAll uses regular expression 3. Use java.text.MessageFormat class if string to replace is like {0}, {1}, {2} This code snippets were useful while testing Spring Rest URLs with a […]
Read More
Posted In: DB2, Java Core, JDBC
DB2: Java Inserting data using stored procedure
Accesing and executing DB2 stored procedure using Java JDBC Inserting data into sample COUNTRY table: Table Schema: COUNTRY_CODE VARCHAR(20) COUNTRY_NAME VARCHAR(30) Crating a db2 stored procedure Java Code
Read More
Posted In: Java Core, JSON
Converting JSON to CSV in Java using Arkni – json-to-csv
Download org.jsontocsv.parser.JSONFlattener from Arkni – json-to-csv package com.example.csv; import java.util.List; import java.util.Map; import org.jsontocsv.parser.JSONFlattener; import org.jsontocsv.writer.CSVWriter; import org.junit.Test; public class JsonToCsv2 { @Test public void jsontocsv() throws Exception { System.out.println(“jsontocsv started”); try { List flatJson = JSONFlattener .parseJson(jsonString()); CSVWriter.writeToFile(CSVWriter.getCSV(flatJson, “,”), “E:/files/outcsv1.csv”); } catch (Exception e) { e.printStackTrace(); } } private static String jsonString() { return […]
Read MoreTags: CSV

Posted In: Java Core, JSON
Converting JSON to CSV in Java using org.json.CDL
Example first reads file and the creates JSONArray. After that it again creates CSV string and writes to a CSV file. package com.example.csv; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import org.apache.commons.io.FileUtils; import org.json.CDL; import org.json.JSONArray; import org.junit.Test; public class JsonToCsv { private StringBuilder sb = new StringBuilder(); @Test public void jsontocsv() throws Exception […]
Read MoreTags: CSV

Posted In: Java Core
How to read and parse CSV file using Scanner
Example will not work with elements with comma inside quotes package com.example.csv; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import org.junit.Test; public class CvsScanner { @Test public void parser1() throws Exception { System.out.println(“\nparser1 started”); String filepath= “E:/files/cm24AUG2017bhav.csv”; Scanner scanner = null; try { File file = new File(filepath); scanner = new Scanner(file); scanner.useDelimiter(“,”); while (scanner.hasNext()) { […]
Read MoreTags: CSV

Posted In: Java Core, Security
How to generate a random alpha-numeric string
Creating a random string with A-Z and 0-9 Techniques used 1. Apache Commons RandomStringUtils 2. SecureRandom and com.google.common.io.BaseEncoding 3. UUID.randomUUID() 4. https://www.random.org/strings/ In all these methods if you are using these strings for security and do not to take any chance with uniqueness then I suggest store it in DB with unique constraint or add […]
Read More
Posted In: Java Core, String
How to parse a mathematical expression given as a string – using ScriptEngine
Evaluating a math expression given in string form package com.example.mathexpression; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import org.junit.Test; public class MathExpression2 { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); String result = “5+4*(7-15)”; System.out.println(engine.eval(result)); } }
Read MoreTags: String mathematical expression

Posted In: Java Core, String
How to parse a mathematical expression given as a string – using Bean Shell
Evaluating a math expression given in string form Maven beanshell beanshell import package com.example.mathexpression; import org.junit.Test; import bsh.Interpreter; public class MathExpression { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); Interpreter interpreter = new Interpreter(); interpreter.eval(“result = 5+4*(7-15)”); System.out.println(interpreter.get(“result”)); } }
Read MoreTags: String mathematical expression

Posted In: Java Core, String
How to read and parse CSV file using Guava Splitter
Splitting a comma-separated string but ignoring commas in quotes Maven guava Guava Splitter Works only without commas in quotes data Works with commas in quotes data
Read MoreTags: CSV
- Apache (13)
- Build Tools (2)
- Gradle (2)
- Caching (1)
- cpanel (1)
- cURL (1)
- Database (7)
- Hibernate (5)
- Java Core (38)
- Java Script (15)
- Bootstrap (1)
- File Upload (7)
- jQuery (3)
- React (3)
- JEE (13)
- JSON (41)
- GSON (13)
- Jackson 1X (1)
- Jackson 2X (12)
- jsoniter (1)
- Logging (2)
- Apache Commons Logging (1)
- Apache Log4J (1)
- Logback (1)
- SLF4J (1)
- MongoDB (1)
- OS (1)
- Linux (1)
- Security (5)
- Server (4)
- Tomcat (4)
- Service (2)
- Micro (2)
- Spring (46)
- Pattern (2)
- Spring Boot (20)
- Spring Data (4)
- Spring MVC (8)
- Spring REST (13)
- Spring Security (7)
- Testing (11)
- XML (5)
- JDOM XML Parser (1)