In this post i am sharing Java Data Base Connectivity Interview Questions. These questions are very useful for freshers and experienced candidates as well. Let us look what questions they are.
1) What is JDBC?
Ans: Java Data Base Connectivity(JDBC) is a Java API that is used to connect and execute query to the database. JDBC API uses JDBC drivers to connects to the database.
2) What are JDBC 3.0 new features?
Ans:
1.Transaction Savepoint support: Added the Savepoint interface, which contains new methods to set, release, or roll back a transaction to designated savepoints.
2. Reuse of prepared statements by connection pools: - to control how prepared statements are pooled and reused by connections.
3. Connection pool configuration :- Defined a number of properties for the ConnectionPoolDataSource interface.These properties can be used to describe how PooledConnection objects created by DataSource objects should be pooled.
4. Retrieval of parameter metadata: - Added the interface ParameterMetaData, which describes the number, type and properties of parameters to prepared statements.
5. Retrieval of auto-generated keys: - Added a means of retrieving values from columns containing automatically generated values.
6. Multiple open ResultSet objects: - Added the new method getMoreResults(int).
7. BOOLEAN data type: - Added the data type java.sql.Types.BOOLEAN. BOOLEAN is logically equivalent to BIT.
8. Making internal updates to the data in Blob and Clob objects: - Added methods to allow the data contained in Blob and Clob objects to be altered.
9. Retrieving and updating the object referenced by a Ref object: - Added methods to retrieve the object referenced by a Ref object. Also added the ability to update a referenced object through the Ref object.
10. Updating of columns containing BLOB, CLOB, ARRAY and REF types: - Added of the updateBlob, updateClob, updateArray, and updateRef methods to the ResultSet interface.
3) What are the JDBC Drivers?
Ans: JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers they are:
1)JDBC-ODBC bridge driver
2)Native-API driver(partially java driver)
3)Network Protocol driver(All java driver)
4)Thin driver(pure java driver)
4) What are the steps to connect to the database in Java?
Ans: The following are the steps to connect to the database in Java:
Step 1: First thing is using jdbc you have to establish a connection to the data base that means Registering the driver class. You must load the JDBC driver.
Step 2: After loading the JDBC driver then make a connection that means creating connection,to do this we can call the getConnection() method of driver manager class.
step 3: To execute any sql commands using jdbc connection you must first create a statement object to create this call
statement st = con.createSteatement().
This is done by calling the createStatement() method in connection interface.
Step 4:
Once the statement is created you can executed it by calling execute() method of the statement interface. That means Executing queries.
Step 5: Closing connection.
5) What are the JDBC API componenets?
Ans: The java.sql package contains interfaces and classes for JDBC API. They are
interfaces:
1)connection
2)Statement
3)preparedStatement
4)ResultSet
5)ResultSetMetaData
6)DabaseMetaData
7)CallableStatement
etc......
classes:
Driver Manager
Blob
Clob
SQLException etc...
6) What is statement?
Ans: Statement acts like vehicle through which SQL commands can be sent. Through the connection object we create statement kind of objects.
7)Why you need JDBC if ODBC is available?
Ans: ODBC is purely written in “c” so we cannot directly connect with java. JDBC is a low level pure java API used to execute SQL statements. (i) ODBC is not appropriate for direct use from java because it uses “c” interfaces. Calls from java to native “c” code has number of drawbacks in the security, implementation and robustness.
8)Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?
Ans: No. You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge.
9)Is the JDBC-ODBC Bridge multi-threaded?
Ans: No. The JDBC-ODBC Bridge does not support concurrent access from different threads. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC
10) What is PreparedStatement?
Ans: PreparedStatements are pre compiled statements once we compile the statements and send it to the server for later use. P.S are partially compiled statements placed at server side with placeholders. Before execution of these statements user has to supply values for place holders, it will increase performance of application.
Example:
PreparedStatement pst = con.prepareStatement("SELECT * FROM EMP WHERE deptno=?");
DataInputStream dis = new DataInputStream(“System.in”);
Int dno = Integer.ParseInt(dis.readLine());
pst.setInt(1, dno);
ResultSet rs = pst.executeQuery()
11) What are CallableStatements?
Ans: Callable Statements are used to retrieve data by invoking stored procedures, stored procedure are program units placed at data base server side for re-usability. These are used by n-number of clients.Callable statement will call a single stored procedure, they perform multiple queries and updates without network traffic.
Example:
callableStatement cst = con.prepareCall(“{CALL procedure-name(??)} ”);
DataInputStream dis = new DataInputStream(“System.in”);
Int enum = Integer.ParseInt(dis.readLine());
cst.setInt(1, enum);
cst.registerOutParameter(2, types.VARCHAR)
resultset rs = cst.execute();
In -> used to send information to the procedure.
Out -> used to retrieve information from data base.
InOut -> both.
12) What is difference between Statement and PreparedStatement interface?
Ans: In case of Statement query is compiled each time whereas PreparedStatement query is compiled only once. So performance of PreparedStatement is better than Statement.
13) What does the JDBC Connection interface?
Ans: The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that returns the instance of Statement,PreparedStatement,CallableStatement and DatabaseMetaData.
14) What is JDBC Connection pool?
Ans: When you are going to caret a pool of connection to the database. This will give access to a collection of already opened data base connections, which will reduce the time it takes to service the request and you can service “n” number of request at once.
15) How do you implement Connection pooling?
Ans: If you use an application server like WebLogic,jBoss,Tomcat.. then your application server provides the facilities to configure for connection pooling. If you are not using an application server then components like Apache Commons DBCP component can be used.
16)In which interface the methods commit() & rollback() savepoint() defined
Ans: java.sql.Connection interface
17) What is ResultSetMetaData?
Ans: It is used to find out the information of a table in a data base.
ResultSet rs = stmt.executeQuery("SELECT * FROM "+ table);
ResultSetMetaData rsmd = rs.getMetaData();
18) How can we store and retrieve images from the database?
Ans: By using PreparedStatement interface,we can store and retrieve images.
19) What is the use of blob,clob data types in JDBC?
Ans: These are used to store large amount of data into database like images,movies etc.. which are extremely large in size.
20)Why do you have to close database connections in Java?
Ans: You need to close the resultset,the statement and the connection. If the connection has come from a pool,closing it actually sends it back to the pool for reuse. We can do this in the finally {} block.
21) Why do you use a batch process?
Ans: Batch Processing allows you group related SQL statements into batch and submit them with one call to the database.
22) What does setAutoCommit() do?
Ans: 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.
23) What is need to set auto commit mode to false?
Ans:
1) What is JDBC?
Ans: Java Data Base Connectivity(JDBC) is a Java API that is used to connect and execute query to the database. JDBC API uses JDBC drivers to connects to the database.
2) What are JDBC 3.0 new features?
Ans:
1.Transaction Savepoint support: Added the Savepoint interface, which contains new methods to set, release, or roll back a transaction to designated savepoints.
2. Reuse of prepared statements by connection pools: - to control how prepared statements are pooled and reused by connections.
3. Connection pool configuration :- Defined a number of properties for the ConnectionPoolDataSource interface.These properties can be used to describe how PooledConnection objects created by DataSource objects should be pooled.
4. Retrieval of parameter metadata: - Added the interface ParameterMetaData, which describes the number, type and properties of parameters to prepared statements.
5. Retrieval of auto-generated keys: - Added a means of retrieving values from columns containing automatically generated values.
6. Multiple open ResultSet objects: - Added the new method getMoreResults(int).
7. BOOLEAN data type: - Added the data type java.sql.Types.BOOLEAN. BOOLEAN is logically equivalent to BIT.
8. Making internal updates to the data in Blob and Clob objects: - Added methods to allow the data contained in Blob and Clob objects to be altered.
9. Retrieving and updating the object referenced by a Ref object: - Added methods to retrieve the object referenced by a Ref object. Also added the ability to update a referenced object through the Ref object.
10. Updating of columns containing BLOB, CLOB, ARRAY and REF types: - Added of the updateBlob, updateClob, updateArray, and updateRef methods to the ResultSet interface.
3) What are the JDBC Drivers?
Ans: JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers they are:
1)JDBC-ODBC bridge driver
2)Native-API driver(partially java driver)
3)Network Protocol driver(All java driver)
4)Thin driver(pure java driver)
4) What are the steps to connect to the database in Java?
Ans: The following are the steps to connect to the database in Java:
Step 1: First thing is using jdbc you have to establish a connection to the data base that means Registering the driver class. You must load the JDBC driver.
Step 2: After loading the JDBC driver then make a connection that means creating connection,to do this we can call the getConnection() method of driver manager class.
step 3: To execute any sql commands using jdbc connection you must first create a statement object to create this call
statement st = con.createSteatement().
This is done by calling the createStatement() method in connection interface.
Step 4:
Once the statement is created you can executed it by calling execute() method of the statement interface. That means Executing queries.
Step 5: Closing connection.
5) What are the JDBC API componenets?
Ans: The java.sql package contains interfaces and classes for JDBC API. They are
interfaces:
1)connection
2)Statement
3)preparedStatement
4)ResultSet
5)ResultSetMetaData
6)DabaseMetaData
7)CallableStatement
etc......
classes:
Driver Manager
Blob
Clob
SQLException etc...
6) What is statement?
Ans: Statement acts like vehicle through which SQL commands can be sent. Through the connection object we create statement kind of objects.
7)Why you need JDBC if ODBC is available?
Ans: ODBC is purely written in “c” so we cannot directly connect with java. JDBC is a low level pure java API used to execute SQL statements. (i) ODBC is not appropriate for direct use from java because it uses “c” interfaces. Calls from java to native “c” code has number of drawbacks in the security, implementation and robustness.
8)Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?
Ans: No. You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge.
9)Is the JDBC-ODBC Bridge multi-threaded?
Ans: No. The JDBC-ODBC Bridge does not support concurrent access from different threads. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC
10) What is PreparedStatement?
Ans: PreparedStatements are pre compiled statements once we compile the statements and send it to the server for later use. P.S are partially compiled statements placed at server side with placeholders. Before execution of these statements user has to supply values for place holders, it will increase performance of application.
Example:
PreparedStatement pst = con.prepareStatement("SELECT * FROM EMP WHERE deptno=?");
DataInputStream dis = new DataInputStream(“System.in”);
Int dno = Integer.ParseInt(dis.readLine());
pst.setInt(1, dno);
ResultSet rs = pst.executeQuery()
11) What are CallableStatements?
Ans: Callable Statements are used to retrieve data by invoking stored procedures, stored procedure are program units placed at data base server side for re-usability. These are used by n-number of clients.Callable statement will call a single stored procedure, they perform multiple queries and updates without network traffic.
Example:
callableStatement cst = con.prepareCall(“{CALL procedure-name(??)} ”);
DataInputStream dis = new DataInputStream(“System.in”);
Int enum = Integer.ParseInt(dis.readLine());
cst.setInt(1, enum);
cst.registerOutParameter(2, types.VARCHAR)
resultset rs = cst.execute();
In -> used to send information to the procedure.
Out -> used to retrieve information from data base.
InOut -> both.
12) What is difference between Statement and PreparedStatement interface?
Ans: In case of Statement query is compiled each time whereas PreparedStatement query is compiled only once. So performance of PreparedStatement is better than Statement.
13) What does the JDBC Connection interface?
Ans: The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that returns the instance of Statement,PreparedStatement,CallableStatement and DatabaseMetaData.
14) What is JDBC Connection pool?
Ans: When you are going to caret a pool of connection to the database. This will give access to a collection of already opened data base connections, which will reduce the time it takes to service the request and you can service “n” number of request at once.
15) How do you implement Connection pooling?
Ans: If you use an application server like WebLogic,jBoss,Tomcat.. then your application server provides the facilities to configure for connection pooling. If you are not using an application server then components like Apache Commons DBCP component can be used.
16)In which interface the methods commit() & rollback() savepoint() defined
Ans: java.sql.Connection interface
17) What is ResultSetMetaData?
Ans: It is used to find out the information of a table in a data base.
ResultSet rs = stmt.executeQuery("SELECT * FROM "+ table);
ResultSetMetaData rsmd = rs.getMetaData();
18) How can we store and retrieve images from the database?
Ans: By using PreparedStatement interface,we can store and retrieve images.
19) What is the use of blob,clob data types in JDBC?
Ans: These are used to store large amount of data into database like images,movies etc.. which are extremely large in size.
20)Why do you have to close database connections in Java?
Ans: You need to close the resultset,the statement and the connection. If the connection has come from a pool,closing it actually sends it back to the pool for reuse. We can do this in the finally {} block.
21) Why do you use a batch process?
Ans: Batch Processing allows you group related SQL statements into batch and submit them with one call to the database.
22) What does setAutoCommit() do?
Ans: 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.
23) What is need to set auto commit mode to false?
Ans:
- To increase performance
- To maintain the integrity of business processes
- To use distributed Transactions
Watch Video : Best JDBC Interview Questions for freshes
No comments:
Post a Comment