Skip to main content


JDBC Driver Types

JDBC API in Java programming language provides a standard, universal way to connect to databases. It is the responsibility of different DB vendors to provide the implementation of the interfaces in JDBC API and that implementation by DB vendors is provided as JDBC drivers. Table of contents Types of JDBC drivers Type 1 JDBC Driver Type 2 JDBC Driver Type 3 JDBC Driver Type 4 JDBC Driver Types of JDBC drivers Based on these different implementations JDBC drivers are categorized into four types. Type 1 driver: JDBC-ODBC bridge JDBC driver Type 2 driver: Written partly in Java and partly in native code Type 3 driver: Pure Java client and middleware server translating client request to data source. Type 4 driver: Written completely in Java. Type 1 JDBC Driver Type 1 JDBC driver implements the JDBC API as a mapping to another data access API, such as ODBC (Open Database Connectivity). The JDBC-ODBC Bridge driver is an example of type 1 JDBC

JDBC Database Connection Steps

In this post we’ll go through the steps to connect your Java application to the Database using JDBC. Steps that are to be followed are as given below- Register the appropriate driver for the DB Create connection object by passing Database URL, DB user and DB password. Create statement Execute query Process ResultSet (If you are getting records from DB) Close connection Register driver class First thing is to register JDBC driver for the DB you are connecting to. Once the driver is registered it is loaded into the memory. You can register a driver in one of two ways- By using Class.forName() By using Driver.registerDriver() Using Class.forName() You have to pass the driver class name as a string with the method to dynamically load the driver's class file into memory. General form is as follows- Class.forName(“JDBC Driver Class”); Registering Oracle driver. Requires ojdbc8.jar or higher in classpath. Class.forName(“oracle.jdbc.d

Java Reflection – Arrays

Since array in Java is also a class so many of the methods in java.lang.Class can be used with the array too. Apart from that in Java Reflection API there is a class java.lang.reflect.Array that has methods for creating new arrays, getting and setting array elements at run time. Table of contents Identifying array type using Java Reflection Creating new array using Java Reflection Get/set elements of array using Java Reflection Identifying array type using Java Reflection To identify if any of the class field is of array type you can use Class.isArray() method. Following class is used for example purpose with an array field. public class Message { String msg; private int[] codeArray; public void displayMessage(){ System.out.println(msg); } } import java.lang.reflect.Field; public class ReflectionDemo { public static void main(String[] args) { try { // get instance of Class Class<?> cls = Class.forName("com.knpcode.programs.Mess

Invoke Getters and Setters Using Java Reflection

In this post we’ll see how to invoke getters and setters using reflection in Java. For calling get() and set() methods of a class there are two ways in Java. Using PropertyDescriptor class . Search for get and set methods of the class and call it . Using PropertyDescriptor class In PropertyDescriptor class there are constructors where you can pass the property for which you want to create descriptor and then you can call- getReadMethod() - To call the getter to read the property value. getWriteMethod() - To call the setter to write the property value. Here is an example to show how to use PropertyDescriptor class to invoke getters and setters. There is a User class with three fields and we’ll call these getters and setters using PropertyDescriptor. public class User { private String name; private int age; boolean activeFlag; public String getName() { return name; } public void setName(String name) { = name; } public int g

Java Reflection – Class Methods

Using Java Reflection API you can get information about the methods of the class and even invoke methods at run time. In Java Reflection API there is a class java.lang.reflect.Method that has methods for accessing field's type, field’s modifier and setting and getting values of a field. Table of contents Getting Method instance Getting information about class methods – Java example Invoking class method using Java Reflection Invoking private method using Java Reflection Getting Method instance First thing is to get the instance of Method class for that you will have to use methods of the java.lang.Class as that class is the entry point for all reflection operations. getMethod(String name, Class<?>... parameterTypes) - This method returns a Method object for the specified public member method name of the class or interface getMethods() - Returns an array containing Method objects reflecting all the public methods of the class or interface represented by

Serialization in Java With Examples

Serialization in Java is the process of converting state of the object into a byte stream and the reverse process of deserialization converts that byte stream (serialized form of object) back into a copy of the object. Once the object is converted into byte stream you can save it. Because of this ability to persist an object, provided by serialization in Java, objects we create could exist beyond the lifetime of the JVM. Following image shows the process of serialization and deserialization in Java and what can you do with serialized object. Table of contents What is required for serialization ObjectOutputStream and ObjectInputStream classes Java object serialization example Points about Serialization Implementing writeObject() and readObject() methods Using writeObject() and readObject() to stop serialization What is required for serialization A Java object can be serialized if its class or any of its superclasses implements either the

Java Externalizable Interface Example

When your class implements Serializable interface, object serialization is automatic where you just need to call writeObject() and readObject() methods for serialization and deserialization . If you want to customize the serialization process with you having more control then you can use Externalizable interface in Java. Table of contents Externalizable interface in Java Externalizable Java example Serialization order in Java Externalizable interface in Java interface extends Serializable interface and adds two methods of its own- writeExternal(ObjectOutput out) - To write object into a stream by calling writexxx methods for primitive types and writeObject method for objects. This flexibility to call write() methods for individual fields gives you control over the serialization process. readExternal(ObjectInput in) - To read object from stream by calling readxxx methods for primitive types and readObject method for objects. Any