JDBC Interview Questions and Answers
Question - 101 : - What are the types of ResultSet?
Answer - 101 : -
There are 3 types in ResultSet. These are:
- TYPE_FORWARD_ONLY: It is the default option. The cursor will move from start to end.
- TYPE_SCROLL_INSENSITIVE: In this type, the cursor will move in both forward and backward directions. Dataset has the data when the SQL query returns the data.
- TYPE_SCROLL_SENSITIVE: It is the same as TYPE_SCROLL_INSENSITIVE, the difference is that it will have the updated data while iterating the resultset object.
Question - 102 : - What are the concurrency modes in ResultSet?
Answer - 102 : -
There are 2 different modes of Concurrency in ResultSet. They are:
- ResultSet.CONCUR_READ_ONLY: It is the default concurrency mode. A read-only option is available. Updation is not possible.
- ResultSet.CONCUR_UPDATABLE: Updation is possible.
Question - 103 : - How to check whether the database supports the concurrency mode?
Answer - 103 : - We have the supportsResultSetConcurrency() method which will be used to check whether the given type and concurrency modes are supported by the database or not.
Question - 104 : - Can we get the data of the particular row from the resultset?
Answer - 104 : - Yes, we can get the data of the particular row from the resultSet using the relative() method. It will move the cursor to the given row either in a forward or in a backward direction from the current row. If the positive value has been given, it will move in the forward direction. If the negative value has been given, it will move in the backward direction.
Question - 105 : - What is the use of the getter and setter methods in ResultSet?
Answer - 105 : -
Getter methods: These are used to retrieve the values of the particular column of the table from ResultSet. Either the Column Index value or Column Name should be passed as a parameter. Normally, we will represent the getter method as getXXX() methods.
Example:
- int getInt(string Column_Name): It is used to retrieve the value of the specified column Index and int data type as a return type.
Setter Methods: We can set the value in the database using ResultSet setter methods. It is similar to getter methods, but here we need to pass the values/data for the particular column to insert into the database and the index value or column name of that column. Normally we will represent the setter method as setXXX() methods.
Example:
- void setInt(int Column_Index, int Data_Value): It is used to insert the value of the specified column Index with an int value.
Question - 106 : - What is the main purpose of the ResultSetMetaData interface?
Answer - 106 : -
This interface gives more information about ResultSet. Each ResultSet object has been associated with one ResultSetMetaData object.
This object will have the details of the properties of the columns like datatype of the column, column name, the number of columns in that table, table name, schema name, etc., getMetaData() method of ResultSet object is used to create the ResultSetMetaData object.
Syntax:
PreparedStatement pstmntobj = conn.prepareStatement(insert_query);
ResultSet resultsetobj = pstmntobj.executeQuery(“Select * from EMPLOYEE”);
ResultSetMetaData rsmd obj= resultsetobj.getMetaData();
Question - 107 : - What is DatabaseMetaData?
Answer - 107 : - The DatabaseMetaData interface gives information about the Database we are using. We will get the following information – DatabaseName, Database version, and so on.
Question - 108 : - What is ACID property?
Answer - 108 : -
- A–Atomicity -> If all the queries have executed successfully, then the data will be committed else won’t commit.
- C–Consistency -> Data should be consistent after any transaction.
- I–Isolation -> Each transaction should be isolated.
- D–Durability -> If the transaction is committed once, it should be available always (if no changes have happened)
Question - 109 : - How to change the auto-commit mode value?
Answer - 109 : - By default, the value of AutoCommit is TRUE. After the execution of the SQL statement, it will be committed automatically. Using the setAutoCommit() method, we can change the value to AutoCommit.
Question - 110 : - What is the use of Commit and Rollback methods?
Answer - 110 : -
Commit() method: We have the commit() method in Java to commit the data. Once the SQL execution is done, we can call the commit method.
Syntax: connectionobj.commit();
Rollback() method: We have the rollback() method in Java to rollback the data. Rollback means to undo the changes. If any of the SQL statements are failed, we can call the rollback method to undo the changes.
Syntax: connectionobj.rollback();