Multiple Choice Questions on JDBC in Java
JDBC is an API (Application Programming Interface) that helps a programmer to write a Java program to connect to a database, retrieve the data from the database, and perform various operations on the data in a Java program. As it is an important topic, the questions related to JDBC frequently asked in Java interviews and competitive exams. So, in this section, we have collected some multiple-choice questions based on JDBC that are important from the perspective of different competitive exams and interviews.
1) What are the major components of the JDBC?
2) Select the packages in which JDBC classes are defined?
Explanation: JDBC API is divided into two packages i.e. java.sql and javax.sql. We have to import these packages to use classes and interfaces in our application.
3) Thin driver is also known as?
Explanation: The JDBC thin driver is a pure Java driver. It is also known as Type-4 Driver. It is platform-independent so it does not require any additional Oracle software on the client-side. It communicates with the server using SQL *Net to access Oracle Database.
4) What is the correct sequence to create a database connection?
i. Import JDBC packages.
ii. Open a connection to the database.
iii. Load and register the JDBC driver.
iv. Execute the statement object and return a query resultset.
v. Create a statement object to perform a query.
vi. Close the resultset and statement objects.
vii. Process the resultset.
viii. Close the connection.
Explanation: To create a database connection in Java, we must follow the sequence given below:
5) Which of the following method is used to perform DML statements in JDBC?
Explanation: We use the executeUpdate() method for DML SQL queries that change data in the database, such as INSERT, UPDATE, and DELETE which do not return a resultset.
6) How many transaction isolation levels provide the JDBC through the Connection interface?
Explanation: The following table defines transaction isolation levels.
7) Which of the following method is static and synchronized in JDBC API?
Explanation: A Java application using the JDBC API establishes a connection to a database by obtaining a Connection object. The standard way to obtain a Connection object is to call the method DriverManager.getConnection() method that accepts a String contains the database connection URL. It is a static and synchronized method.
8) Which methods are required to load a database driver in JDBC?
Explanation: There are two ways to load a database driver in JDBC:
9) Parameterized queries can be executed by?
Explanation: The PreparedStatement interface extends the Statement interface. It represents a precompiled SQL statement that can be executed multiple times. It accepts parameterized SQL quires. We can pass 0 or more parameters to this query.
10) Which of the following is not a valid statement in JDBC?
11) Identify the isolation level that prevents the dirty in the JDBC Connection class?
Explanation: The isolation level TRANSACTION_READ_COMMITTED prevents the dirty read but non-repeatable reads and phantom reads can occur.
12) What does setAutoCommit(false) do?
Explanation: The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. After the auto-commit mode is disabled, no SQL statements are committed until we call the commit() method explicitly.
13) Stored procedure can be called by using the ????..?
Explanation: The stored procedure is a database program that can be utilized to perform CRUD tasks with the table. We can call these procedures by using the Statement Interface. It provides methods to execute queries with the database.
14) What should be the correct order to close the database resource?What should be the correct order to close the database resource?
Explanation: The golden rule to JDBC connections and statements is to close in the reverse order of initiation or opening. In addition, the ResultSet is dependant on the execution of the Statement and the Statement is dependant on the Connection instance. Hence, the closing should occur in that order (ResultSet, Statement, and then Connection).
15) A good way to debug JDBC-related problems is to enable???..?
Explanation: The JDBC Driver supports both DriverManager and DataSource tracing as documented in the JDBC 3.0 API specification. Trace information consists of JDBC API method entry and exit points with the corresponding parameter and returns values. DriverManager.setLogWriter method to send trace messages to a PrintWriter. The trace output contains a detailed listing of the JDBC activity.
16) Which JDBC driver can be used in servlet and applet both?
Explanation: Type 3 driver follows the three-tier approach which is used to access the databases. The JDBC clients use standard network sockets to communicate with a middleware application server. In a Type 4 driver, a pure Java-based driver that communicates directly with the vendor's database through a socket connection.
17) JDBC-ODBC driver is also known as?
Explanation: Type 1 driver is also known as the JDBC-ODBC bridge driver. It is a database driver implementation that employs the ODBC driver to connect to the database. The driver converts JDBC method calls into ODBC function calls.
18) Which of the following driver is the fastest one?
Explanation: JDBC Net pure Java driver (Type 4) is the fastest driver because it converts the JDBC calls into vendor-specific protocol calls and it directly interacts with the database.
19) Which of the following is not a type of ResultSet object?
Explanation: There are three types of ResultSet object:
Based on the concurrency there are two types of ResultSet object.
20) What is JDBC Savepoint?
Explanation: JDBC Savepoint helps us in creating checkpoints in a transaction and we can rollback to that particular checkpoint.
21) How many stages are used by Java programmers while using JDBC in their programs?
Explanation: There are following stages in a JDBC program:
22) Which of the following is the correct to register a JdbcOdbcDriver?
Explanation: By creating an object to the driver class of the driver software, we can register the driver. To register the JdbcOdbcDriver of the sun microsystems, we can create an object to the driver class JdbcOdbcDriver, as follows:
sun.jdbc.odbc.JdbcOdbcDriver obj = new sun.jdbc.odbc.JdbcOdbcDriver();
23) How many ways to register a driver?
Explanation: There are four ways to register a driver:
1. By creating an object of the Driver For example:
sun.jdbc.odbc.JdbcOdbcDriver obj = new sun.jdbc.odbc.JdbcOdbcDriver();
2. By sending the driver class object to the registerDriver() method of the DriverManager class. For example:
3. By sending the driver class name directly to the forName() For example:
4. By using the getProperty() method of the System class. For example:
String dname = System.getProperty("driver"); Class.forName(dname);
If you are using the above method to register the driver, we should have to specify the driver's name at the time of running the program. The getProperty() method receives the driver name and stores the driver name in dname.
We use the following command to provide the driver name at run time:
c:\> java -Ddriver = driverclassname Programname
c:\> java -Ddriver = sun.jdbc.odbc.JdbcOdbcDriver MyProgram
24) Identify the DSN in the following statement:
Explanation: Data Source Name (DSN) is a name given to the database to identify it in the Java program. The DSN is linked with the actual location of the database.
25) Which statement is correct if we want to connect the Oracle database using the thin driver provided by Oracle Corp.?
Explanation: We use the following statement to connect Oracle database using the thin driver.
DriverManager.getConnection("jdbc::thin@localhost:1521:oracle", "scott", "tiger");
26) What are the types of ResultSet in JDBC?
Explanation: JDBC provides only two types of ResultSets: Forward and Scrollable ResultSet.
27) What is blob in the following statement?
Explanation: SQL offers BLOB (Binary Large OBject) data type to store image files like .gif or .jpg or jpeg into the database table.
28) Which data type is used to store files in the database table?
Explanation: To store a large volume of data such as text file into a table, we use CLOB (Character Large OBject) data type of SQL.
29) DatabaseMetaData interface is used to get?????..?
Explanation: DatabaseMetaData is an interface that is used to get Comprehensive information about the database as a whole. It is implemented by driver vendors to let users know the capabilities of a DBMS in combination with the JDBC driver that is used with it.
30) Which of the following driver converts the JDBC calls into database-specific calls?
Explanation: Type 2 driver converts JDBC calls into database-specific calls with the help of vendor database library. It directly communicates with the database server.
31) Are ResultSets updateable?
Explanation: By default, a ResultSet object is not updatable and its cursor moves only in the forward direction. If we want to create an updateable ResultSet object, either we can use ResultSet.TYPE_SCROLL_INSENSITIVE or the ResultSet.TYPE_SCROLL_SENSITIVE type, which moves the cursor forward and backward relative to the current position.
32) Which of the following interface provides the commit() and rollback() methods?
Explanation: The connection interface provides the commit() and rollback() method. The commit() method makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by this Connection object. The rollback() method undoes all changes made in the current transaction and releases any database locks currently held by this Connection object.
33) How many statement objects can be created using a Connection?
Explanation: Multiple statements can be created and used on the same connection, but only one resultset at once can be created and used on the same statement.
34) JDBC API supports____________ and __________ architecture model for accessing the database.
Explanation: The JDBC API supports both two-tier and three-tier processing models for database access. In the two-tier model, a Java application talks directly to the data source. In the three-tier model, commands are sent to a "middle tier" of services, which then sends the commands to the data source.
35) Which statement(s) is/ are true about transactions?
i. A transaction is a set of one or more SQL statements that make up a logical unit of work.
ii. A transaction ends with either a commit or a rollback, depending on whether there are any problems with data consistency or data concurrency.
iii. A lock is a mechanism that allows two transactions from manipulating the same data at the same time.
iv. To avoid conflicts during a transaction, a DBMS uses locks, mechanisms for blocking access by others to the data that is being accessed by the transaction.
Explanation: The statements i, ii, and iv are true about transactions but iii is not because the lock mechanism prohibits two transactions from manipulating the same data at the same time.