JDBC FAQ
Explain Basic Steps in writing a Java program using JDBC?
JDBC makes the interaction with RDBMS simple and intuitive. When a Java application needs to access database :
- Load the RDBMS specific JDBC driver because this driver actually communicates with the database (Incase of JDBC 4.0 this is automatically loaded).
- Open the connection to database which is then used to send SQL statements and get results back.
- Create JDBC Statement object. This object contains SQL query.
- Execute statement which returns resultset(s). ResultSet contains the tuples of database table as a result of SQL query.
- Process the result set.
- Close the connection.
—————
What are the main components of JDBC ?
The life cycle of a servlet consists of the following phases:
- DriverManager: Manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication subprotocol. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.
- Driver: The database communications link, handling all communication with the database. Normally, once the driver is loaded, the developer need not call it explicitly.
- Connection : Interface with all methods for contacting a database.The connection object represents communication context, i.e., all communication with database is through connection object only.
- Statement : Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
- ResultSet: The ResultSet represents set of rows retrieved due to query execution.
—————
What is the difference between a Statement and a PreparedStatement?
Statement | Preparedstatement |
A standard Statement is used to create a Java representation of a literal SQL statement and execute it on the database. |
A PreparedStatement is a precompiled statement. This means that when the PreparedStatement is executed, the RDBMS can just run the PreparedStatement SQL statement without having to compile it first.
|
Statement has to verify its metadata against the database every time. | While a prepared statement has to verify its metadata against the database only once. |
If you want to execute the SQL statement once go for STATEMENT | If you want to execute a single SQL statement multiple number of times, then go for PREPAREDSTATEMENT. PreparedStatement objects can be reused with passing different values to the queries |
—————
What are callable statements ?
Callable statements are used from JDBC application to invoke stored procedures and functions.
—————
How to call a stored procedure from JDBC ?
PL/SQL stored procedures are called from within JDBC programs by means of the prepareCall() method of the Connection object created. A call to this method takes variable bind parameters as input parameters as well as output variables and creates an object instance of the CallableStatement class.
The following line of code illustrates this:
CallableStatement stproc_stmt = conn.prepareCall("{call procname(?,?,?)}");
Here conn is an instance of the Connection class.
—————
What are types of JDBC drivers?
There are four types of drivers defined by JDBC as follows:
- Type 1: JDBC/ODBC—These require an ODBC (Open Database Connectivity) driver for the database to be installed. This type of driver works by translating the submitted queries into equivalent ODBC queries and forwards them via native API calls directly to the ODBC driver. It provides no host redirection capability.
- Type2: Native API (partly-Java driver)—This type of driver uses a vendor-specific driver or database API to interact with the database. An example of such an API is Oracle OCI (Oracle Call Interface). It also provides no host redirection.
- Type 3: Open Protocol-Net—This is not vendor specific and works by forwarding database requests to a remote database source using a net server component. How the net server component accesses the database is transparent to the client. The client driver communicates with the net server using a database-independent protocol and the net server translates this protocol into database calls. This type of driver can access any database.
- Type 4: Proprietary Protocol-Net(pure Java driver)—This has a same configuration as a type 3 driver but uses a wire protocol specific to a particular vendor and hence can access only that vendor's database. Again this is all transparent to the client.
Note: Type 4 JDBC driver is most preferred kind of approach in JDBC.
—————
What is a DataSource?
A DataSource object is the representation of a data source in the Java programming language. In basic terms,
- A DataSource is a facility for storing data.
- DataSource can be referenced by JNDI.
- Data Source may point to RDBMS, file System , any DBMS etc..
—————
What is connection pooling? what is the main advantage of using connection pooling?
A connection pool is a mechanism to reuse connections created. Connection pooling can increase performance dramatically by reusing connections rather than creating a new physical connection each time a connection is requested.
—————
What will Class.forName do while loading drivers?
These are the steps happing inside
Step 1.
Class.forName("com.mysql.jdbc.Driver") load the Driver.
Step 2.
In the com.mysql.jdbc.Driver class there is static bock . That will execute because of static bock.
static
{
try
{
DriverManager.registerDriver(new Driver());
}
catch(SQLException E)
{
throw new RuntimeException("Can't register driver!");
}
}
This static block call DriverManager.registerDriver(new Driver());
Step 3.
Inside DriverManager.registerDriver() method.
public static synchronized void registerDriver(java.sql.Driver driver)
throws SQLException {
DriverInfo di = new DriverInfo();
di.driver = driver;
di.driverClass = driver.getClass();
di.driverClassName = di.driverClass.getName();
drivers.addElement(di);
println("registerDriver: " + di);
}
DriverManager class create a Vector name drivers and add the Driver class to the vector.
Now In the Vector we have com.mysql.jdbc.Driver object.
Step 4.
To get connection we can
Connection con = DriverManager.getConnection(?jdbc:mysql://localhost:3306/testDB?,?username?,?password?);
In this method , it search the Driver in the vector , if available then connect and return connection.
for (int i = 0; i < drivers.size(); i++) {
DriverInfo di = (DriverInfo)drivers.elementAt(i);
// if ?jdbc:mysql? keywork withic in input , match with the driver in the vector {
Connection result = di.driver.connect(url, info);
}
}
Return connection;
This class getting jdbc:mysql://localhost:3306/testDB as input . This class check for the driver based on ?jdbc:mysql? in this case and connect the driver.
—————
What is Dirty read?
A has changed a row, but has not committed the changes. B reads the uncommitted data but his view of the data may be wrong if A rolls back his changes and updates his own changes to the database.
—————
Topic: JDBC FAQ
—————
—————
—————
—————
—————
—————
—————
—————
—————
—————