JDBC questions
Q. What is JDBC ? what are its advantages ?
A. It is an API .The latest version of jdbc api is (3.0).
The JDBC 3.0 API is divided into two packages:
(1) java.sql and (2) javax.sql.
Both packages are included in the J2SE and J2EE platforms.
Advantage:
The JDBC API can be used to interact with multiple data sources in a distributed, heterogenous environment.
It can connect to any of the database from java language.
It can switch over to any backend database without changing java code or by minute changes.
Q. How many JDBC Drivers are there ? what are they?
A. There are 4 types of JDBC drivers.
a. JDBC-ODBC Bridge Driver(Type-1 driver)
b. Native API Partly Java Driver(Type-2 driver)
c. Net protocol pure Java Driver(Type-3 driver)
d. Native protocol Pure Java Driver(Type-4 driver)
Q. Explain about JDBC-ODBC driver(Type-1) ? When this type of driver is used ?
A. In this mechanism the flow of execution will be
Java code(JDBC API)<------>JDBC-ODBC bridge driver<------->ODBC API<------->ODBC Layer<-------->DataBase
This type of JDBC Drivers provides a bridge between JDBC API and ODBC API.
This Bridge(JDBC-ODBC bridge) translates standard JDBC calls to Corresponding ODBC Calls, and
send them to ODBC database via ODBC Libraries.
The JDBC API is based on ODBC API.
ODBC(Open Database Connectivity)is Microsoft’s API for Database drivers.
ODBC is based on X/Open Call Level Interface(CLI)specification for database access.
The URL and class to be loaded for this type of driver are
Class :- sun.jdbc.odbc.JdbcOdbcDriver
URL :- jdbc:odbc:dsnname
Q. Explain about Type-2 driver ? When this type of driver is used ?
A. The Drivers which are written in Native code will come into this category
In this mechanism the flow of Execution will be
java code(JDBC API)<------>Type-2 driver(jdbc driver)<------->Native API(vendor specific)<------->DataBase
When database call is made using JDBC,the driver translates the request into vendor-specific API calls.
The database will process the requet and sends the results back through the Native API ,which will forward them
back to the JDBC dirver. The JDBC driver will format the results to conform to the JDBC standard and return
them to the application.
Q. Explain about Type-3 driver ? When this type of driver is used ?
A. In this mechanism the flow of Execution will be
java code(JDBC API)<------>JDBC driver<------->JDBC driver server<-------->Native driver<------->DataBase
The Java Client Application sends the calls to the Intermediate data access server(jdbc driver server)
The middle tier then handles the requet using other driver(Type-II or Type-IV drivers) to complete the request.
Q. Explain about Type-4 driver ? When this type of driver is used ?
A. This is a pure java driver(alternative to Type-II drivers).
In this mechanism the flow of Execution will be
java code(JDBC API)<------>Type-4 driver(jdbc driver)<------->DataBase
These type of drivers convert the JDBC API calls to direct network calls using
vendor specific networking protocal by making direct socket connection with
database.
examples of this type of drivers are
1.Tabular Data Stream for Sybase
2.Oracle Thin jdbc driver for Oracle
Q. Which Driver is preferable for using JDBC API in Applets?
A. Type-4 Drivers.
Q.Write the Syntax of URL to get connection ? Explain?
A.Syntax:- jdbc:
jdbc —–> is a protocal .This is only allowed protocal in JDBC.
name of the database connectivity mechanism, choosen by the database driver providers.
ex: jdbc:odbc:dsn
jdbc:oracle:oci8:@ database name.
jdbc:orale:thin:@ database name:port number:SID
Q.How do u Load a driver ?
A. Using Driver Class.forName(java.lang.String driverclass) or registerDriver(Driver driver) .
Q.what are the types of resultsets in JDBC3.0 ?How you can retrieve information of resultset?
A. ScrollableResultSet and ResultSet.We can retrieve information of resultset by using java.sql.ResultSetMetaData interface.You can get the instance by calling the method getMetaData() on ResulSet object.
Q.write the steps to Connect database?
A. Class.forName(The class name of a spasific driver);
Connection c=DriverManager.getConnection(url of a spasific driver,user name,password);
Statement s=c.createStatement();
(or)
PreparedStatement p=c.prepareStatement();
(or)
CallableStatement cal=c.prpareCall();
Depending upon the requirement.
Q.Can java objects be stored in database? how?
A.Yes.We can store java objects, BY using setObject(),setBlob() and setClob() methods in PreparedStatement
Q.what do u mean by isolation level?
A. Isolation means that the business logic can proceed without
consideration for the other activities of the system.
Q.How do u set the isolation level?
A. By using setTransactionIsolation(int level) in java.sql.Connection interface.
level MEANS:-
static final int TRANSACTION_READ_UNCOMMITTED //cannot prevent any reads.
static final int TRANSACTION_READ_COMMITTED //prevents dirty reads
static final int TRANSACTION_REPEATABLE_READ //prevents dirty reads & non-repeatable read.
static final int TRANSACTION_SERIALIZABLE //prevents dirty reads , non-repeatable read & phantom read.
These are the static final fields in java.sql.Connection interface.
Q.What is the difference between java.sql.Statement & java.sql.PreparedStatement ?
A. write the appropriate situations to use these statements?
Q.How to retrieve the information about the database ?
A.we can retrieve the info about the database by using inerface java.sql.DatabaseMetaData we can get this object by using getMetaData() method in Connection interface.
Q.what are the Different types of exceptions in jdbc?
A. BatchUpdateException
DataTruncation
SQLException
SQLWarning
Q.How to execute no of queries at one go?
A. By using a batchUpdate’s (ie throw addBAtch() and executeBatch()) in java.sql.Statement interface,or by using procedures.
Q.what are the advantages of connection pool.
A. Performance
Q. In which interface the methods commit() & rollback() are defined ?
A. java.sql.Connection interface
Q.How to store images in database?
A. Using binary streams (ie getBinaryStream() ,setBinaryStream()). But it is not visable in database ,it is stored in form of bytes ,to make it visable we have to use any one frontend tool.
Q.How to check null value in JDBC?
A. By using the method wasNull() in ResultSet ,it returns boolean value.
Returns whether the last column read had a value of SQL NULL.
Note that you must first call one of the getXXX methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.
Q.Give one Example of static Synchronized method in JDBC API?
A. getConnection() method in DriverManager class.Which is used to get object of Connection interface.
Q.What is a Connection?
A. Connection is an interface which is used to make a connection between client and Database (ie opening a session with a particular database).
Q.what is the difference between execute() ,executeUpdate() and executeQuery() ? where we will use them?
A. execute() method returns a boolean value (ie if the given query returns a resutset then it returns true else false),so depending upon the return value we can get the ResultSet object (getResultset())or we can know how many rows have bean affected by our query (getUpdateCount()).That is we can use this method for Fetching queries and Non-Fetching queries.Fetching queries are the queries which are used to fetch the records from database (ie which returns resutset) ex: Select * from emp.
Non-Fetching queries are the queries which are used to update,insert,create or delete the records from database
ex: update emp set sal=10000 where empno=7809.
executeUpdate() method is used for nonfetching queries.which returns int value.
executeQuery() method is used for fetching queries which returns ResulSet object ,Which contains methods to fetch the values.
Popularity: 11%
Leave a Reply
You must be logged in to post a comment.