Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

JDBC Interview Questions and Answers

Test your skills through the online practice test: JDBC Quiz Online Practice Test

Related differences

Ques 6. How can you make the connection?

To establish a connection you need to have the appropriate driver connect to the DBMS.

The following line of code illustrates the general idea:
String url = "jdbc:odbc:WithoutBook";
Connection con = DriverManager.getConnection(url, "Username", "Password");

Is it helpful? Add Comment View Comments
 

Ques 7. How can you create JDBC statements and what are they?

A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send.

For a SELECT statement, the method to use is executeQuery. For statements that create or modify tables, the method to use is executeUpdate. It takes an instance of an active connection to create a Statement object.
In the following example, we use our Connection object con to create the Statement object:
Statement stmt = con.createStatement();

Is it helpful? Add Comment View Comments
 

Ques 8. How can you retrieve data from the ResultSet?

JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object:
ResultSet rs = stmt.executeQuery("SELECT NAME,SAL FROM EMPLOYEE");
String s = rs.getString("NAME");

The method getString is invoked on the ResultSet object (rs), so getString() will retrieve (get) the value stored in the column NAME in the current row of rs. Note: rs is ResultSet variable name here.

Is it helpful? Add Comment View Comments
 

Ques 9. What are the different types of Statements?

  • Regular statement (use createStatement method)
  • prepared statement (use prepareStatement method)
  • callable statement (use prepareCall)

Is it helpful? Add Comment View Comments
 

Ques 10. How can you use PreparedStatement?

This special type of statement is derived from class Statement. If you need a Statement object to execute many times, it will normally make sense to use a PreparedStatement object instead. 

The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement's SQL statement without having to compile it first.
PreparedStatement updateSales = con.prepareStatement("UPDATE EMPLOYEE SET SAL=? WHERE EMP_ID=?");

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook
JDBC vs HibernateJDBC vs JPAJDBC 3.0 vs JDBC 4.0