Jdbc Interview Questions
By Ramakrishna on Jun 25, 2008 in jdbc questions
Q) Is java.sql.Driver a class or an Interface ?
A) It’s an interface.
Q) Is java.sql.DriverManager a class or an Interface ?
A) It’s a class. This class provides the static getConnection method, through which the database connection is obtained.
Q) Is java.sql.Connection a class or an Interface ?
A) java.sql.Connection is an interface. The implmentation is provided by the vendor specific Driver.
Q) Is java.sql.Statement a class or an Interface ?
A) java.sql.Statement,java.sql.PreparedStatement and java.sql.CallableStatement are interfaces.
Q) Which interface do PreparedStatement extend?
A) java.sql.Statement
9) Which interface do CallableStatement extend?
A) CallableStatement extends PreparedStatement.
Q) What is the purpose Class.forName(”") method?
A) The Class.forName(”") method is used to load the driver.
Q) Do you mean that Class.forName(”") method can only be used to load a driver?
A) The Class.forName(”") method can be used to load any class, not just the database vendor driver class.
Q) Which statement throws ClassNotFoundException in SQL code block? and why?
A) Class.forName(”") method throws ClassNotFoundException. This exception is thrown when the JVM is not able to find the class in the classpath.
Q) What exception does Class.forName() throw?
A) ClassNotFoundException.
Q) What is the return type of Class.forName() method ?
A) java.lang.Class
Q) Can an Interface be instantiated? If not, justify and explain the following line of code:
A) Connection con = DriverManager.getConnection(”dsd”,”sds”,”adsd”);
An interface cannot be instantiated. But reference can be made to a interface. When a reference is made to interface, the refered object should have implemented all the abstract methods of the interface. In the above mentioned line, DriverManager.getConnection method returns Connection Object with implementation for all abstract methods.
Q) What type of a method is getConnection()?
A) static method.
Q) What is the return type for getConnection() method?
A) Connection object.
Q) What is the return type for executeQuery() method?
A) ResultSet
Q) What is the return type for executeUpdate() method and what does the return type indicate?
A) int. It indicates the no of records affected by the query.
Q) What is the return type for execute() method and what does the return type indicate?
A) boolean. It indicates whether the query executed sucessfully or not.
Q) is Resultset a Classor an interface?
A) Resultset is an interface.
Q) What is the advantage of PrepareStatement over Statement?
A) PreparedStatements are precompiled and so performance is better. PreparedStatement objects can be reused with passing different values to the queries.
Q) What is the use of CallableStatement?
A) CallableStatement is used to execute Stored Procedures.
Q) Name the method, which is used to prepare CallableStatement?
A) CallableStament.prepareCall().
