Unit 4
Unit 4
1|Page
unit 4
2|Page
unit 4
3|Page
unit 4
14. Class.forName("_________________________"):
- A) sun.jdbc.odbc.JdbcOdbcDriver
- B) All of these
- C) Data Source Name
- D) application program interface
- Answer: A) sun.jdbc.odbc.JdbcOdbcDriver
16. Connection object can be initialized using the _______ method of the DriverManager Class:
- A) executeUpdate()
- B) getConnection()
- C) PreparedStatement
- D) ResultSet
- Answer: B) getConnection()
17. createStatement() method without any parameter is used to create a statement with:
- A) a forward only and read only ResultSet
- B) DriverManager
- C) Data Source Name
- D) application program interface
- Answer: A) a forward only and read only ResultSet
18. Database meta data are retrieved through ________:
- A) a PreparedStatement object
- B) DriverManager
- C) executeUpdate()
- D) ResultSet
- Answer: D) ResultSet
4|Page
unit 4
20. DriverManager.getConnection(_______ , ______ , ______) What are the two parameters that
are included?
- A) URL or machine name where server runs, User ID, Password
- B) executeUpdate(), Data Source Name, application program interface
- C) PreparedStatement, DriverManager, ResultSet
- D) java.sql.ResultSet, executeQuery(), executeUpdate()
- Answer: A) URL or machine name where server runs, User ID, Password
5|Page
unit 4
25. For the execution of INSERT SQL query in JDBC, ___________ method must be used:
- A) executeUpdate()
- B) executeQuery()
- C) ResultSet
- D) DriverManager
- Answer: A) executeUpdate()
26. For the execution of SELECT SQL query in JDBC, _______________ method must be used:
- A) executeQuery()
- B) executeUpdate()
- C) PreparedStatement
- D) DriverManager
- Answer: A) executeQuery()
6|Page
unit 4
30. How can you execute DML statements (i.e., insert, delete, update) in the database?
- A) By invoking the executeUpdate(...) method of a Statement object
- B) Using executeQuery() method of ResultSet
- C) DriverManager
- D) PreparedStatement
- Answer: A) By invoking the executeUpdate(...) method of a Statement object
7|Page
unit 4
32. How many JDBC driver types are available by Sun Microsystems?
- A) Four
- B) Five
- C) Two
- D) Three
- Answer: A) Four
33. How many steps are used to connect any Java application with the database using JDBC?
- A) Five
- B) Six
- C) Four
- D) Three
- Answer: A) Five
34. How many transaction isolation levels are defined in java.sql.Connection interface?
- A) Five
- B) Six
- C) Four
- D) Three
- Answer: A) Five
35. If a PreparedStatement is a SQL SELECT statement, you execute the statement using
__________.
- A) PreparedStatement.executeQuery();
- B) PreparedStatement.executeUpdate();
- C) PreparedStatement.execute();
- D) DriverManager
- Answer: A) PreparedStatement.executeQuery();
8|Page
unit 4
36. If you need to use a stored procedure with output parameters, which of the following
statement type should be used to call the procedure?
- A) CallableStatement
- B) PreparedStatement
- C) Statement
- D) DriverManager
- Answer: A) CallableStatement
38. In JDBC _________ imports all Java classes concerned with database connectivity.
- A) java.sql.
- B) DriverManager
- C) Connection
- D) PreparedStatement
- Answer: A) java.sql.
39. In the following JDBC drivers, which is known as partly java driver?
- A) Native-API driver
- B) Thin driver
- C) Network Protocol driver
- D) DriverManager
- Answer: A) Native-API driver
9|Page
unit 4
40. In the three-tier model, the middle tier of the services acts as a mediator between
______________________ and _____________________.
- A) Java application and databases
- B) GUI and Database
- C) Network and Server
- D) DriverManager and Connection
- Answer: A) Java application and databases
41. In which model does a Java application or applet support database access?
- A) Both a and b
- B) Only a
- C) Only b
- D) None of the above
- Answer: A) Both a and b
42. In which type of driver must ODBC drivers be loaded on the client machine?
- A) Type 1
- B) Type 2
- C) Type 3
- D) Type 4
- Answer: A) Type 1
10 | P a g e
unit 4
44. JDBC is a _______________ interface, which means that it is used to invoke SQL commands
directly.
- A) High-level
- B) Low-level
- C) Medium-level
- D) Advanced-level
- Answer: B) Low-level
11 | P a g e
unit 4
51. Native API converts ___________________ into the ___________________ used by DBMS.
- A) JDBC API, Native API calls
- B) JDBC calls, network protocol
- C) ODBC calls, JDBC API
- D) Network protocol, JDBC calls
- Answer: A) JDBC API, Native API calls
12 | P a g e
unit 4
52. Native-protocol pure Java converts ____________ into the ____________ used by DBMSs
directly.
- A) ODBC calls, JDBC API
- B) JDBC calls, network protocol
- C) Network protocol, JDBC calls
- D) ODBC API, Native API calls
- Answer: B) JDBC calls, network protocol
54. ODBC is not appropriate for direct use from Java because it uses a _______________.
- A) Java interface
- B) JDBC interface
- C) C interface
- D) Python interface
- Answer: C) C interface
13 | P a g e
unit 4
56. ODBC requires configuring _______ which represents the target database.
- A) Data Source Name
- B) Database URL
- C) Database Name
- D) JDBC Connection
- Answer: A) Data Source Name
14 | P a g e
unit 4
- C) ResultSet Class
- D) PreparedStatement Class
- Answer: A) Connection Class
15 | P a g e
unit 4
C) Non-threaded
D) Asynchronous
Answer: B) Multithreaded
65. The -----------------method executes a simple query and returns a single Result Set object
A) executeUpdate()
B) executeQuery()
C) execute()
D) executeFetch()
Answer: B) executeQuery()
66. The .................. object provides you with methods to access data from the table.
A) Connection
B) PreparedStatement
C) ResultSet
D) Statement
Answer: C) ResultSet
67. The ...................... method sets the query parameters of the PreparedStatement Object.
A) setParameters()
B) setValues()
C) setQueryParams()
D) setString()
Answer: D) setString()
16 | P a g e
unit 4
Answer: D) PreparedStatement
69. The ......................... method executes an SQL statement that may return multiple results.
A) executeQuery()
B) executeUpdate()
C) execute()
D) executeMultiple()
Answer: C) execute()
70. The ............................. package contains classes that help in connecting to a database,
sending SQL statements to the database, and processing the query results.
A) java.sql
B) java.connection
C) java.jdbc
D) java.db
Answer: A) java.sql
71. The command to remove all the rows from a table 'CUSTOMER' is:
A) DELETE FROM CUSTOMER
B) REMOVE FROM CUSTOMER
C) TRUNCATE CUSTOMER
D) DROP FROM CUSTOMER
Answer: A) DELETE FROM CUSTOMER
72. The correct string for loading Jdbc-Odbc bridge driver using Class.forName() method is:
A) sun.jdbc.odbc.JdbcOdbcDriver
B) java.jdbc.odbc.JdbcDriver
C) com.jdbc.odbc.JdbcOdbc
17 | P a g e
unit 4
D) oracle.jdbc.odbc.JdbcDriver
Answer: A) sun.jdbc.odbc.JdbcOdbcDriver
75. The JDBC API is an application interface of Java for connecting Java as ___________.
A) Backend
B) Middleware
C) Front End
D) Database
Answer: B) Middleware
18 | P a g e
unit 4
D) Front End
Answer: D) Front End
77. The parameters of the PreparedStatement object are ______________ when the user clicks
on the Query button.
A) Opened
B) Closed
C) Initialized
D) Terminated
Answer: C) Initialized
78. The PreparedStatement ___________ symbol is a placeholder that is replaced by the input
parameter at runtime.
A) $
B) #
C) ?
D) @
Answer: C) ?
79. The ResultSet ______________ provides methods for retrieving and manipulating the results
of executed queries.
A) Executor
B) Interface
C) Processor
D) Iterator
Answer: B) Interface
19 | P a g e
unit 4
82. The _____ object provides you with methods to access data from the table.
A) Connection
B) PreparedStatement
C) ResultSet
D) Statement
Answer: C) ResultSet
83. The _____ package contains classes that help in connecting to a database, sending SQL
statements to the database, and processing the query results.
A) java.sql.
B) java.connection
C) java.jdbc
D) java.db
Answer: A) java.sql.
84. The ___________ interface provides various methods for getting information about the
Database.
A) DatabaseInfo
B) DBInfo
C) DatabaseMetaData
20 | P a g e
unit 4
D) DBMetaData
Answer: C) DatabaseMetaData
85. The ______________driver resolves JDBC calls and makes equivalent ODBC calls.
A) JDBC-ODBC bridge
B) Type 1 driver
C) Type 3 driver
D) Type 4 driver
Answer: A) JDBC-ODBC bridge
86. The ___________________ is the language for interacting with the Database.
A) Java
B) C++
C) Python
D) Structured Query Language (SQL)
Answer: D) Structured Query Language (SQL)
21 | P a g e
unit 4
D) obtainConnection()
Answer: B) getConnection()
22 | P a g e
unit 4
95. What maintains a cursor pointing to a particular row of data initially? Cursor points to
before the first row.
A) ResultSet interface
B) Statement interface
C) DatabaseMetaData interface
D) Connection interface
Answer: A) ResultSet interface
23 | P a g e
unit 4
100. Which class/interface is used for an SQL statement that is executed frequently?
A) PreparedStatement
B) CallableStatement
C) Statement
24 | P a g e
unit 4
D) ResultSet
Answer: A) PreparedStatement
101. Which driver provides JDBC API access via one or more ODBC drivers?
- a) JDBC-ODBC Bridge Driver
- b) Type-2 Driver
- c) Type-3 Driver
- d) Type-4 Driver
- Answer: a) JDBC-ODBC Bridge Driver
103. Which driver is efficient and always preferable for using JDBC applications?
- a) Type-1
- b) Type-2
- c) Type-3
- d) Type-4
- Answer: d) Type-4
25 | P a g e
unit 4
26 | P a g e
unit 4
- d) ResultSet
- Answer: b) PreparedStatement
109. Which interface provides methods to execute queries with the database?
- a) Statement interface
- b) ResultSet interface
- c) Connection interface
- d) PreparedStatement interface
- Answer: a) Statement interface
27 | P a g e
unit 4
- d) String next()
- Answer: a) boolean next()
113. Which JDBC driver Type(s) can be used in either applet or servlet code?
- a) Type 1
- b) Type 2
- c) Both Type 3 and Type 4
- d) Neither Type 3 nor Type 4
- Answer: c) Both Type 3 and Type 4
114. Which JDBC driver Type(s) can you use in a three-tier architecture if the Web server and
the DBMS are running on the same machine?
- a) Type 1
- b) Type 2
- c) Both Type 3 and Type 4
- d) Neither Type 3 nor Type 4
- Answer: c) Both Type 3 and Type 4
115. Which JDBC driver Types are used for over communications networks?
- a) Type 1
- b) Type 2
- c) Both Type 3 and Type 4
- d) Neither Type 3 nor Type 4
- Answer: c) Both Type 3 and Type 4
116. Which kind of driver converts JDBC calls into calls on the client API for Oracle, Sybase,
Informix, IBM DB2, or other DBMS?
- a) JDBC-ODBC Bridge Driver
- b) Native-API partly-Java driver
- c) Thin driver
28 | P a g e
unit 4
- d) Thick driver
- Answer: b) Native-API partly-Java driver
117. Which method is used to obtain the count of total rows of ResultSet?
- a) getRowCount()
- b) countRows()
- c) getRow()
- d) getRowCountInResultSet()
- Answer: c) getRow()
119. Which method is used to send CREATE TABLE queries to the database using JDBC?
- a) executeQuery()
- b) execute()
- c) executeUpdate()
- d) createTable()
- Answer: c) executeUpdate()
120. Which method must be used to execute INSERT, UPDATE, or DELETE statements?
- a) executeQuery()
- b) execute()
- c) executeUpdate()
29 | P a g e
unit 4
- d) executeStatement()
- Answer: c) executeUpdate()
121. Which method of class is used to register & dynamically load the driver class?
- a) registerDriver()
- b) loadDriver()
- c) forName()
- d) initDriver()
- Answer: c) forName()
122. ___________ type of driver is very flexible and needs no code installation on the client
machine.
- a) Type 1-JDBC-ODBC Bridge
- b) Type 2-JDBC-Native API
- c) Type 3-JDBC-Net Pure Java
- d) Type 4-JDBC-Thin
- Answer: c) Type 3-JDBC-Net Pure Java
124. ________ is an open-source DBMS product that runs on UNIX, Linux, and Windows.
- a) Oracle
- b) MySQL
- c) SQL Server
30 | P a g e
unit 4
- d) PostgreSQL
- Answer: b) MySQL
126. ___________ Calls get converted into native C or C++ API calls.
- a) JDBC API
- b) JDBC Driver
- c) JDBC Statement
- d) JDBC Connection
- Answer: a) JDBC API
31 | P a g e
unit 4
130. _____________________ Returns the current row number, with the first row starting at 1.
- a) getRowIndex()
- b) rowNumber()
- c) getRowNumber()
- d) getRow()
- Answer: d) getRow()
131. ………………… can not directly communicate with the database to submit data & retrieve
the result.
- a) Java
- b) JDBC
- c) Python
- d) C++
- Answer: b) JDBC
32 | P a g e
unit 4
133:Consider the following code. What should be the correction done in the code to get the
correct output?
import java.sql.;
Options:
1. `String s= ("jdbc:odbc:mystudtable");`
2. `Both option i and ii`
3. `studste.executeUpdate();`
4. `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");`
33 | P a g e
unit 4
134:Consider the following code. What will be the student table data after executing this code as
the table was empty?
import java.sql.;
Options:
1. `4 N A 2`
2. `only one record in table`
3. `Missing Statement`
4. `Use satement Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");`
34 | P a g e
unit 4
135:Consider the following code. Which statement needs to update the student table data by this
code?
import java.sql.;
Options:
1. `studste.executeUpdate();`
2. `Connection con = DriverManager.getConnection(s);`
3. `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");`
4. `ResultSet Rset = stmt.executeQuery(studqry);`
35 | P a g e
unit 4
| ID | name | Salary |
|----|--------|--------|
| 1 | ramesh | 1200 |
| 2 | ganesh | 1111 |
| 6 | sanjay | 9500 |
```java
import java.sql.;
class SelectDemo {
public static void main(String a[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String url="jdbc:odbc:mydsn";
Connection con=DriverManager.getConnection(url);
System.out.println(" Connection to DataBase created");
Statement stmt=con.createStatement();
String query="select from emp";
ResultSet rs=stmt.executeQuery(query);
while(rs.next()) {
System.out.println(" ID : "+ rs.getInt(1));
System.out.println(" name : "+ rs.getString(2));
System.out.println(" Salary : "+ rs.getInt(3));
System.out.println();
}
} catch(ClassNotFoundException e) {
36 | P a g e
unit 4
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
```
Options:
1. Output showing records but with errors in formatting
2. `4 N A 2`
3. `Missing Statement`
4. `ResultSet rs=stmt.executeQuery(query);`
Correct Answer: Option 1 - Output showing records but with errors in formatting
137:Consider the following program. Select the statement that should be added to the program
to get the correct output.
```java
import java.sql.;
37 | P a g e
unit 4
while (res.next()) {
int i = res.getInt("stud_code");
String s = res.getString("stud_name");
System.out.println(i + "\t\t" + s);
}
con.close();
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
}
} catch (Exception e) {
System.out.println("Error:connection not created");
}
}
}
```
Options:
1. `st.executeQuery()`
2. `res.executeQuery()`
3. `st.executeUpdate()`
4. `con.executeQuery()`
138:Consider the following program. Select the statement that should be added to the program
to get the correct output.
```java
import java.sql.;
38 | P a g e
unit 4
Options:
1. Use `satement Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");`
2. Missing Statement
3. `PreparedStatement s = c.prepareStatement("update db3 set Name=? where Roll_no=?");`
4. `Statement s = c.createStatement();`
139:Consider the following program. Select the statement that should be added to the program
to get the correct output.
```java
import java.sql.;
class create {
public static void main(String args[]) {
39 | P a g e
unit 4
try {
Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
System.out.printf("Driver Loaded");
String url = " jdbc.odbc.:dsn1";
Connection con = DriverManager.getConnection(url);
System.out.println("Connection established ");
String sql = "create table employee1(emp_id int,emp_name char";
PreparedStatement ps1 = con.prepareStatement(sql);
System.out.println("table created");
ps1.close();
con.close();
} catch(Exception e) {
}
}
}
```
Options:
1. `ps1.executeUpdate();`
2. Missing Statement
3. `PreparedStatement ps1 = con.prepareStatement(sql);`
4. `String url = " jdbc.odbc.:dsn1";`
140:Consider the following program. Select the statement that should be added to the program
to get the correct output.
```java
import java.sql.;
import java.io.;
40 | P a g e
unit 4
Options:
1. `PreparedStatement stmt=conn.prepareStatement("Delete from Stud wher");`
2. Missing Statement
3. `stmt.setInt(1, rno);`
4. `stmt.executeUpdate();`
141:Consider the following program. What correction should be done in the program to get the
correct output?
41 | P a g e
unit 4
```java
import java.sql.;
import java.io.;
class Create {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:oraodbc", "scott", "tiger");
Statement stmt = con.createStatement();
stmt.executeUpdate("create table customers(CUST_NUM int, COMPANY char(20),
CUST_REP int, CREDIT_LIMIT number(7,2))");
stmt.close();
con.close();
System.out.println("Table Successfully created");
} catch(Exception e) {
e.printStackTrace();
}
}
}
```
Options:
1. Missing Statement
2. `Statement stmt = con.createStatement();`
3. `stmt.executeUpdate("create table customers(CUST_NUM int, COMPANY char(20), CUST_REP
int, CREDIT_LIMIT number(7,2))");`
4. `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");`
42 | P a g e
unit 4
```java
import java.sql.;
class ConnectionTest {
public static void main(String srgs[]) {
try {
Class.forname("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
String url="jdbc:odbc:xyz";
Connection con=DriverManager.getConnection(url);
System.out.println("connection to database created");
} catch(sQLException e) {
System.out.pritln("SQL error");
} catch(Exceptionn e) {
System.out.println("error");
}
}
}
```
Options:
1. Driver loaded Connection to database created
2. `4 N A 2`
3. Missing Statement
4. `Class.forname("sun.jdbc.odbc.JdbcOdbcDriver");`
143:Consider the following program. What will be the output of the following code?
43 | P a g e
unit 4
```java
import java.sql.;
Options:
1. Empty Table
2. Missing Statement
3. `4 N A 2`
4. `SQLException`
144:Consider the following program. Which package should be added to the program to get the
correct code?
```java
public class db15 {
44 | P a g e
unit 4
Options:
1. `java.sql.`
2. Missing Statement
3. `java.io.`
4. `java.util.`
145:Consider the following program, what should be the correction done in the program to get
the correct output?
```java
import java.sql.;
45 | P a g e
unit 4
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connection established");
} catch(Exception e) {
System.out.println("Connection error");
}
try {
String str = "jdbc.odbc:dsn1";
con = DriverManager.getConnection(str, "", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Selectfrom employee");
int n = rs.getMetaData().getColumnCount();
for(int i = 1; i <= n; i++) {
System.out.print(rs.getMetaData().getColumnLabel(i) + '\t');
System.out.println("");
while(rs.next()) {
for(int i = 1; i <= n; i++) {
System.out.print(rs.getString(i) + '\t');
}
System.out.println("");
}
rs.close();
con.close();
}
} catch(SQLException e) {
System.out.print("SQL error");
}
}
}
```
Options:
46 | P a g e
unit 4
1. Missing Statement
2. `for(int i = 1; i <= n; i++)`
3. `System.out.print(rs.getMetaData().getColumnLabel(i) + '\t');`
4. `System.out.print(rs.getString(i) + '\t');`
146:Consider the following program. Select the statement that should be added to the program
to get the correct output.
```java
import java.sql.;
class PreparedInsert {
public static void main(String a[]) {
Try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:javadb");
System.out.println(" Connection to DataBase created");
String a1 = "Insert into employee(id,name,salary) values(?,?,?)";
PreparedStatement ps = con.prepareStatement(a1);
ps.execute(a1);
System.out.println("Record Inserted");
String querySel = "Select from employee";
ResultSet rs = ps.executeQuery(querySel);
47 | P a g e
unit 4
System.out.println("After Insertion");
while(rs.next()) {
System.out.println(" ID : "+ rs.getInt(1));
System.out.println(" Name : "+ rs.getString(2));
System.out.println(" Salary : "+ rs.getInt(3));
System.out.println();
}
con.close();
} catch(SQLException e) {
System.out.println("SQL Error");
} catch(Exception e) {
System.out.println("Error");
}
}
}
`
Options:
1. All the above
2. `ps.execute(a1);`
3. `ResultSet rs = ps.executeQuery(querySel);`
4. `System.out.println("After Insertion");`
```java
import java.sql.;
class Type4_simple {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
48 | P a g e
unit 4
```java
public class SimpleDemo {
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
String jdbcurl="jdbc:mysql://localhost:3306/test";
con=DriverManager.getConnection(jdbcurl, "root", "root");
System.out.println("Connection established");
stmt=con.createStatement();
rs=stmt.executeQuery("select from emp");
System.out.println("Empid \t Emap name \t City \t Salary");
49 | P a g e
unit 4
while(rs.next()) {
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getFloat(4));
System.out.println();
}
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch(Exception e) {
}
}
}
}
```
Options:
1. All of the above
2. `catch(SQLException e) {e.printStackTrace()}`
3. `con=DriverManager.getConnection(jdbcurl, "root", "root");`
4. `rs=stmt.executeQuery("select from emp");`
149:Consider the following program. What should be the correction done in the program to get
the correct output?
50 | P a g e
unit 4
import java.sql.;
class ExecuteQueryTest {
public static void main(String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
String url="jdbc:odbc:xyz";
Connection con = DriverManager.getConnection(url);
Statement state =con.createStatement();
System.out.println("Statement object created");
String sql= "select Name,Age from student";
ResultSet rs =state.executeQuery(sql);
String text = " ";
while (rs.next()) {
text+= rs.getString(1) + " " + rs .getInt (2) + '\n' ;
}
System.out.println(text);
} catch(Exception e) {
System.out.println(“error”);
}
}
}
```
Options:
1. `String sql= "select Name,Age from student";`
2. Missing Statement
3. `state.executeQuery(sql);`
4. `Connection con = DriverManager.getConnection(url);`
51 | P a g e
unit 4
52 | P a g e