
Advanced Java Interview Questions and Answers
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
All prices
More details
Content
CHAPTER 2
JDBC Programming
2.1 The JDBC Connectivity Model
Question: What is the purpose of the setFetchSize() method in JDBC?
Answer: The setFetchSize() method specifies the number of rows to fetch from the database in a single round trip.
Question: How do you use savepoints in JDBC?
Answer: Use the setSavepoint() method to mark a point in a transaction and rollback(Savepoint savepoint) to
roll back to it.
Question: What is the purpose of DatabaseMetaData in JDBC?
Answer: DatabaseMetaData provides information about the database, such as its capabilities, tables, and drivers.
Question: What is the use of the getGeneratedKeys() method?
Answer: The getGeneratedKeys() method retrieves auto-generated keys (like primary keys) after an INSERT operation.
Question: What are row sets in JDBC?
Answer: Row sets are enhanced ResultSet objects that support features like disconnected operation and serialization.
Question: How do you perform a batch update in JDBC?
Answer: Add multiple SQL statements using addBatch() and execute them using executeBatch().
Question: What is the difference between commit() and rollback()?
Answer: commit() saves changes made in a transaction, while rollback() reverts changes.
Question: How do you check if a connection is valid in JDBC?
Answer: Use the isValid(int timeout) method of the Connection interface.
Question: What is the use of rowId in JDBC?
Answer: RowId represents a unique identifier for a row in the database and can be retrieved using getRowId().
Question: What is a Scrollable ResultSet?
Answer: A Scrollable ResultSet allows you to navigate forward and backward through the query results.
Question: How do you retrieve metadata about a ResultSet?
Answer: Use the getMetaData() method of the ResultSet object.
Question: What are the different types of statements in JDBC?
Answer: The types are Statement, PreparedStatement, and CallableStatement.
Question: How do you update data in a ResultSet?
Answer: Use ResultSet methods like updateString(), updateInt(), and updateRow() to modify data.
Question: What is connection pooling in JDBC?
Answer: Connection pooling is a technique to reuse database connections for better performance and resource management.
Question: How does JDBC support batch processing?
Answer: JDBC supports batch processing using the addBatch() and executeBatch() methods for bulk updates.
Question: How do you execute stored procedures in JDBC?
Answer: Use CallableStatement to execute stored procedures.
Question: What is the role of ParameterMetaData in JDBC?
Answer: ParameterMetaData provides information about parameters in a PreparedStatement.
Question: How can you retrieve database-specific keywords in JDBC?
Answer: Use the getSQLKeywords() method of the DatabaseMetaData object.
Question: How do you handle large binary data in JDBC?
Answer: Use Blob for binary data and Clob for large text data.
Question: What is JDBC 4.0's feature for auto-loading drivers?
Answer: In JDBC 4.0 and later, drivers are auto-loaded at runtime without explicitly calling Class.forName().
2.2 Creating a SQL Query
Question: How do you retrieve data from a SELECT query in JDBC?
Answer: Use ResultSet methods like next(), getString(), or getInt() to retrieve and process data row by row.
Question: How do you optimize performance when creating SQL queries in Java?
Answer: Use PreparedStatement for repeated queries, index database columns, and limit fetched rows.
Question: How do you handle null values in a SQL query?
Answer: Use IS NULL in the query or check for null in the application code before processing the data.
Question: What is the purpose of the setMaxRows() method in JDBC?
Answer: The setMaxRows() method limits the number of rows returned by a query.
Question: How do you use JOINs in a SQL query in JDBC?
Answer: Include JOIN clauses in the SQL query string and execute it using Statement or PreparedStatement.
Question: How do you perform a query with ORDER BY in JDBC?
Answer: Add an ORDER BY clause to the SQL query string before executing it.
Question: How do you perform a query with GROUP BY in JDBC?
Answer: Add a GROUP BY clause to the SQL query string and process the aggregated results using ResultSet.
Question: How do you perform a query with WHERE conditions in JDBC?
Answer: Add a WHERE clause to filter rows and use placeholders (?) for dynamic parameters.
Question: How do you use aggregate functions in a SQL query in JDBC?
Answer: Include aggregate functions like SUM(), COUNT(), AVG(), etc., in the query string.
Question: What is the significance of the LIMIT clause in a SQL query?
Answer: The LIMIT clause restricts the number of rows returned by a query, improving performance.
Question: How do you perform a query with a subquery in JDBC?
Answer: Include a subquery within the main SQL query string and execute it using Statement or PreparedStatement.
Question: What is the role of the LIKE operator in SQL queries?
Answer: The LIKE operator is used for pattern matching in WHERE conditions.
Question: How do you execute a query with a HAVING clause in JDBC?
Answer: Add a HAVING clause to filter aggregated results and execute the query as usual.
Question: How do you combine queries using UNION in JDBC?
Answer: Construct a query with the UNION operator and execute it using Statement or PreparedStatement.
Question: How do you handle special characters in SQL queries?
Answer: Escape special characters or use PreparedStatement to avoid issues like SQL injection.
Question: What is the purpose of the DISTINCT keyword in SQL queries?
Answer: The DISTINCT keyword removes duplicate rows from the query results.
Question: How do you update multiple rows in a SQL query?
Answer: Use an UPDATE query with a WHERE condition that matches multiple rows.
Question: How do you delete rows using a SQL query in JDBC?
Answer: Use a DELETE statement with a WHERE condition and execute it using executeUpdate().
Question: How do you insert multiple rows in a SQL query?
Answer: Use a single INSERT statement with multiple values or batch processing for better performance.
Question: How do you create a table using a SQL query in JDBC?
Answer: Use a CREATE TABLE statement and execute it using the execute() method.
2.3 Getting the Results
Question: What is the default cursor behavior of a ResultSet?
Answer: By default, a ResultSet is TYPE_FORWARD_ONLY and read-only.
Question: What is the difference between relative(int rows) and absolute(int row) in ResultSet?
Answer: relative() moves the cursor relative to its current position, while absolute() moves it to an absolute row number.
Question: How do you check the type of a ResultSet?
Answer: Use the getType() method to determine if the ResultSet is forward-only or scrollable.
Question: Can you navigate a ResultSet without rows?
Answer: No, attempting to navigate a ResultSet with no rows will cause the cursor methods to return false.
Question: How do you retrieve a timestamp from a ResultSet?
Answer: Use the getTimestamp() method to retrieve a SQL TIMESTAMP...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.