0% found this document useful (0 votes)
3 views

JDBC1

Uploaded by

Reyan Dabre
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JDBC1

Uploaded by

Reyan Dabre
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1. What does JDBC stand for?

Answer: Java Database Connectivity

2. What is the primary purpose of JDBC?

Answer: To connect and execute queries with a database from Java


applications.

3. Which package contains the JDBC classes and interfaces?

Answer: java.sql

4. What is a JDBC Driver?

Answer: A software component that enables Java applications to interact


with a database.

5. Name the four types of JDBC drivers.

Answer: Type 1 (JDBC-ODBC Bridge), Type 2 (Native-API driver), Type 3


(Network Protocol driver), Type 4 (Thin driver).

6. What is a Connection object in JDBC?

Answer: It represents a connection to a specific database.

7. How do you create a Connection object?

Answer: By calling DriverManager.getConnection() method.

8. What is a Statement object?

Answer: An object used to execute SQL queries against a database.

9. What method is used to execute a SQL query with a Statement?

Answer: executeQuery() for SELECT statements and executeUpdate() for


INSERT, UPDATE, DELETE statements.
10. What is a ResultSet in JDBC?

Answer: An object that holds the data returned by executing a SQL query.

11. How can you iterate through a ResultSet?

Answer: By using a while loop and the next() method.

12. What is a PreparedStatement?

Answer: A precompiled SQL statement that can be executed multiple


times with different parameters.

13. How do you set parameters in a PreparedStatement?

Answer: By using setter methods like setInt(), setString(), etc.

14. What is a CallableStatement?

Answer: An interface that allows you to call stored procedures in a


database.

15. What is the purpose of the DriverManager class?

Answer: To manage a list of database drivers and establish connections.

16. How do you register a JDBC driver?

Answer: By calling Class.forName() with the driver class name.

17. What is the difference between executeQuery() and executeUpdate()?

Answer: executeQuery() is used for SELECT statements, while


executeUpdate() is for INSERT, UPDATE, or DELETE.

18. How do you handle SQL exceptions in JDBC?

Answer: By using try-catch blocks to catch SQLException.


19. What is a transaction in JDBC?

Answer: A sequence of one or more SQL operations that are executed as a


single unit.

20. How do you begin a transaction in JDBC?

Answer: By setting auto-commit to false using conn.setAutoCommit(false).

21. How do you commit a transaction in JDBC?

Answer: By calling conn.commit().

22. How do you roll back a transaction in JDBC?

Answer: By calling conn.rollback().

23. What is connection pooling?

Answer: A technique to manage a pool of database connections for


improved performance.

24. What interface provides support for connection pooling in JDBC?

Answer: DataSource

25. How do you retrieve metadata about a database in JDBC?

Answer: By using DatabaseMetaData interface.

26. What method retrieves the database URL from a Connection?

Answer: getMetaData().getURL()

27. What is the purpose of the setAutoCommit method?

Answer: To enable or disable the auto-commit mode for a Connection.


28. What is the significance of closing the ResultSet, Statement, and
Connection objects?

Answer: To release database resources and avoid memory leaks.

29. How can you close JDBC resources effectively?

Answer: By using a try-with-resources statement.

30. What is batch processing in JDBC?

Answer: A technique to execute multiple SQL statements in a single batch


for improved performance.

31. What is the role of the DriverManager.getConnection() method?

Answer: It establishes a connection to the specified database using a


given URL and credentials.

32. How can you specify the database URL in JDBC?

Answer: By providing a connection string formatted as


"jdbc:subprotocol:subname".

33. What are the common JDBC subprotocols?

Answer: MySQL, Oracle, PostgreSQL, SQL Server, etc.

34. How do you retrieve the number of rows affected by a SQL statement?

Answer: By using the executeUpdate() method, which returns an integer


count.

35. What is the use of the ResultSetMetaData interface?

Answer: To retrieve information about the types and properties of the


columns in a ResultSet.

36. How can you determine the number of columns in a ResultSet?


Answer: By calling getColumnCount() on a ResultSetMetaData object.

37. What is the purpose of the BatchUpdateException?

Answer: To provide information about an exception that occurs during


batch processing of SQL statements.

38. How do you handle multiple SQL commands in a batch?

Answer: By adding commands to a Statement or PreparedStatement using


the addBatch() method.

39. What is the significance of the setFetchSize method?

Answer: It determines the number of rows fetched from the database in a


single round trip.

40. How can you handle multiple ResultSets returned by a stored


procedure?

Answer: By using the getMoreResults() method of the CallableStatement.

41. What is the purpose of the Connection.setReadOnly() method?

Answer: To indicate that the connection should be read-only, which can


optimize performance.

42. How can you get the current transaction isolation level?

Answer: By calling getTransactionIsolation() on the Connection object.

43. What are the standard transaction isolation levels in JDBC?

Answer: READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ,


SERIALIZABLE.

44. What is the use of the savepoint in JDBC?

Answer: To create a point in a transaction to which you can later roll back.
45. How do you create a savepoint?

Answer: By calling conn.setSavepoint().

46. What is the role of the DataSource interface in JDBC?

Answer: It provides an alternative to DriverManager for establishing


connections and supports connection pooling.

47. How can you configure a DataSource for connection pooling?

Answer: By using a connection pool implementation like Apache DBCP or


HikariCP.

48. What is the purpose of using the close() method on a Connection?

Answer: To release the connection and free up resources.

49. How do you retrieve a specific column value from a ResultSet?

Answer: By using getString(), getInt(), or appropriate getter methods with


the column index or name.

50. What is a SQL Injection, and how can JDBC help prevent it?

Answer: A SQL injection is a code injection technique; using


PreparedStatement helps prevent it by separating SQL logic from data.

51. What is the purpose of the Connection.setCatalog() method?

Answer: To set the current database catalog for the connection.

52. How do you execute a stored procedure in JDBC?

Answer: By using a CallableStatement and the appropriate SQL syntax.

53. What is a warning in JDBC?


Answer: It provides information about issues that may not stop execution,
such as potential performance problems.

54. How can you access warnings from a Statement?

Answer: By calling getWarnings() on the Statement object.

55. What is the difference between getString() and getObject() in


ResultSet?

Answer: getString() retrieves a column value as a String, while getObject()


retrieves it as an Object type.

56. What is the role of the Connection.getMetaData() method?

Answer: To obtain database metadata about the connection, including


information about the database and its capabilities.

57. How can you check if a ResultSet contains any data?

Answer: By using the next() method; it returns false if there are no rows.

58. What is the purpose of Connection.rollback()?

Answer: To undo all changes made in the current transaction since the last
commit.

59. What happens if you call commit() without a prior call to


setAutoCommit(false)?

Answer: It throws an SQLException because there's no transaction to


commit.

60. How can you ensure that resources are closed in JDBC?

Answer: By using try-with-resources statements or explicitly closing them


in a finally block.

61. What is the purpose of the Class.forName() method in JDBC?


Answer: It loads the JDBC driver class into memory, enabling the
application to use it.

62. How do you establish a connection to a database using a DataSource?

Answer: By calling getConnection() on the DataSource object.

63. What are the main advantages of using a PreparedStatement over a


Statement?

Answer: PreparedStatement offers better performance, protection against


SQL injection, and support for parameterized queries.

64. What is the significance of the execute() method in JDBC?

Answer: It can execute any SQL statement and returns a boolean


indicating whether it returns a ResultSet.

65. How do you retrieve the value of a specific column by name in a


ResultSet?

Answer: By using the getString(columnName) or appropriate getter


method.

66. What is the purpose of the Connection.createStatement() method?

Answer: To create a Statement object for executing SQL queries.

67. How do you enable transaction management in JDBC?

Answer: By setting auto-commit to false and managing commits and


rollbacks manually.

68. What is the role of the Statement.setQueryTimeout() method?

Answer: It sets the maximum time in seconds that a statement can run
before timing out.

69. How can you perform batch updates using a PreparedStatement?


Answer: By adding multiple sets of parameters with addBatch() and then
executing them with executeBatch().

70. What happens if you attempt to use a closed Connection?

Answer: It throws an SQLException.

71. How do you get a Connection object's metadata?

Answer: By calling getMetaData() on the Connection object.

72. What is the purpose of the Connection.setHoldability() method?

Answer: To specify how the ResultSet behaves when the transaction is


committed.

73. What is the difference between the executeBatch() and


executeUpdate() methods?

Answer: executeBatch() executes multiple SQL statements as a batch,


while executeUpdate() executes a single SQL statement.

74. How can you handle multiple ResultSets returned by a


CallableStatement?

Answer: By calling getMoreResults() in a loop until there are no more


results.

75. What does the term "data source" refer to in JDBC?

Answer: A data source is a factory for connections to a specific database.

76. What is the purpose of the Connection.getSchema() method?

Answer: To retrieve the current schema name of the connection.

77. How do you check the status of a ResultSet in JDBC?


Answer: By checking if it is closed using the isClosed() method (if available
in the implementation).

78. What are SQL types in JDBC, and how do they relate to Java types?

Answer: SQL types define the data types in SQL; JDBC provides mappings
to Java types (e.g., INTEGER maps to int).

79. How can you use a ResultSet in a loop efficiently?

Answer: By calling next() to iterate through the rows until it returns false.

80. What is the purpose of the Connection.setTransactionIsolation()


method?

Answer: To set the isolation level for the current transaction.

81. What does the Connection.getAutoCommit() method return?

Answer: It returns true if auto-commit is enabled, otherwise false.

82. How do you close multiple JDBC resources effectively?

Answer: By using try-with-resources for each resource that implements


AutoCloseable.

83. What is a SQL exception?

Answer: An exception that provides information about database access


errors or other errors related to SQL operations.

84. How can you convert a ResultSet to a List in Java?

Answer: By iterating through the ResultSet and adding the values to a List.

85. What is the significance of using a schema in a database?

Answer: A schema organizes database objects, providing a namespace


and structure for data.
86. How do you create a custom JDBC driver?

Answer: By implementing the required JDBC interfaces and classes defined


in the JDBC API.

87. What is the role of the SQLException class in JDBC?

Answer: It encapsulates any SQL-related errors that occur during database


access.

88. How can you retrieve the database product name using JDBC?

Answer: By calling getDatabaseProductName() on the DatabaseMetaData


object.

89. What is a Connection Listener in JDBC?

Answer: A component that monitors and responds to events related to


database connection changes.

90. How can you implement pagination in JDBC queries?

Answer: By using the LIMIT and OFFSET clauses in SQL or setting the fetch
size in the ResultSet.

91. What is a PreparedStatement in Java?

Answer: A Java interface that allows you to execute parameterized SQL


queries.

92. How does PreparedStatement prevent SQL injection?

Answer: By using parameterized queries, it separates SQL code from data.

93. What method is used to create a PreparedStatement?

Answer: The `prepareStatement(String sql)` method of the `Connection`


interface.
94. How do you set a String parameter in a PreparedStatement?

Answer: By using the `setString(int parameterIndex, String x)` method.

95. Can PreparedStatement be used for executing multiple queries?

Answer: Yes, it can be reused for executing multiple queries with different
parameters.

96. What is the benefit of using PreparedStatement over Statement?

Answer: PreparedStatement is more efficient and secure for executing


repeated queries.

97. How do you execute a PreparedStatement?

Answer: By using the `executeQuery()` or `executeUpdate()` method.

98. What happens if you try to execute a PreparedStatement with


uninitialized parameters?

Answer: An `SQLException` will be thrown due to missing parameters.

99. Can PreparedStatement be used for batch processing?

Answer: Yes, it can be used for batch processing with the `addBatch()`
method.

100. How do you close a PreparedStatement?

Answer: By calling the `close()` method to release database resources.

101. What is ResultSetMetaData in Java?

Answer: An interface that provides metadata about the columns in a


ResultSet.

102. How do you obtain ResultSetMetaData from a ResultSet?


Answer: By calling the `getMetaData()` method on a ResultSet object.

103. What method would you use to get the number of columns in a
ResultSet?

Answer: The `getColumnCount()` method of the ResultSetMetaData


interface.

104. How can you find the name of a specific column in a ResultSet?

Answer: By using the `getColumnName(int column)` method.

105. What does the `isNullable(int column)` method return?

Answer: It returns an integer indicating whether the specified column can


accept null values.

106. How do you determine the data type of a column in a ResultSet?

Answer: By using the `getColumnType(int column)` method.

107. Can ResultSetMetaData provide information about column display


size?

Answer: Yes, using the `getColumnDisplaySize(int column)` method.

108. What does the `getColumnTypeName(int column)` method return?

Answer: It returns the SQL type name of the specified column.

109. How can you check if a column is an auto-increment column?

Answer: By using the `isAutoIncrement(int column)` method.

110. Can you use ResultSetMetaData to get the catalog name of a


column?

Answer: Yes, using the `getCatalogName(int column)` method.


111. What is a ResultSet in Java?

Answer: An interface that represents a table of data retrieved from a


database.

112. How do you create a ResultSet object?

Answer: By executing a SQL query using the `Statement` or


`PreparedStatement` interface.

113. What method is used to move the cursor to the first row of a
ResultSet?

Answer: The `first()` method.

114. How can you check if there are more rows in a ResultSet?

Answer: By using the `next()` method, which returns true if there is a next
row.

115. How do you retrieve an integer value from a ResultSet?

Answer: By using the `getInt(int columnIndex)` method.

116. Can you update data in a ResultSet?

Answer: Yes, if the ResultSet is created with the appropriate concurrency


settings.

117. What method is used to close a ResultSet?

Answer: The `close()` method.

118. How do you get the value of a column as a String from a ResultSet?

Answer: By using the `getString(int columnIndex)` method.

119. What is the purpose of the `absolute(int row)` method in ResultSet?

Answer: It moves the cursor to a specified row number in the ResultSet.


120. Can ResultSet be used to scroll through rows in both directions?

Answer: Yes, if it is created with a type of `TYPE_SCROLL_INSENSITIVE` or


`TYPE_SCROLL_SENSITIVE`.

121. What is a CallableStatement in Java?

Answer: An interface that allows you to execute SQL stored procedures.

122. How do you create a CallableStatement object?

Answer: By using the `prepareCall(String sql)` method of the `Connection`


interface.

123. How can you register an output parameter in a CallableStatement?

Answer: By using the `registerOutParameter(int parameterIndex, int


sqlType)` method.

124. How do you execute a CallableStatement?

Answer: By using the `execute()` or `executeUpdate()` method.

125. Can CallableStatement handle both input and output parameters?

Answer: Yes, it can handle both using input parameters and output
parameters.

You might also like