Skip to main content


Showing posts from September, 2020

JDBC ResultSet Interface

java.sql.ResultSet interface represents the data you get by executing a database query. Once you get the ResultSet you can iterate it to go over the records. For facilitating iteration, ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. There is a next() method in ResultSet that moves the cursor to the next row, returning false when there are no more rows in the ResultSet object. Table of contents Creating and iterating a ResultSet ResultSet Java example ResultSet Types in Java ResultSet Concurrency Modes ResultSet Holdability Scrollable ResultSet Java example Methods in ResultSet for cursor movement Updater methods in ResultSet Updatable ResultSet Java example Creating and iterating a ResultSet You can obtain an object of ResultSet from a Statement object, it may be a plain Statement, PreparedStatement or a CallableStatement . // From Statement ResultSet rs =

JDBC CallableStatement Interface

In JDBC API there is a Statement interface that provides functionality to execute static SQL statements. Then there is PreparedStatement interface that extends Statement and adds functionality to create parameterized SQL statements. PreparedStatement is extended by CallableStatement in JDBC API and it provides functionality to execute SQL stored procedures. How to get CallableStatement Object You can get a CallableStatement instance by calling the prepareCall() method of the Connection interface. CallableStatement callableStatement = connection.prepareCall(“{call PROCEDURE_NAME(?, ?, ?)}”); Here ‘?’ is a place holder used to register IN, OUT and INOUT parameters. If you want to call a function using Callable statement- CallableStatement callableStatement = connection.prepareCall(“? = {call PROCEDURE_NAME(?, ?, ?)}”); Methods in CallableStatement CallableStatement inherits execute methods from its parent class- execute() - Executes any SQL statement. This method retu

JDBC PreparedStatement Interface

Using Statement in JDBC you can execute SQL query on the connected DB but there is one limitation with using Statement, it doesn’t give you a chance to parameterize the SQL query so that the same query can be used multiple times by passing different parameters. There is a sub-interface of Statement, the PreparedStatement in JDBC that provides the option to write a parameterized query that can be executed multiple times. Apart from giving you an option to write dynamic queries there is one more advantage of using JDBC PreparedStatement. In PreparedStatement you pass the SQL statement when it's instance is created so the SQL statement is pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times. Since the query is already compiled when execute is called so performance of PreparedStatement is better. How to get PreparedStatement Object You can get a PreparedStatement object by calling prepareSt

JDBC Statement Interface

java.sql.Statement is used for executing a static SQL statement and returning the results it produces. Statement interface has two subinterfaces- PreparedStatement - Stores a precompiled SQL statement which can then be used to execute this statement multiple times by passing different values for the parameters. Read more about PreparedStatement in this post- JDBC PreparedStatement Interface CallableStatement - Used to execute SQL stored procedures. Read more about CallableStatement in this post- JDBC CallableStatement Interface Methods in Statement interface Some of the frequently used methods of the Statement interface are detailed here- execute(String sql) - Executes the given SQL statement, which may return multiple results. This method returns a boolean; true if the first result is a ResultSet object; false if it is an update count or there are no results. executeQuery(String sql) - Executes the given SQL statement returning a ResultSet object. Suited for runn

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

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 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 BigDecimal Class With Examples

If you are doing some calculation in your Java application where precise value is required such as in financial operations you should not be using float or double primitive type. Same way if you want to store a very large number, for example if you are calculating factorial then again long or double may fall short. What you should use instead in such scenarios is the Java BigDecimal class which provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. Since it is a class so its object can store a very large value limited only by the computer memory so you can store vary large numbers in BigDecimal class objects. Table of contents Java BigDecimal Constructors Rounding modes in BigDecimal Scaling methods in Java BigDecimal BigDecimal method examples Comparing two BigDecimals BigDecimal class in Java also gives its user complete control over rounding behavior which is another common requirement when doing financial calcu