devxlogo

Java

Handling BindException in Java

Port BindExceptions can occur if a port is already occupied by some other process and you try to use it again. Below is a simple example that demonstrates this and

How to Pass Parameters in @Query

Passing parameters to an SQL query written via @Query can be done via @Param or via positional parameters: // via @Param@Query(value = “SELECT p FROM Product p WHERE p.department=:department”)List fetchByDepartment(@Param(“department”)

Inspect Class Annotations

Class annotations can be inspected via reflection as follows: Class clazz = Foo.class;Annotation[] clazzAnnotations = clazz.getAnnotations();

Transform a String into IntStream

Returning a stream of int zero-extending the char values from a String can be done via chars(): String str = “hello world”;IntStream chars = str.chars();

Performing File Compression in Java

Often, file sizes are too big to back them up or share them with someone. You can create a compressed version that will save you disk space, transfer time, etc.

Set Up a Database Dialect in Spring Boot

For setting database dialect in Spring Boot, rely on spring.jpa.properties.hibernate.dialect?property in application.properties. For example, setting the MySQL5Dialect dialect can be done as below: spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Converting a Code-Point into a String

To convert a code-point (Unicode surrogate pair) into a string, rely on the following code: String str = “hello world”;int cp = str.codePointAt(some_position_in_str);String ch = String.valueOf(Character.toChars(cp));

Formatting Global Dates in Java

Dates are an important part of all record keeping activities. Due to globalization, it is important to understand how each country represents the date. Some countries start the date with