Skip to Content, PHP, MySQL, Javascript, Ajax, Htacces

JDBC Interview Questions and Answers For Freshers Part-4

Be First!

jdbc31.How do you handle SQL NULL values in Java?
SQL’s use of NULL values and Java’s use of null are different concepts. There are three tactics you can use

Avoid using getXXX( ) methods that return primitive data types.

Use wrapper classes for primitive data types, and use the ResultSet object’s wasNull( ) method to test whether the wrapper class variable that received the value returned by the getXXX( ) method should be set to null.

Use primitive data types and the ResultSet object’s wasNull( ) method to test whether the primitive variable that received the value returned by the getXXX( ) method should be set to an acceptable value that you’ve chosen to represent a NULL.

32.What does setAutoCommit do?
When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. By setting auto-commit to false no SQL statements will be committed until you explicitly call the commit method.

33.Why will you set auto commit mode to false?
Following are the reasons

To increase performance.

To maintain the integrity of business processes.

To use distributed transactions.

34.What is SavePoint? Give an example.
A savepoint marks a point that the current transaction can roll back to. Instead of rolling all of its changes back, it can choose to roll back only some of them. For example, suppose you

start a transaction.

insert 10 rows into a table.

set a savepoint.

insert another 5 rows.

rollback to the savepoint.

commit the transaction.

After doing this, the table will contain the first 10 rows you inserted. The other 5 rows will have been deleted by the rollback. A savepoint is just a marker that the current transaction can roll back to.

35.What are SQL warnings?
SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do. They simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method.

36.Why would you use a batch process?
Batch Processing allows you to group related SQL statements into a batch and submit them with one call to the database.

37.What are the steps followed to create a batch process?
Typical sequences of steps to use Batch Processing with Statement or PrepareStatement Object are

In case of Batch processing using PrepareStatement object, create SQL statements with placeholders.

Create a Statement or PrepareStatement object using either createStatement() or prepareStatement() methods respectively.

Set auto-commit to false using setAutoCommit().

Add as many as SQL statements you like into batch using addBatch() method on created statement object.

Execute all the SQL statements using executeBatch() method on created statement object.

Finally, commit all the changes using commit() method.

38.What is a Stored Procedure and how do you call it in JDBC?
A stored procedure is a group of SQL statements that form a logical unit and perform a particular task. For example operations on an employee database (hire, fire, promote, lookup) could be coded as stored procedures executed by application code. Stored procedures can be called using CallableStatement class in JDBC API. For example the following code demonstrates this

CallableStatement cs = con.prepareCall(“{call MY_SAMPLE_STORED_PROC}”);
ResultSet rs = cs.executeQuery();

39.What is JDBC SQL escape syntax?
The escape syntax gives you the flexibility to use database specific features unavailable to you by using standard JDBC methods and properties.

The general SQL escape syntax format is as follows

{keyword ‘parameters’}.
JDBC defines escape sequences that contain the standard syntax for the following language features

Date, time, and timestamp literals (d, t, ts Keywords).

Scalar functions such as numeric, string, and data type conversion functions(fn Keyword).

Outer joins(oj Keyword)

Escape characters for wildcards used in LIKE clauses(escape Keyword).

Procedure calls(call Keyword).

40.What is a transaction?
A transaction is a logical unit of work. To complete a logical unit of work, several actions may need to be taken against a database. Transactions are used to provide data integrity, correct application semantics, and a consistent view of data during concurrent access.



Leave a Reply