Executing sql statements as a batch through hibernate

They are two ways to execute sql statements as batch in hibernate, which are 1) Work 2) ReturningWork This approach is useful when we want to perform batch operations directly on the database instead of indirectly through hibernate. First we will implement the Work interface as shown below JdbcWork class JdbcWork implements Work { private…… Continue reading Executing sql statements as a batch through hibernate

Mocking Static class methods

In Mockito we cannot mock static class methods, so we can use PowerMock which extends the Mockito features and provides its own features. One of which is mocking static class methods. Below code will give an example how to mock static class methods Calculator package package1; public class Calculator { public static int add(int value1,…… Continue reading Mocking Static class methods

Getting access to all printers configured in machine

This post explains how to programmatically get access to all printers configured in a machine. Java provides lookup facitlity through PrintServiceLookup class. This class has static methods through which we can get a reference to all the printers. We use the lookupPrintServices method in the PrintServiceLookup class. The api definition is as shown below public…… Continue reading Getting access to all printers configured in machine

Executing scripts written in a file using java

This post explain how to execute javascript code stored in a file. We create an instance of FileReader representing the physical file and pass it to eval method of an instance of ScriptEngine. Please refer to previous posts to know more about ScriptEngine and ScriptEngineManager The javascript code in the file that has to be…… Continue reading Executing scripts written in a file using java

Executing scripts using java

This post explain how to execute code written in scripting language like php or javascript etc in java. The scripts are executed by calling eval on an instance of ScriptEngine interface. An instance of ScriptEngine is used to represent scripting language specific engines. For example in case of JavaScript, Mozilla Rhino provided out of the…… Continue reading Executing scripts using java

Getting access to default printer

This post explains how to programmatically get access to a printer configured as default printer in a machine. Java provides PrintServiceLookup class, which provides static methods through which we can get a reference to the printer. Each printer installed in a machine is represented by an instance of PrintService interface. The PrintService interface provides methods…… Continue reading Getting access to default printer

Writing json data using Jackson JsonGenerator

The below post explains how to write json data to file using JsonGenerator. JsonGenerator writes the data to a file in streaming way, helping the developer using the api to avoid creating an object representation of the data. We can get an instance of JsonGenerator with the help of an instance of JsonFactory as shown…… Continue reading Writing json data using Jackson JsonGenerator

PushBackReader Demo

This post explains PushBackReader class in java. This reader allows us to put the data read from the input stream back into the stream and reread it again. The below explains how to use it Main Class package IO; import java.io.PushbackReader; import java.io.StringReader; public class PushBackReaderDemo { public static void main(String[] args) throws Exception {…… Continue reading PushBackReader Demo

Intercepting hibernate CRUD operations

This post explains how to intercept hibernate create, read, update and delete operations, so that we can add our own logic that has to be performaned before CRUD operations. The different use case where we need to intercept these hibernate operations are as follows 1) Every entity in the applications has common fields for example…… Continue reading Intercepting hibernate CRUD operations