mkyong.com

Java Events 2019

Some Java Conferences and Events 2019. 1. JAX London (October 7–10, 2019) JAX London is a four-day conference for cutting edge software engineers and enterprise-level professionals. JAX brings together the world’s leading innovators in the fields of JAVA, microservices, continuous delivery and DevOps. Date: October 7–10, 2019 Location: Business Design Centre, London Discount(15%): mky_jxl19#15 Twitter: Link 2. I Code Java (July 24-26, 2019) Our

mkyong.com

ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

This is caused by the requested SID doesn’t exist in {ORACLE_HOME}/network/admin/tnsnames.ora P.S Tested with Oracle database 19c with ojdbc8.jar 1. JDBC try (Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe", "system", "password")) { //... } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } Output: SQL State: 66000 Listener refused the connection with the following error: ORA-12505, TNS:listener does not

mkyong.com

How to Uninstall Oracle Database 19c on Windows

To uninstall Oracle database on Windows, we have to delete the all the related Ora* registry keys, files and folders manually. P.S Tested with Oracle database 19c on Windows 10 Note I can’t find any uninstaller to uninstall the Oracle database automatically? 1. Stop Oracle* services Press Windows key, type services, stop all Oracle related services. 2. Delete Oracle* registry keys Press Windows key, type regedit, delete

mkyong.com

Java – How to change date format in a String

If Java 8, DateTimeFormatter, else SimpleDateFormat to change the date format in a String. 1. DateTimeFormatter (Java 8) Convert the String to LocalDateTime and change the date format with DateTimeFormatter DateFormatExample1.java package com.mkyong; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateFormatExample1 { // date format 1 private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); // date format 2 private static final DateTimeFormatter dateFormatterNew

mkyong.com

Java – Add new line in String

Different operating system has a different new line or line separator string: UNIX, Linux or Mac OSX = \n Windows = \r\n NewLineExample.java package com.mkyong; public class NewLineExample { public static void main(String[] args) { String original = "Hello World Java"; System.out.println(original); // add new line String originalNewLine = "Hello\nWorld\nJava"; System.out.println(originalNewLine); } } Output Hello World Java Hello World Java We can also use the Java 1.

mkyong.com

FastJson – Convert Java objects to / from JSON

FastJson provides easily APIs to convert Java objects to / from JSON JSON.toJSONString – Java objects to JSON JSON.parseObject – JSON to Java objects JSON.parseArray – JSON array to List of Java objects Note You may have interest to read this How to parse JSON with Jackson Overall, the FastJson is really simple and easy to convert JSON to / from objects, however, it lack of direct File support,

mkyong.com

Java 8 – Should we close the Stream after use?

Only Streams whose source are an IO channel like Files.lines(Path, Charset) need to be closed. Read this Stream JavaDocs Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by

mkyong.com

Java – How to save a String to a File

In Java, there are many ways to write a String to a File. 1. Java 11 – Files.writeString Finally, a new method added in java.nio to save a String into a File easily. StringToFileJava11.java package com.mkyong; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class StringToFileJava11 { public static void

mkyong.com

Jackson 2 – Convert Java Object to / from JSON

In this tutorial, we will show you how to use Jackson 2.x to convert Java objects to / from a JSON. 1. Basic 1.1 Convert a Staff object to from JSON. writeValue(...) – Java Objects to JSON ObjectMapper mapper = new ObjectMapper(); // Java object to JSON file mapper.writeValue(new File("c:\\test\\staff.json"), new Staff()); // Java object to JSON string String jsonString = mapper.writeValueAsString(object); readValue(...) – JSON

mkyong.com

Java password generator example

A Java example to generate a strong, secure random password containing [[email protected]#$%&*()_+-=[]?] characters, with a length of 15. PasswordGenerator.java package com.mkyong; import java.security.SecureRandom; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class PasswordGenerator { private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase(); private static final String NUMBER = "0123456789"

mkyong.com

Java – How to generate a random String

Few Java examples to show you how to generate a random alphanumeric String, with a fixed length. 1. Random [a-ZA-Z0-9] 1.1 Generate a random alphanumeric String [a-ZA-Z0-9], with a length of 8. RandomExample.java package com.mkyong; import java.security.SecureRandom; public class RandomExample { private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase(); private static final String NUMBER = "0123456789"; private static final

mkyong.com

Java – Convert Array to ArrayList

In Java, we can use new ArrayList<>(Arrays.asList(array)) to convert an Array into an ArrayList ArrayExample1.java package com.mkyong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayExample1 { public static void main(String[] args) { String[] str = {"A", "B", "C"}; List<String> list = new ArrayList<>(Arrays.asList(str)); list.add("D"); list.forEach(x -> System.out.