InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How to create a table dynamically from a JDBC application? |
|
Answer» We can dynamically create a table by using the FOLLOWING code: //import sectionimport java.sql.*;import java.io.*;public class CreateTableEx{ public static void main(String[] args)throws Exception { //create an objet of buffered reader BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //load and register the driver Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); //ESTABLISH a connection Connection con = DriverManager.getConnection(“jdbc:odbc:nag”,”system”,”naveen”); //create a statement object Statement ST = con.createStatement(); //receive dynamic input as a table name System.out.println(“Enter table name”); String tablename = br.readLine(); //execute SQL query St.executeUpdate("create table"+tablename+"(empno number,EMPNAME varchar2(10),empsalary number,empaddress varchar2(20))"); System.out.println(“Successfully CREATED the table”); //close the connection con.close(); }} |
|
| 2. |
What are the isolation levels of connections in JDBC? |
Answer»
|
|
| 3. |
What is Two phase commit in JDBC? |
Answer»
|
|
| 4. |
Give few examples of most common exceptions in JDBC. |
|
Answer» Some of the most common JDBC exceptions are GIVEN below:
|
|
| 5. |
Explain the methods available for Transaction Management in JDBC. |
|
Answer» The connection interface is having 5 methods for transaction management. They are given below:
|
|
| 6. |
Explain the benefits of PreparedStatement over Statement. |
|
Answer» Benefits of PREPAREDSTATEMENT over Statement interface are:
|
|
| 7. |
What is JDBC Transaction Management and why is it needed? |
Answer»
|
|
| 8. |
What are the types of JDBC architecture? |
|
Answer» JDBC supports 2 types of processing MODELS to access the DATABASE. They are:
|
|
| 9. |
How to use JDBC API to call Stored procedures? |
|
Answer» Stored procedures are a set of SQL queries that are compiled in the DATABASE and will be executed from JDBC API. For executing Stored procedures in the database, JDBC CallableStatement can be used. The SYNTAX for initializing a CallableStatement is: CallableStatement cs = con.prepareCall("{call insertEmployee(?,?,?,?,?)}");stmt.setInt(1, id);stmt.setString(2, name);stmt.setString(3, role);stmt.setString(4, address);stmt.setString(5, salary);//registering the OUT PARAMETER before calling the stored procedurecs.registerOutParameter(5, java.sql.Types.VARCHAR); cs.executeUpdate();We must register the OUT parameters before executing the CallableStatement. |
|
| 10. |
What is JDBC Connection? Explain steps to get JDBC database connection in a simple Java program. |
|
Answer» Loading the driver: At first, you need to load or register the driver before using it in the program. Registration must be done once in your program. You can register a driver by using any one of the two methods mentioned below:
The below given example uses Class.forName() to load the Oracle driver: Class.forName(“oracle.jdbc.driver.OracleDriver”);The MySQL Connector/J version 8.0 library comes with a JDBC driver class: com.mysql.jdbc.Driver. Before Java 6, we had to load the driver explicitly using the statement given below: Class.forName("com.mysql.jdbc.Driver");However, this statement is no longer needed, because of a new update in JDBC 4.0 that comes from Java 6. As long as you place the MySQL JDBC driver JAR file into the classpath of your program, the driver manager can find and load the driver.
The below given example uses DriverManager.registerDriver() to register the Oracle driver: DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());For registering the MySQL driver, use the below-given CODE: DriverManager.registerDriver(new com.mysql.jdbc.Driver(); );Create the connections:
Here, con: Reference to a Connection interface. url: Uniform Resource Locator. user: Username from which SQL command prompt is accessed. password: Password from which SQL command prompt is accessed.
Where oracle represents the database used, thin is the driver used, @localhost is the IP(Internet Protocol) address where the database is stored, 1521 is the PORT number and xe represents the service provider. All 3 parameters given above are of string type and are expected to be declared by the programmer before the function call. Use of this can be REFERRED from the final code of an application.
Where localhost represents hostname or IP address of the MySQL server, 3306 port number of the server and by default, it is 3306, test1 is the name of the database on the server. Create a statement:
Here, con is a reference to the Connection interface used in the earlier step. Execute the query:
Example: int m = st.executeUpdate(sql); if (m==1) System.out.println("Data inserted successfully : "+sql); else System.out.println("Data insertion failed");Here SQL is the SQL query of string type. Close the connection:
Example: con.close(); Implementation of JDBC Oracle database connection using a Java program: import java.sql.*;import java.util.*;class OracleCon{ public static void main(String a[]) { //Creating the connection String url = "jdbc:oracle:thin:@localhost:1521:xe"; String user = "system"; String password = "123"; //Entering the data Scanner k = new Scanner(System.in); System.out.println("Enter employee Id"); int empid = k.nextInt(); System.out.println("Enter employee name"); String empname = k.next(); System.out.println("Enter employee address"); String address = k.next(); //Inserting data using SQL query String sql = "insert into employee values("+empid+",'"+empname+"','"+address+"')"; Connection con=null; try { DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); //Reference to connection interface con = DriverManager.getConnection(url,user,password); Statement st = con.createStatement(); int m = st.executeUpdate(sql); if (m == 1) System.out.println("Data inserted successfully : "+sql); else System.out.println("Data insertion failed"); con.close(); } catch(Exception ex) { System.err.println(ex); } }}Implementation of JDBC MySQL database connection using Java program: import java.sql.*; class MysqlCon{ public static void main(String args[]) { //Creating the connection String url = "jdbc:mysql://localhost:3306/test1"; String user = "system"; String password = "123"; try { Class.forName("com.mysql.jdbc.Driver"); //Reference to connection interface Connection con=DriverManager.getConnection(url,user,password); Statement st = con.createStatement(); //Displaying all the records of employee table ResultSet rs = st.executeQuery("select * from employee"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); } catch(Exception e) { System.out.println(e); } } } |
|
| 11. |
What causes “No suitable driver” error? |
|
Answer» “No suitable driver” ERROR occurs during a call to the DriverManager.getConnection() method, because of the following reasons: |
|
| 12. |
What is “Dirty read” in terms of database? |
Answer»
|
|
| 13. |
What is database connection pooling? What are the advantages of connection pool? |
Answer»
|
|
| 14. |
What is meant by a locking system in JDBC? |
Answer»
|
|
| 15. |
Explain the usage of the getter and setter methods in ResultSet. |
Answer»
The above statement is used to retrieve the value of the specified column Index and the return type is an int data type.
The above statement is used to insert the value of the specified column Index with an int value. |
|
| 16. |
Explain the types of RowSet available in JDBC. |
|
Answer» A RowSet is an OBJECT that encapsulates a set of rows from JDBC result sets or tabular data sources.
|
|
| 17. |
Explain the difference between execute(), executeQuery() and executeUpdate() methods in JDBC. |
||||||||||||
Answer»
The execute() method is used in the situations when you are not sure about the type of statement else you can use executeQuery() or executeUpdate() method. |
|||||||||||||
| 18. |
What is DataSource in JDBC? What are its benefits? |
Answer»
|
|
| 19. |
What is the difference between Statement and PreparedStatement? |
||||||||||||||||
Answer»
|
|||||||||||||||||
| 20. |
Explain JDBC Batch processing. |
Answer»
|
|
| 21. |
What are the types of JDBC statements? |
|
Answer» Statements are USEFUL for sending SQL commands to the database and receiving data from the database. There are three TYPES of statements in JDBC. They are:
|
|
| 22. |
Explain JDBC API components. |
|
Answer» The java.sql package contains different interfaces and classes for JDBC API. They are:
Classes:
|
|
| 23. |
Explain the types of ResultSet. |
|
Answer» ResultSet refers to the row and column data contained in a ResultSet object. The object of ResultSet maintains a cursor pointing to the CURRENT row of data in the result set.
|
|
| 24. |
What are difference between ResultSet and RowSet? |
||||||||||||
Answer»
|
|||||||||||||
| 25. |
What are the different types of JDBC drivers in Java? Explain each with an example. |
|
Answer» There are four types of JDBC drivers in Java. They are:
|
|