HackerRank Solution: Subarray using Kotlin

This tutorial will show you how to solve HackerRank Subarray using Kotlin.

A subarray of an n-element array is an array composed from a contiguous block of the original array’s elements. For example, if array=[1,2,3], then the subarrays are [1], [2], [3], [1,2], [2,3], and [1,2,3]. Something like [1,3] would not be a subarray as it’s not a contiguous subsection of the original array.

Please go through first how to create Kotlin project in Eclipse

Continue reading “HackerRank Solution: Subarray using Kotlin”

Creating Kotlin Project in Eclipse

In this tutorial I will show you how to create Kotlin project in Eclipse. This example shows you how to create Kotlin gradle project or Kotlin maven project.

Kotlin is statically typed programming language for modern multiplatform applications. Kotlin has 100% compatibility with Java or vice-versa.

Salient Features of Kotlin

Drastically reduces the amount of boilerplate code.

Avoids entire classes of errors such as null pointer exception.

Leverages existing libraries for the JVM, Android and the browser.

Choose any Java IDE or build from the command line.

Continue reading “Creating Kotlin Project in Eclipse”

Configure JNDI DataSource with Spring Boot

Introduction

JNDI data source is very similar to JDBC data source. The JNDI data source accesses a database connection that is pre-defined and configured in the application server and published as a JNDI resource or service. Instead of specifying a driver and database as we do with JDBC data sources, we only need to specify the JNDI resource name in our application server.

Why do we use JNDI DataSource

JNDI comes in rescue when you have to move an application between environments: development -> integration -> test -> production. If you configure each application server to use the same JNDI name, you can have different databases in each environment but you need not to change your code. You just need to drop the deployable WAR file in the new environment.

You may also like to read Spring Data JPA CRUD Example and Spring Data JPA Entity Graphs

Continue reading “Configure JNDI DataSource with Spring Boot”

Spring Boot MVC AutoComplete using jQuery

Introduction

Autocomplete allows you to easily create autocomplete and auto-suggest boxes for text input fields. When you start a search on Google, you can find the information you are looking for using search predictions. Search predictions are possible search terms you can use that are related to the terms you are typing and what other people are searching for. Spring Boot MVC autocomplete example will show you exactly the same thing here.

Autocomplete is a feature in which an application predicts the rest of a word a user is typing. In graphical user interfaces, users can typically press the tab key to accept a suggestion or the down arrow key to accept one of several.
Autocomplete speeds up human-computer interactions when it correctly predicts the word a user intends to enter after only a few characters have been typed into a text input field. The autocomplete is a normal text input enhanced by a panel of suggested options. Continue reading “Spring Boot MVC AutoComplete using jQuery”

Server Sent Events with Spring – Push Notifications

Introduction

We have seen a popular choice for sending real time data from server to client in web application is using WebSocket in our previous tutorials Messaging with STOMP over WebSockets using Spring and ActiveMQ and Spring Boot WebSocket AngularJS Gradle Example. WebSocket opens bidirectional connections between server and client. So both server and client can send messages. But sometimes we face situations, where the application needs only one way communication, i.e., sending data from server to the client and for this Spring provides a simpler solution using Server Sent Events (SSE). SSE is a technology that allows you to stream data from server to the browser  (Push Notifications) within one HTTP connection in one direction. For example, pushing stock price changes in real-time or showing progress of long-running process or real time showing of cricket or football scores on display board etc.

Browser Support

SSE are supported in most modern browsers. Only Microsoft’s IE and Edge browsers do not have a built in implementation. But there is a way out because Server-Sent Events uses common HTTP connections and can therefore be implemented with the following libraries to support IE and Edge browsers.

Continue reading “Server Sent Events with Spring – Push Notifications”

Username availability check using Servlet, AJAX and MySQL

Sometimes we need to check username availability instantly before a user presses the submit button after filling a long-sized signup form. In this case we can use AJAX with any technologies like PHP, Codeigniter, Servlet, Struts, JSF, Spring etc. for checking whether the input username is avaialable or already occupied by someone else. So if we give instant result to the user for username availability then sometimes it makes more sensible than while pressing the submit button and goes top of the signup form to rectify the username input field if input username is not available.

This tutorial shows how to check username availability using Servlet, AJAX and MySQL. So after finishing this example you will be able to apply the same logic to any Servlet based web framework or any server side technologies such as PHP, Codeigniter etc. Continue reading “Username availability check using Servlet, AJAX and MySQL”

org.hibernate.MappingException: No Dialect mapping for JDBC type

You might have seen similar kind of exception occurred during performing native queries without entity class and this kind of issue may have been occurred in different databases such as MySQL, Oracle, SQL Server, PostgreSQL etc. Depending upon the database vendor you will get error for JDBC types -4, -9, 7 or 2003 etc. It means that Hibernate is unable to implement a type mapping for database type(for example, table column type float) to java type (float) though float type is listed in the JDBC reference.

Here I will create an example how to query Oracle database without entity class using Hibernate JPA and in what situation it throws the exception “org.hibernate.MappingException: No Dialect mapping for JDBC type: -9”. In this example I will also show you how to resolve the issue.

I assume that you are using Hibernate JPA with Spring Boot application.

Continue reading “org.hibernate.MappingException: No Dialect mapping for JDBC type”

Evolving Database using Spring Boot and Liquibase

When developing web application, it is important to consider how to create schema for production and evolve the database schema going forward of a Java web application.

Liquibase is an open source library for tracking, managing and applying database changes that can be used for any database. It helps you create the schema, run them during deployment and also help you write automated tests so that your changes will work in production.

Liquibase is a tool that reads a list of database changes from a changelog file. The changelog file is often XML-based, but it could be in other formats also, such as YAML, JSON and SQL formats. Once a new change is added to the changelog file, Liquibase will read the file, detect the new change, and apply it to the database. When code changes are committed to the version control system, the changelog file containing the database “version” also gets committed along with it. Continue reading “Evolving Database using Spring Boot and Liquibase”

Spring Boot REST API Documentation with Swagger 2

This tutorial will show you how to use swagger 2 using spring boot application for creating RESTful documentation. We create and deploy REST services but we do not have an option to let others know in a single place, where REST services are exposed for consumption. We do not have option to let others know that these are the methods, request body, request params for these exposed REST services. But luckily we have got simple but powerful API called swagger through which we can document such RESTful services in a single place in order to let others know about every details (request URI, request body, request params, request method etc.) of the exposed REST services. This API is third party API because developers do not have option to generate RESTful documentation using JAX-RS (JSR-339). After completing the Spring Boot REST API Documentation with Swagger 2 example, you will understand the usage of this wonderful API and you will be able to apply the similar concept for your own projects.

Continue reading “Spring Boot REST API Documentation with Swagger 2”

Writing Junit Test on Java Thread

In this tutorial you will see how to write test case on Java thread using junit. Writing junit to test thread in Java will show an example on single threaded environment.

Prerequisites

Java 8

Eclipse

Gradle

It is advisable to structure the code in such a way so that the code could be easily tested on several distinct areas:

  1. the code that launches the thread and wait for result.
  2. the worker code that runs in the thread.
  3. the concurrency issue may occur when multiple threads are active.

Continue reading “Writing Junit Test on Java Thread”