• +91 9723535972
  • info@interviewmaterial.com

JDBC Interview Questions and Answers

JDBC Interview Questions and Answers

Question - 41 : - What Is Metadata And Why Should I Use It?

Answer - 41 : -

Metadata ('data about data') is information about one of two things:

  • Database information (java.sql.DatabaseMetaData), or
  • Information about a specific ResultSet (java.sql. ResultSet MetaData).
Use DatabaseMetaData to find information about your database, such as its capabilities and structure. Use ResultSetMetaData to find information about the results of an SQL query, such as size and types of columns.

Question - 42 : - How Does The Java Database Connectivity (jdbc) Work?

Answer - 42 : -

The JDBC is used whenever a Java application should communicate with a relational database for which a JDBC driver exists. JDBC is part of the Java platform standard; all visible classes used in the Java/database communication are placed in package java.sql.

Main JDBC classes:

  • 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 (such as odbc or dbAnywhere/dbaw) 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.
  • Statement: Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
  • ResultSet: The answer/result from a statement. A ResultSet is a fancy 2D list which encapsulates all outgoing results from a given SQL query.

Question - 43 : - What Is The Advantage Of Using A Preparedstatement?

Answer - 43 : -

For SQL statements that are executed repeatedly, using a PreparedStatement object would almost always be faster than using a Statement object. This is because creating a PreparedStatement object by explicitly giving the SQL statement causes the statement to be precompiled within the database immediately. Thus, when the PreparedStatement is later executed, the DBMS does not have to recompile the SQL statement and prepared an execution plan - it simply runs the statement.

Typically, PreparedStatement objects are used for SQL statements that take parameters. However, they can also be used with repeatedly executed SQL statements that do not accept parameters.

Question - 44 : - How Do I Extract The Sql Statements Required To Move All Tables And Views From An Existing Database To Another Database?

Answer - 44 : -

The operation is performed in 9 steps:

  • Open a connection to the source database. Use the DriverManager class.
  • Find the entire physical layout of the current database. Use the DatabaseMetaData interface.
  • Create DDL SQL statements for re-creating the current database structure. Use the DatabaseMetaData interface.
  • Build a dependency tree, to determine the order in which tables must be setup. Use the DatabaseMetaData interface.
  • Open a connection to the target database. Use the DriverManager class.
  • Execute all DDL SQL statements from (3) in the order given by (4) in the target database to setup the table and view structure. Use the PreparedStatement interface.
  • If (6) threw exceptions, abort the entire process.
  • Loop over all tables in the physical structure to generate DML SQL statements for re-creating the data inside the table. Use the ResultSetMetaData interface.
  • Execute all DML SQL statements from (8) in the target database.

Question - 45 : - What Does Resultset Actually Contain? Is It The Actual Data Of The Result Or Some Links To Databases? If It Is The Actual Data Then Why Can't We Access It After Connection Is Closed?

Answer - 45 : -

A ResultSet is an interface. Its implementation depends on the driver and hence ,what it "contains" depends partially on the driver and what the query returns. For example with the Odbc bridge what the underlying implementation layer contains is an ODBC result set. A Type 4 driver executing a stored procedure that returns a cursor - on an oracle database it actually returns a cursor in the databse. The oracle cursor can however be processed like a ResultSet would be from the client. Closing a connection closes all interaction with the database and releases any locks that might have been obtained in the process.

Question - 46 : - Can I Use The Jdbc-odbc Bridge Driver In An Applet?

Answer - 46 : - No

Question - 47 : - How Can I Connect From An Applet To A Database On The Server?

Answer - 47 : -

There are two ways of connecting to a database on the server side.

  • The hard way. Untrusted applets cannot touch the hard disk of a computer. Thus, your applet cannot use native or other local files (such as JDBC database drivers) on your hard drive. The first alternative solution is to create a digitally signed applet which may use locally installed JDBC drivers, able to connect directly to the database on the server side.
  • The easy way. Untrusted applets may only open a network connection to the server from which they were downloaded. Thus, you must place a database listener (either the database itself, or a middleware server) on the server node from which the applet was downloaded. The applet would open a socket connection to the middleware server, located on the same computer node as the webserver from which the applet was downloaded. The middleware server is used as a mediator, connecting to and extract data from the database.

Question - 48 : - How Do I Insert An Image File (or Other Raw Data) Into A Database?

Answer - 48 : -

All raw data types (including binary documents or images) should be read and uploaded to the database as an array of bytes, byte[]. Originating from a binary file,

  • Read all data from the file using a FileInputStream.
  • Create a byte array from the read data.
  • Use method setBytes(int index, byte[] data); of java.sql.PreparedStatement to upload the data.

Question - 49 : - How Can Resultset Records Be Restricted To Certain Rows?

Answer - 49 : -

The easy answer is "Use a JDBC 2.0 compliant driver". With a 2.0 driver, you can use the setFetchSize() method within a Statement or a ResultSet object.
For example,

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from customers");
will change the default fetch size to 400.
You can also control the direction in which the rows are processed. For instance:
will process the rows from bottom up.
The driver manager usually defaults to the most efficient fetch size...so you may try experimenting with different value for optimal performance.

Question - 50 : - What Is The Difference Between Client And Server Database Cursors?

Answer - 50 : -

What you see on the client side is the current row of the cursor which called a Result (ODBC) or ResultSet (JDBC). The cursor is a server-side entity only and remains on the server side.

NCERT Solutions


Share your email for latest updates


Our partners