Saturday, September 5, 2009

JDBC interview Questions

What is rowset?

A RowSet is an object that encapsulates a set of rows from either Java Database Connectivity (JDBC) result sets or tabular data sources like a file or spreadsheet. RowSets support component-based development models like JavaBeans, with a standard set of properties and an event notification mechanism.

 

What is the need of BatchUpdates?

The BatchUpdates feature allows us to group SQL statements together and send to database server in one single trip.


What is a DataSource?

A DataSource object is the representation of a data source in the Java programming language. In basic terms,

  • A DataSource is a facility for storing data.
  • DataSource can be referenced by JNDI.
  • Data Source may point to RDBMS, file System , any DBMS etc..

What are the advantages of DataSource?

The few advantages of data source are :

  • An application does not need to hardcode driver information, as it does with the DriverManager.
  • The DataSource implementations can easily change the properties of data sources. For example: There is no need to modify the application code when making changes to the database details.
  • The DataSource facility allows developers to implement a DataSource class to take advantage of features like connection pooling and distributed transactions.


What is connection pooling? what is the main advantage of using connection pooling?

A connection pool is a mechanism to reuse connections created. Connection pooling can increase performance dramatically by reusing connections rather than creating a new physical connection each time a connection is requested..

 

Explain the Java packages which contains JDBC classes and interfaces, Java.SQL, Javax.SQL

java.sql:

java.sql is an API to access and process the data stored in a database, typically a relational database using the java. Different drivers can be installed dynamically for the access of various databases, using a framework which in-built in this JDBC API.

javax.sql:

javax.sql is a JDBC API for the server side for accessing and processing the data from the databases typically a relational database using java. It is the essential part for J2EE. This API provides the facilities such as connection pooling, distributed transactions and row sets for the enterprise applications. An interface by name DataSource is provided in this API as an alternative to DriverManager to establish the connection.

DataSource and RowSet usage is direct, for the applications, where as connection pooling, distributed transactions are implemented by an infrastructure called middle-tier.

Describe how to open a database connection using JDBC.

Opening a database connection:

The database connection should be established after registering the drivers. The getConnection is used to open a database connection. The following code snippet illustrates this:

Connection con;
con = DriverManager.getConnection(url,"scott","tiger");// returns the connection object

where url specifies the database server port, ip address,domain name etc. For ex:
jdbc:oracle:thin:@192.137.63.230:1521:MyCompany

the ip address is an imaginary one and not intended to specify any specific system. The “scott” and “tiger” are the user name and passwords respectively.

Describe with an example how to send SQL statements to databases for execution.

The following illustrates with examples, to send SQL statements to databases for execution:

  • The SQL statements are to be created as objects. These objects are to be assigned to Statement reference. 
    Ex: Statement stmts = con.createStatement();//creates a statement object
  • The specific SQL statements are to be executed and the result should be captured
    Ex: ResultSet rsltSet = stmts.executeQuery(“select * from emp”);
  • To make a row inserted into the database and committed:
    stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

    ResultSet rs=stmt.executeQuery("select * from emp");
             rs.moveToInsertRow();
             rs.updateInt(1,15); // first attribute with value 15
             rs.updateString(2,"Vismay”); // second value with Vismay
             rs.updateFloat(3,4500); // third attribute with 4500
             rs.insertRow();
    con.commit();

1

2

3

4

5

6

7

8

 

No comments:

Post a Comment