Interview Questions.

Jdbc Interview Questions and Answers

fluid

Jdbc Interview Questions and Answers

Q1. What is JDBC?

Ans: JDBC stands for Java Database Connectivity.It is a J2EE API that offers cross-Database connectivity to a wide variety of SQL databases.With a JDBC generation enabled driver we will connect to all corporate statistics although it's far in a heterogeneous environment.

 

Q2. Explain JDBC Driver?

 

Ans: The JDBC Driver provides specific implementations of the summary lessons supplied by using the JDBC API. The driving force is used to hook up with the database.

 

Q3. What is the distinction between Database and Database control system?

Ans: Database is a collection of interrelated data. Database control system is a software program which can be used to manipulate the facts by means of storing it on to the statistics base and via retrieving it from the records base.  And  DBMS is a collection of interrelated facts and some set of packages to get right of entry to the facts.

There are three styles of Database Management Systems:

Relational DataBase Management Systems(RDBMS):   It is a software program machine, which can be used to represents statistics inside the shape of tables. RDBMS will use SQL2 as a Queries  language.

Object Oriented DataBase Management Systems(OODBMS):  It is a software program system, which can be used to represent the records within the form of objects. This DBMS will use OQL as a Query language.

Object Relational DataBase Management Systems(ORDBMS):  It is a DBMS in an effort to represents a few part of the statistics within the shape of tables and a few other a part of the information inside the form of objects. This management machine will use SQL3 as a Query Language, it's miles a mixture of  SQL2 and OQL.

Q4. How to load a JDBC driving force?

Ans:

In fashionable sun Microsystems  has furnished Driver interface for this all the database companies has supplied their very own implementation.

If we want to apply the database seller provided Driver implementation to our jdbc software, first we need to make the availability of the respective Driver’s   .Elegance report to JVM, for this we need to set magnificence direction surroundings variable to the area in which we've the driving force implementation.

Sun Microsystems is also provided an implementation to the Driver interface inside the shape of JdbcOdbcDriver class as part of the java software.

If we want to apply JdbcOdbcDriver in our jdbc packages no need to set class direction surroundings variable. Why as it turned into already to be had within the java software program’s pre-defined library.

JdbcOdbcDriver internally depends on the mocrosoft product Odbc driver. If we want to apply the JdbcOdbcDriver in our jdbc applications first we must configure Odbc motive force, for this we can use the subsequent course.

Start/ conrlol panel / overall performance and renovation / administrative equipment / statistics supply(Odbc)/ person dsn / click on on Add / pick out microsofr Odbc for oracle / finish / offer records supply name simplest / click on on good enough / adequate.

To load the motive force’s class byte code to the reminiscence we will use the following technique.

Q5. Name the primary additives of JDBC ? 

Ans: The fundamental additives of JDBC consists of the following phases:

DriverManager: Manages a list of database drivers. Matches connection requests from the java utility with the right database motive force the usage of verbal exchange subprotocol. The first driving force that recognizes a certain subprotocol below JDBC can be used to set up a database Connection.

Driver: The database communications hyperlink, managing all conversation with the database. Normally, as soon as the driver is loaded, the developer want not call it explicitly.

Connection: Interface with all strategies for contacting a database.The connection item represents communication context, i.E., all communication with database is thru connection item most effective.

Statement: Encapsulates an SQL announcement that's handed to the database to be parsed, compiled, deliberate and accomplished.

ResultSet: The ResultSet represents set of rows retrieved due to question execution.

Q6. What is JDBC and What are the stairs to write a JDBC application?

Ans:

The process of interacting with the database from a java software is called as JDBC(Java Database Connectivity)

To engage with the database from a java application we are able to use the subsequent 5 steps.

Load and sign in the motive force.

Establish a connection between java application and the database.

Put together either assertion item or PreparedStatement item or CallebleStatement object as in step with the application requirements.

Write and executer the sq. Queries.

Terminate the relationship which we've got installed.

Q7. Which JDBC motive force is quickest?

Ans: The JDBC Net pure Java motive force is the fastest driving force.It converts the JDBC calls into vendor particular calls and for this reason it without delay interacts with the database and eventually it is fastest.

Q8. Name the distinctive kinds of JDBC drivers?

Ans: Following are the one-of-a-kind sorts of distinctive:

a) Network protocol Driver. 

B) JDBC Net pure Java Driver. 

C) Native API Partly Java Driver. 

D) JDBC-ODBC Bridge Driver.

Q9. How to set up a Database connection between java utility and Database?

Ans:

If we need to set up a connection between java software and the database we are able to the following piece of code.

Connection con= DriverManager.GetConnection(“jdbc:odbc:nag”,”nag”,”machine”,”supervisor”);

Where getConnectin() is a static technique from DriverManager magnificence, which can be used to go back connection item.

Q10. What is the requirement to apply Statement item?

Ans:

After setting up a connection between java utility and the database we need to write down the square queries and we need to execute them.

To execute the sq. Queries we will use some pre-defined library, which become described in the shape of Statement item, PreparedStattement object and CallableStatement item.

As in line with the application necessities we want to create both Statement object or CallableStatement object and PreparedStatement object.

To create Statement item dwe will use the following technique from connection object.

Public  Statement createStatement()

Eg:     Statement st = con.CreateStatement();

Q11. How to  execute SQL Queries from a java utility?

Ans: st.ExecuteQuery(…)

st.ExecuteUpdate(…)

st.Execute(…)

To execute the square queries we can use the subsequent techniques from Statement object.

Q12. What is resultset ?

Ans: The ResultSet represents set of rows retrieved due to query execution. ResultSet rs = stmt.ExecuteQuery(sqlQuery);

Q13. What are the unique kinds of RowSet ?

Ans: There are  types of RowSet are there. They are:

a) Connected: A related RowSet item connects to the database as soon as and remains linked till the software terminates.

B)Disconnected: A disconnected RowSet object connects to the database, executes a question to retrieve the information from the database after which closes the relationship. A application may also trade the data in a disconnected RowSet while it's far disconnected. Modified statistics can be updated within the database after a disconnected RowSet reestablishes the reference to the database.

 

Q14. What is a jdbc Drivermanager?

Ans:AThe DriverManager elegance acts as an interface among consumer and drivers. It maintains track of the drivers which are available and handles organising a connection between a database and an appropriate motive force. The DriverManager elegance continues a listing of Driver classes that have registered themselves by means of calling the technique DriverManager.RegisterDriver().

 

Q15. What is rowset? 

Ans: A RowSet is an object that encapsulates a hard and fast of rows from either Java Database Connectivity (JDBC) end result sets or tabular facts resources like a file or spreadsheet. RowSets assist issue-primarily based development fashions like JavaBeans, with a fashionable set of homes and an occasion notification mechanism.

 

Q16. How to create a table dynamically from a jdbc software?

Ans:   

 import java.Sq..*;

import java.Io.*;

public class CreateTableEx

public static void important(String[] args)throws Exception

//create buffered reader item

BufferedReader br = new BufferedReader(new       InputStreamReader(System.In));

//load and register the motive force

Class.ForName(“sun.Jdbc.Odbc.JdbcOdbcDriver”);

//establish connection

Connection con = DriverManager.GetConnection(“jdbc:odbc:nag”,”device”,”durga”);

//create statement item

Statement st = con.CreateStatement();

//take desk name as dynamic enter

System.Out.Println(“Enter table call”);

String tname = br.ReadLine();

//execute square question

St.ExecuteUpdate(“create desk”+tname+”(eno range,ename varchar2(10),esal quantity,eaddr varchar2(10))”);

System.Out.Println(“desk created successfully”);

//ultimate the connection

con.Close();

//import section

Q17. How to insert information into a table from a JDBC utility?

Ans:

import java.Io.*;

public class InsertTableEx

public static void main(String[] args) throws Exception

BufferedReader br = new BufferedReader(new InputStreamReader(System.In));

Class.ForName(“oracle.Jdbc.Driver.OracleDriver”);

Connection con  =               DriverManager.GetConnection(“jdbc:oracle:skinny:@localhost:1521:xe”,”machine”,”durga”);

Statement st = con.CreateStatement();

even as(true)

System.Out.Println(“Enter emp range”);

Int eno = Integer.ParseInt(br.ReadLine());

System.Out.Println(“Enter emp call”);

String ename = br.ReadLine();

System.Out.Println(“Enter emp sal”);

Float esal = Float.ParseFloat(br.ReadLine());

System.Out.Println(“Enter emp cope with”);

String eaddr = br.ReadLine();

st.ExecuteUpdate(“insert into emp1 values(“+eno+”,’”+ename+”’,”+esal+”,’”+eaddr+”’)”);

System.Out.Println(“study efficaciously inserted”);

System.Out.Println(“one extra document[y/n]);

String choice = br.ReadLine();

If(choice.Equals(“n”))

spoil;

import java.Sq..*;

Q18. How to update a desk  from a jdbc application?.

Ans:

import java.Square.*;

public class UpdateTableEx

public static void essential(String[] args)throws Exception

//load n check in the driver in alternative manner to Class.ForName

DriverManager.RegisterDriver(new oracle.Jdbc.Motive force.OracleDriver());

Connection con = DriverManager.GetConnection(“jdbc:oracle:skinny:@localhost:1521:xee”,”machine”,”durga”);

Statement st = con.CreateStatement();

int updateCount = st.ExecuteUpdate(“update emp1 set esal = esal+500 wherein esal<9000”);

System.Out.Println(“facts updated……..”+updateCount);

con.Close();

Q19.  Write a pattern join query and its type?

Ans: INNER JOIN: Returns all rows while there is as a minimum one match in BOTH tables

LEFT JOIN: Return all rows from the left desk, and the matched rows from the right desk

RIGHT JOIN: Return all rows from the proper desk, and the matched rows from the left table

FULL JOIN: Return all rows whilst there's a in shape in ONE of the tables

Q20. What are the exceptional forms of jdbc Statements?

Ans: There are three sorts of Statements, as given below:

Statement:

It may be used for standard-purpose get entry to to the database. It is beneficial while you are the use of static SQL statements at runtime.

PreparedStatement:

It can be used when you plan to apply the identical SQL declaration normally. The PreparedStatement interface accepts enter parameters at runtime.

CallableStatement:

CallableStatement can be used whilst you need to get admission to database stored procedures.




CFG