Javatpoint Logo
Javatpoint Logo

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?

  1. DriverManager, Driver, Connection, Statement, and ResultSet
  2. DriverManager, Driver, Connection, and Statement
  3. DriverManager, Statement, and ResultSet
  4. DriverManager, Connection, Statement, and ResultSet

Answer: a

Explanation:

  • DriverManager: Manages a list of database drivers.
  • Driver: The database communications link, handling all communication with the database.
  • 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 ResultSet represents a set of rows retrieved due to query execution.

2) Select the packages in which JDBC classes are defined?

  1. jdbc and javax.jdbc
  2. rdb and javax.rdb
  3. jdbc and java.jdbc.sql
  4. sql and javax.sql

Answer: d

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?

  1. Type 3 Driver
  2. Type-2 Driver
  3. Type-4 Driver
  4. Type-1 Driver

Answer: c

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.

  1. i, ii, iii, v, iv, vii, viii, vi
  2. i, iii, ii, v, iv, vii, vi, viii
  3. ii, i, iii, iv, viii, vii, v, vi
  4. i, iii, ii, iv, v, vi, vii, viii

Answer: b

Explanation: To create a database connection in Java, we must follow the sequence given below:

  1. Import JDBC packages.
  2. Load and register the JDBC driver.
  3. Open a connection to the database.
  4. Create a statement object to perform a query.
  5. Execute the statement object and return a query resultset.
  6. Process the resultset.
  7. Close the resultset and statement objects.
  8. Close the connection.

5) Which of the following method is used to perform DML statements in JDBC?

  1. executeResult()
  2. executeQuery()
  3. executeUpdate()
  4. execute()

Answer: c

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?

  1. 3
  2. 4
  3. 7
  4. 2

Answer: b

Explanation: The following table defines transaction isolation levels.

Transaction Isolation Level Description
TRANSACTION_READ_UNCOMMITTED Dirty reads, non-repeatable reads, and phantom reads can occur.
TRANSACTION_READ_COMMITTED Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
TRANSACTION_REPEATABLE_READ Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
TRANSACTION_SERIALIZABLE Dirty reads, non-repeatable reads, and phantom reads are prevented.

7) Which of the following method is static and synchronized in JDBC API?

  1. getConnection()
  2. prepareCall()
  3. executeUpdate()
  4. executeQuery()

Answer: A

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?

  1. getConnection()
  2. registerDriver()
  3. forName()
  4. Both b and c

Answer: d

Explanation: There are two ways to load a database driver in JDBC:

  • By using the registerDriver() Method: To access the database through a Java application, we must register the installed driver in our program. We can do this with the registerDriver() method that belongs to the DriverManager class. The registerDriver() method takes as input a driver class, that is, a class that implements the java.sql.Driver interface, as is the case with OracleDriver.
  • By using the Class.forName() Method: Alternatively, we can also use the forName() method of the java.lang.Class to load the JDBC drivers directly. However, this method is valid only for JDK-compliant Java virtual machines. It is invalid for Microsoft JVMs.

9) Parameterized queries can be executed by?

  1. ParameterizedStatement
  2. PreparedStatement
  3. CallableStatement and Parameterized Statement
  4. All kinds of Statements

Answer: b

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?

  1. Statement
  2. PreparedStatement
  3. QueryStatement
  4. CallableStatement

Answer: c

Explanation:

  • Statement: Use this for general-purpose access to your database. It is useful when we are using static SQL statements at runtime. The Statement interface cannot accept parameters.
  • PreparedStatement: It represents the pre-compiled SQL statements that can be executed multiple times.
  • CallableStatement: It is used to execute SQL stored procedures.
  • QueryStatement: It is not supported by JDBC.

11) Identify the isolation level that prevents the dirty in the JDBC Connection class?

  1. TRANSACTION_READABLE_READ
  2. TRANSACTION_READ_COMMITTED
  3. TRANSACTION_READ_UNCOMMITTED
  4. TRANSACTION_NONE

Answer: b

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?

  1. It will not commit transactions automatically after each query.
  2. It explicitly commits the transaction.
  3. It never commits the transactions.
  4. It does not commit transaction automatically after each query.

Answer: b

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 ????..?

  1. CallableStatement
  2. Statement
  3. CalledStatement
  4. PreparedStatement

Answer: b

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?

  1. Connection, Statements, and then ResultSet
  2. ResultSet, Connection, and then Statements
  3. Statements, ResultSet, and then Connection
  4. ResultSet, Statements, and then Connection

Answer: d

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???..?

  1. JDBC tracing
  2. Exception handling
  3. Both a and b
  4. Only b

Answer: a

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?

  1. Type 3
  2. Type 4
  3. Type 3 and Type 2
  4. Type 3 and Type 4

Answer: d

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?

  1. Type 4
  2. Type 3
  3. Type 1
  4. Type 2

Answer: c

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?

  1. JDBC-ODBC Bridge Driver
  2. Native API Partly Java Driver
  3. Network Protocol Driver
  4. JDBC Net Pure Java Driver

Answer: d

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?

  1. TYPE_FORWARD_ONLY
  2. CONCUR_WRITE_ONLY
  3. TYPE_SCROLL_INSENSITIVE
  4. TYPE_SCROLL_SENSITIVE

Answer: b

Explanation: There are three types of ResultSet object:

  • TYPE_FORWARD_ONLY: This is the default type and the cursor can only move forward in the result set.
  • TYPE_SCROLL_INSENSITIVE: The cursor can move forward and backward, and the result set is not sensitive to changes made by others to the database after the result set was created.
  • TYPE_SCROLL_SENSITIVE: The cursor can move forward and backward, and the result set is sensitive to changes made by others to the database after the result set was created.

Based on the concurrency there are two types of ResultSet object.

  • CONCUR_READ_ONLY: The ResultSet is read-only, this is the default concurrency type.
  • CONCUR_UPDATABLE: We can use the ResultSet update method to update the rows data.

20) What is JDBC Savepoint?

  1. An intermediate or checkpoint in a transaction
  2. A point where we can store queries
  3. A point where the JDBC application starts execution
  4. A memory where we can store transaction

Answer: a

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?

  1. 3
  2. 2
  3. 5
  4. 6

Answer: d

Explanation: There are following stages in a JDBC program:

  • Register the driver
  • Connecting to a database
  • Preparing SQL statements in Java
  • Executing the SQL statements on the database
  • Retrieving the results
  • Closing the connection

22) Which of the following is the correct to register a JdbcOdbcDriver?

  1. jdbc.odbc.JdbcOdbcDriver obj = new sun.jdbc.odbc.JdbcOdbcDriver();
  2. odbc.JdbcOdbcDriver obj = new sun.odbc.JdbcOdbcDriver();
  3. jdbc.JdbcOdbcDriver obj = new sun.jdbc.JdbcOdbcDriver();
  4. jdbc.odbc.JdbcOdbc obj = new sun.jdbc.odbc.JdbcOdbc();

Answer: a

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?

  1. 2
  2. 3
  3. 4
  4. 5

Answer: c

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:

DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());

3. By sending the driver class name directly to the forName() For example:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

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

For example:

c:\> java -Ddriver = sun.jdbc.odbc.JdbcOdbcDriver MyProgram

24) Identify the DSN in the following statement:

  1. jdbc
  2. odbc
  3. scott
  4. oradsn

Answer: d

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.?

  1. getConnection("jdbc::thin@localhost:1521:oracle", "scott", "tiger");
  2. getConnection("jdbc:thin@localhost:1521:oracle", "scott", "tiger");
  3. getConnection("jdbc::thin@localhost:1522:oracle", "scott", "tiger");
  4. getConnection("jdbc::oracle@localhost:1521:thin", "scott", "tiger");

Answer: a

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?

  1. Forward ResultSet
  2. Scrollable ResultSet
  3. Only a
  4. Both a and b

Answer: d

Explanation: JDBC provides only two types of ResultSets: Forward and Scrollable ResultSet.


27) What is blob in the following statement?

  1. Variable
  2. Object
  3. Data type
  4. Keyword

Answer: c

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?

  1. BLOB
  2. CLOB
  3. File
  4. Both a and b

Answer: b

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?????..?

  1. Comprehensive information about the database as a whole.
  2. Comprehensive information about the table as a whole.
  3. Comprehensive information about the column as a whole.
  4. Both b and c

Answer: a

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?

  1. JDBC-ODBC Bridge Driver (Type 1)
  2. Native API-partly Java Driver (Type 2)
  3. Net Protocol-pure Java Driver (Type 3)
  4. Native Protocol-pure Java Driver (Type 4)

Answer: b

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?

  1. Yes, but only if we call the method openCursor() on the ResultSet and if the driver and database support this option.
  2. Yes, but only if we indicate a concurrency strategy when executing the statement, and if the driver and database support this option.
  3. Yes, but only if the ResultSet is an object of class UpdateableResultSet and if the driver and database support this option.
  4. No, ResultSets are never updateable. We must explicitly execute a DML statement to change the data in the underlying database.

Answer: b

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?

  1. Statement Interface
  2. ResultSet Interface
  3. Connection Interface
  4. RowSet Interface

Answer: c

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?

  1. 2
  2. 1
  3. 3
  4. Multiple

Answer: d

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.

  1. Two-tier
  2. Three-tier
  3. Both a and b
  4. Only b

Answer: c

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.

  1. Only i and ii
  2. Only i and iii
  3. Only i, ii, and iv
  4. All the above

Answer: c

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.


Next Topic#




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA