0% found this document useful (0 votes)
14 views12 pages

Interacting With Database

The document provides a comprehensive overview of JDBC (Java DataBase Connectivity), including key concepts such as establishing connections, executing SQL statements, handling transactions, and using various JDBC interfaces like Statement and ResultSet. It covers methods for data manipulation, exception handling, and the advantages of using PreparedStatements over regular Statements. Additionally, it addresses SQL commands and best practices for database interaction, ensuring data integrity and security.

Uploaded by

lochansawant0107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Interacting With Database

The document provides a comprehensive overview of JDBC (Java DataBase Connectivity), including key concepts such as establishing connections, executing SQL statements, handling transactions, and using various JDBC interfaces like Statement and ResultSet. It covers methods for data manipulation, exception handling, and the advantages of using PreparedStatements over regular Statements. Additionally, it addresses SQL commands and best practices for database interaction, ensuring data integrity and security.

Uploaded by

lochansawant0107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

INTERACTING WITH DATABASE

1. What is JDBC?
a. Java DataBase Connection
b. Java DataBase Configuration
c. Java DataBase Class
d. Java DataBase Code
Answer: a

2. Which interface in JDBC is used to execute SQL statements?


a. Connection
b. Statement
c. ResultSet
d. DriverManager
Answer: b

3. Which method is used to establish a connection to the database in JDBC?


a. createConnection()
b. getConnection()
c. openConnection()
d. connect()
Answer: b

4. What does the ResultSet interface represent?


a. A table in the database
b. A set of results from a database query
c. A connection to the database
d. A SQL statement
Answer: b
5. Which JDBC driver is the most portable?
a. Type 1
b. Type 2
c. Type 3
d. Type 4
Answer: d

6. Which statement is used to update data in a database?


a. SELECT
b. UPDATE
c. MODIFY
d. CHANGE
Answer: b

7. What does a PreparedStatement provide over a regular Statement?


a. Better performance
b. More flexibility
c. SQL injection prevention
d. All of the above
Answer: d

8. Which method of the Connection interface is used to create a Statement object?


a. createStatement()
b. getStatement()
c. prepareStatement()
d. statement()
Answer: a

9. How can you retrieve the number of rows affected by an SQL statement?
a. Using execute()
b. Using executeUpdate()
c. Using executeQuery()
d. Using executeBatch()
Answer: b

10. Which class is used to load the JDBC driver?


a. DriverManager
b. Connection
c. Statement
d. ResultSet
Answer: a

11. What is the purpose of the commit() method in JDBC?


a. To save all changes to the database
b. To undo changes
c. To start a transaction
d. To execute a query
Answer: a

12. Which method is used to close a ResultSet?


a. closeResultSet()
b. close()
c. terminate()
d. end()
Answer: b

13. What is a Transaction in the context of databases?


a. A single operation
b. A sequence of operations treated as a single unit
c. A database query
d. A type of connection
Answer: b

14. Which of the following is a valid way to handle exceptions in JDBC?


a. Using try-catch blocks
b. Using throws keyword
c. Ignoring exceptions
d. Both a and b
Answer: d

15. What is the default isolation level in JDBC?


a. READ_COMMITTED
b. SERIALIZABLE
c. READ_UNCOMMITTED
d. REPEATABLE_READ
Answer: a

16. Which method is used to retrieve the data type of a column in ResultSet?
a. getType()
b. getColumnType()
c. getMetaData()
d. getColumnMetadata()
Answer: c

17. How can you insert data into a database using JDBC?
a. Using the INSERT statement
b. Using a PreparedStatement
c. Both a and b
d. None of the above
Answer: c
18. Which interface provides metadata about the database?
a. DatabaseMetaData
b. ResultSetMetaData
c. Connection
d. DriverManager
Answer: a

19. What does the close() method do in JDBC?


a. Ends the connection
b. Releases the resources held by JDBC objects
c. Commits transactions
d. Starts a transaction
Answer: b

20. Which of the following is NOT a method of the Connection interface?


a. createStatement()
b. prepareStatement()
c. executeQuery()
d. setAutoCommit()
Answer: c

21. What is the use of setAutoCommit(false)?


a. To enable transactions
b. To disable transactions
c. To enable auto-commit mode
d. To rollback changes
Answer: a

22. Which statement is used to delete data from a database?


a. REMOVE
b. DELETE
c. DROP
d. TRUNCATE
Answer: b

23. How can you prevent SQL injection attacks in JDBC?


a. Using PreparedStatement
b. Using Statement
c. Sanitizing inputs
d. Both a and c
Answer: d

24. What is the purpose of the executeBatch() method?


a. To execute multiple SQL statements at once
b. To retrieve multiple ResultSets
c. To perform transactions
d. To close statements
Answer: a

25. Which of the following methods is used to retrieve a String from ResultSet?
a. getString()
b. retrieveString()
c. fetchString()
d. readString()
Answer: a

26. Which of the following exceptions is thrown when a database access error occurs?
a. SQLException
b. IOException
c. NullPointerException
d. ClassNotFoundException
Answer: a

27. What does the method getConnection() require as parameters?


a. URL and username
b. URL, username, and password
c. Driver name
d. None of the above
Answer: b

28. What is the main purpose of using transactions in databases?


a. To improve performance
b. To ensure data integrity
c. To make data retrieval faster
d. To reduce database size
Answer: b

29. Which keyword is used to create a new table in SQL?


a. CREATE
b. ADD
c. NEW
d. INSERT
Answer: a

30. What is the role of the DriverManager class?


a. To manage database connections
b. To load JDBC drivers
c. To execute SQL statements
d. Both a and b
Answer: d

31. Which of the following is a valid SQL query to select all columns from a table named
"employees"?
a. SELECT * FROM employees
b. GET * FROM employees
c. FIND * IN employees
d. ALL FROM employees
Answer: a

32. What does the getAutoCommit() method return?


a. The current transaction status
b. Whether auto-commit mode is enabled
c. The current isolation level
d. None of the above
Answer: b

33. Which method retrieves the number of columns in a ResultSet?


a. getColumnCount()
b. getNumberOfColumns()
c. getMetaData()
d. columnCount()
Answer: c

34. What happens when you call the rollback() method?


a. It commits the current transaction
b. It undoes changes made in the current transaction
c. It closes the connection
d. It resets the database
Answer: b
35. Which interface provides access to the result set of a query?
a. ResultSet
b. Statement
c. Connection
d. DatabaseMetaData
Answer: a

36. How do you handle transactions in JDBC?


a. Using commit() and rollback()
b. Using beginTransaction()
c. Using executeTransaction()
d. Transactions are not supported in JDBC
Answer: a

37. Which SQL command is used to modify an existing record?


a. CHANGE
b. UPDATE
c. MODIFY
d. ALTER
Answer: b

38. What is the primary use of the CallableStatement interface?


a. To execute regular SQL statements
b. To execute stored procedures
c. To manage transactions
d. To create tables
Answer: b

39. Which type of SQL statement retrieves data from a database?


a. INSERT
b. DELETE
c. SELECT
d. UPDATE
Answer: c

40. What does the getGeneratedKeys() method return?


a. The primary keys of the affected rows
b. The number of affected rows
c. The metadata of the result set
d. The SQL statement used
Answer: a

41. How do you ensure that a database connection is not left open?
a. Use a finally block to close it
b. Use a static method
c. Ignore it
d. Close it manually
Answer: a

42. Which of the following methods can be used to read a boolean value from ResultSet?
a

. getBoolean()
b. retrieveBoolean()
c. readBoolean()
d. fetchBoolean()
Answer: a

43. What is the significance of using the executeUpdate() method?


a. It executes a query that returns data
b. It executes a statement that changes data
c. It executes multiple statements
d. It retrieves metadata
Answer: b

44. Which SQL statement is used to remove a table from a database?


a. REMOVE TABLE
b. DROP TABLE
c. DELETE TABLE
d. TRUNCATE TABLE
Answer: b

45. What is an advantage of using PreparedStatement over Statement?


a. PreparedStatement can execute multiple queries
b. PreparedStatement is faster and more secure
c. PreparedStatement cannot accept parameters
d. Both a and b
Answer: b

46. What type of exceptions does JDBC primarily throw?


a. RuntimeException
b. SQLException
c. IOException
d. NullPointerException
Answer: b

47. Which method can be used to retrieve the first column of the first row in a ResultSet?
a. getFirst()
b. getObject()
c. getColumn(1)
d. get(1)
Answer: b

48. What is the use of the DatabaseMetaData interface?


a. To create database tables
b. To provide information about the database
c. To execute SQL statements
d. To manage database connections
Answer: b

49. Which keyword is used to prevent duplicate entries in a database table?


a. UNIQUE
b. DISTINCT
c. DIFFERENT
d. SINGLE
Answer: a

50. How do you obtain a ResultSet from executing a SQL query?


a. Using executeUpdate()
b. Using execute()
c. Using executeQuery()
d. Using fetch()
Answer: c

You might also like