Servlets Java Technical Interview Questions
Categories: Education
Q.1. What do you mean by batch processing in JDBC?
Ans. Batch processing helps you to group related SQL statements into a batch and execute them instead of executing a single query. By using batch processing technique in JDBC, you can execute multiple queries which makes the performance faster.
Q.2. What is the difference between execute, executeQuery, executeUpdate?
Ans. Statement execute(String query) is used to execute any SQL query and it returns TRUE if the result is an ResultSet such as running Select queries. The output is FALSE when there is no ResultSet object such as running Insert or Update queries. We can use getResultSet() to get the ResultSet and getUpdateCount() method to retrieve the update count.
Statement executeQuery(String query) is used to execute Select queries and returns the ResultSet. ResultSet returned is never null even if there are no records matching the query. When executing select queries we should use executeQuery method so that if someone tries to execute insert/update statement it will throw java.sql.SQLException with message “executeQuery method can not be used for update”.
Statement executeUpdate(String query) is used to execute Insert/Update/Delete (DML) statements or DDL statements that returns nothing. The output is int and equals to the row count for SQL Data Manipulation Language (DML) statements. For DDL statements, the output is 0.
You should use execute() method only when you are not sure about the type of statement else use executeQuery or executeUpdate method.
Q.3. What do you understand by JDBC Statements?
Ans. JDBC statements are basically the statements which are used to send SQL commands to the database and retrieve data back from the database. Various methods like execute(), executeUpdate(), executeQuery, etc. are provided by JDBC to interact with the database.
JDBC supports 3 types of statements:
(i) Statement: Used for general purpose access to the database and executes a static SQL query at runtime.
(ii) PreparedStatement: Used to provide input parameters to the query during execution.
(iv) CallableStatement: Used to access the database stored procedures and helps in accepting runtime parameters.
Q.4. What are the differences between forward() method and sendRedirect() methods?
Ans.
forward() method
(i) forward() sends the same request to another resource.
(ii) forward() method works at server side.
(iii) forward() method works within the server only.
SendRedirect() method
(i) sendRedirect() method sends new request always because it uses the URL bar of the browser.
(ii) sendRedirect() method works at client side.
(iii) sendRedirect() method works within and outside the server.
Q.5. What are the differences between ServletContext vs ServletConfig?
Ans. The difference between ServletContext and ServletConfig in Servlets JSP is in below tabular format.
ServletConfig
(i) Servlet config object represent single servlet
(ii) Its like local parameter associated with particular servlet
(iii) It's a name value pair defined inside the servlet section of web.xml file so it has servlet wide scope
(iv) getServletConfig() method is used to get the config object
(v) for example shopping cart of a user is a specific to particular user so here we can use servlet config
ServletContext
(i) It represent whole web application running on particular JVM and common for all the servlet
(ii) Its like global parameter associated with whole application
(iii) ServletContext has application wide scope so define outside of servlet tag in web.xml file.
(iv) getServletContext() method is used to get the context object.
(v) To get the MIME type of a file or application session related information is stored using servlet context object.