Tuesday, January 25, 2011

Java JDBC Questions

Question : What is JDBC ?
Ans : Java Database Connectivity , Provide a java interface to Relational database.

Flow Pattern : 
                         Application ----- > JDBC --------> Driver ------------>Database..


Question: How to get the Instance of Driver ?
Ans : Class Class.forName("name of the JDBC Driver");


Steps in Creating a Database Connection in JAva/
Step 1 : load the Driver.   
          --> through Class.forName("Name of the Driver");


This helps you to establish a connection between front end back-end of Application.
Step 2 : Creating a oracle JDBC Connection ?
   DriverManger.getConnection();
Helps you to get the Connection Object,helps you to build connection between
URL and Database.


Step3 : Create JDBC statement Object.


Step4 : Execute SQL statements?


Question : What is DriverManager Class?
Ans: Which creates objects which can connect Java applications to database.
  It manages the Drivers that are installed on the System.
getConnection() ,method uses to establish connection to the Database.


Syntax of getConnection(dbURL,userName,Password);
dbUrl  == java:subProtocol:subName.
Each Driver has it's own subProtocol Syntax : 
Example : for JDBC-ODBC Driver -> sun.jdbc.odbc.JdbcOdbcDriver
for MYSQL : mysql
Question : what is JDBC connection :
Ans: A jdbc connection is session/connection, within context of connection SQL statements
get Executed and results are returned.
A Connection Object provides MetaData, information about the database, fields,tables etc..
A Connection interface defines methods to interact with database.
Question : what is statement Object ?
Ans : A Statement Object is used to send and execute statements to Database.


Question : What are the different types of statements?
Ans :  Statement              : A simple Sql query Statement.
         Prepared Statement : Pre-Compiled Sql Queries 
         Callable Statement   : A call to Database Stored Procedure.


what is the Difference between DML , DDL , DCL commands /
Ans : DDL ---  use to define the Structure/scheme of a Database. 
Example : Alter, Create, Drop, truncate,Rename.
DML -- Use to manage the data within the scheme Objects
Example : UPDATE,SELECT , INSET, DELETE,MERGE, CALL
DCL :  GRANT, REVOKE.


TCL[ Transaction Control ] : used to manage the changes made by the DML commands
example : COMMIT, SAVEPOINT,ROllback.SetRollback.


Question : What is the Difference between EXECUTE(), ExecuteQuery(), ExecuteUpdate()?
Ans :
   boolean Execute() . == Will do both the Operations of ExecuteQuery and ExecuteUpdate.
  ResultSet ExecuteQuery();
 int ExecuteUpdate(); Insert,Update,Delete. or an SQL query which returns nothing.


What are the different Database Drivers available ?
Ans :
        1) JDBC-ODBC Drivers.
        2) Native - API partly Java Driver.
        3) Net Protocol Java Driver.
        4) Native Protocol Java Driver.
What are the Difference between these drivers?
Ans :  Type 1 : Converts all JDBC calls into ODBC calls.
         Type 4 : No Conversion requires purely java Driver.
         Type 1 : Need to install third Party[ ODBC Drivers ] Drivers.
         Type 4 : No Need.


Type 1 and 3 are Database independent .
Type 2 and 4 are database Dependent.


http://www.bestjavainterviewquestions.com/java-jdbc-questions/




Question : How to Debug problems related to JDBC ?
Ans : DriverManager.setLogWriter(PrintWriter out);
Question : What Causes the "no Suitable driver" error?
Ans : Specifying invalid URL in DriverManager.getConnection(); - Where the driver is 
unable to recognize that URL.


Question : How to get the Number of rows in a ResultSet ?
Ans : 
1. Invoke ResultSet.last() on the ResultSet.
2. Invoke ResultSet.getRow() to get the current row number (which is also the number of rows in the ResultSet)
3. Invoke ResultSet.beforeFirst() on the ResultSet.
4. Process as normal.
Question : 

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...