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

Tags:

by , on October 21st, 2018

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

Tags:

by , on October 21st, 2018

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

Tags:

by , on April 20th, 2018

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 More

Tags: Tags:

by , on August 31st, 2017

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 More

Tags: Tags:

by , on August 31st, 2017

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 More

Tags: Tags:

by , on August 31st, 2017

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

Tags:

by , on August 30th, 2017

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 More

Tags: Tags:

by , on August 30th, 2017

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 More

Tags: Tags:

by , on August 30th, 2017

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 More

Tags: Tags:

by , on August 29th, 2017