Skip to main content

Posts

Showing posts from July, 2022

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.io.Se

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 java.io.Externalizable 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

Java Reflection – Class Constructors

Using Java Reflection you can get information about the constructors of the class and create a new class instance too. In Java Reflection API there is a class java.lang.reflect.Constructor that has methods for finding constructors, retrieving constructor modifiers and creating new instance. Table of contents Getting Constructor instance Getting information about class constructors – Java example Get new class instance using Java Reflection Get new class instance using private constructor Getting Constructor instance First thing is to get the instance of java.lang.reflect.Constructor 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. There are 5 methods for getting Constructor instance- getConstructor(Class<?>... parameterTypes) - This method returns a Constructor object where the passed parameter types match the parameters of the constructor in the class. getConstr

JDBC DriverManager Class

java.sql.DriverManager class in JDBC API in Java is a utility class to manage a set of JDBC drivers. Before connection to DB and start any DB operation you will have to register the driver for that specific DB with the DriverManager class. Java DriverManager class methods Main task of the DriverManager class is to register JDBC driver and to establish a connection with the DB for these tasks following methods are provided in the DriverManager class- registerDriver(Driver driver) - Registers the given driver with the DriverManager. registerDriver(Driver driver, DriverAction da) - Registers the given driver with the DriverManager. Using DriverAction you can specify an implementation that has to be called when deregisterDriver() method of DriverManager is called. deregisterDriver(Driver driver) - Removes the specified driver from the DriverManager's list of registered drivers. getDriver(String url) -Attempts to locate a driver that understands the given URL.

JDBC Driver Interface

java.sql.Driver interface in JDBC API is one interface that every Driver class has to implement compulsorily. Each driver should supply a class that implements the Driver interface. When you load/register a driver by any of the following ways, Driver class implementation is loaded. After the class is loaded it creates an instance of itself and registers it with the DriverManager . Class.forName(“JDBC Driver Class”); DriverManager.registerDriver(Driver class instance); DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to connect to the target URL. Methods in java.sql.Driver Some of the main methods defined in this interface are as follows- connect(String url, Properties info) - Attempts to make a database connection to the given URL. The Properties argument can be used to pass arbitrary string tag/value pairs as connection arguments. getMajorVersion() - Retrieves the driver

JDBC Connection Interface

Using Java Connection interface you can create a session with a specific database. SQL statements are executed and results are returned within the context of a connection. Connection interface in JDBC API resides in java.sql package. How to get Connection instance You can create Connection object using DriverManager.getConnection() method. Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1521:emp", "dbuser", "dbpwd"); Once you have a Connection object you can- Get a Statement object. Using Connection object you can also get metadata about the DB it is connecting to like DB version, driver info, tables in DB and so on. Manage transaction by using commit() and rollback() methods of Connection object and also set transaction isolation level. Methods in Connection interface We’ll go through some of the commonly used methods of the Connection interface categorized by functionality. For getting Statement in