Jdbc

Welcome To Tripathi Jdbc Page

Jdbc Intervie Questions and Answers:

Q: What is the JDBC?

A: Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases form Java. JDBC has set of classes and interfaces which can use from Java application and talk to database without learning RDBMS details and using Database Specific JDBC Drivers
Q: What are the basic steps of using jdbc in java?
A: The basic steps are:
  • Load the RDBMS specific JDBC driver because this driver actually communicates with the database.
  • Open the connection to database which is then used to send SQL statements and get results back.
  • Create JDBC Statement object. This object contains SQL query.
  • Execute statement which returns resultset(s). Resultset contains the tuples of database table as a result of SQL query.
  • Process the result set.
  • Close the connection.
Q: What are the main component of jdbc?
A: The main components are:
  • DriverManager
  • Driver
  • Connection
  • Statement
  • Resultset
Q: What is DriverManager?
A: DriverManager is a static class. It manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication sub protocol. The first driver that recognizes a certain sub protocol under JDBC will be used to establish a database Connection.
Q: What is Driver?
A: The JDBC API defines the Java interfaces and classes that programmers use to connect to databases and send queries. A JDBC driver implements these interfaces and classes for a particular DBMS vendor.database communications link, handling all communication with the database.
Normally, once the driver is loaded, the developer need not call it explicitly.
Q: What is the connection?
A: Interface with all methods for contacting a database. The connection object represents communication context, i.e., all communication with database is through connection object only
Q: What is the statement?
A: Encapsulates an SQL statement which is passed to the database.
Q: What is the resultset?
A: The Resultset represents set of rows retrieved due to query execution.
Q: How we load a database driver with JDBC?
A: Provided the JAR file containing the driver is properly configured, just place the JAR file in the classpath. Java developers NO longer need to explicitly load JDBC drivers using code like Class.forName() to register a JDBC driver.
The DriverManager class takes care of this by automatically locating a suitable driver when the DriverManager.getConnection() method is called. This feature is backward-compatible, so no changes are needed to the existing JDBC code.
Q: What is the JDBC Driver interface?
A: The JDBC Driver interface provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each vendor driver must provide implementations of the java.sql.Connection,Statement,PreparedStatement, CallableStatement, ResultSet and Driver
Q: What does the connection objects represents?
A: The connection object represents communication context, i.e., all communication with database is through connection object only.
Q: What is the statement?
A: Statement acts like a vehicle through which SQL commands can be sent. Through the connection object we create statement kind of objects.
Q: What is the prepared statement?
A: A prepared statement is an SQL statement that is precompiled by the database. Through precompilation, prepared statements improve the performance of SQL commands that are executed multiple times. Once compiled, prepared statements can be customized prior to each execution by altering predefined SQL parameters.
Q: What is the difference between statement and PreparedStatement?
A: The difference is:
  • A standard Statement is used to create a Java representation of a literal SQL statement and execute it on the database. A PreparedStatement is a precompiled statement. This means that when the PreparedStatement is executed, the RDBMS can just run the PreparedStatement SQL statement without having to compile it first.
  • Statement has to verify its metadata against the database every time. While a prepared statement has to verify its metadata against the database only once.
  • If you want to execute the SQL statement once go for STATEMENT. If you want to execute a single SQL statement multiple number of times, then go for PREPAREDSTATEMENT. PreparedStatement objects can be reused with passing different values to the queries
Q: What is the callable statement?
A: Callable statements are used from JDBC application to invoke stored procedures and functions.
Q: How to call a stored procedure from jdbc?
A: PL/SQL stored procedures are called from within JDBC programs by means of the prepareCall() method of the Connection object created. A call to this method takes variable bind parameters as input parameters as well as output variables and creates an object instance of the CallableStatement class.
Q: What are the types of JDBC Driver?
A: The types are:
  • Type 1: JDBC/ODBC
  • Type2: Native API (partly-Java driver)
  • Type 3: Open Protocol-Net
  • Type 4: Proprietary Protocol-Net(pure Java driver)
Q: Which type of jdbc driver is the faster one?
A: JDBC Net pure Java driver(Type IV) is the fastest driver because it converts the JDBC calls into vendor specific protocol calls and it directly interacts with the database.
Q: Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection?
A: No, You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge.
Q: What are the standard isolation levels defined by the jdbc?
A: The standard isolation levels are:
  • TRANSACTION_NONE
  • TRANSACTION_READ_COMMITTED
  • TRANSACTION_READ_UNCOMMITTED
  • TRANSACTION_REPEATABLE_READ
  • TRANSACTION_SERIALIZABLE
Q: What is the resultset?
A: The ResultSet represents set of rows retrieved due to query execution. Example: ResultSetrs = stmt.executeQuery(sqlQuery);
Q: What are the types of resultset?
A: The types are:
  • TYPE_FORWARD_ONLY specifies that a resultset is not scrollable, that is, rows within it can be advanced only in the forward direction.
  • TYPE_SCROLL_INSENSITIVE specifies that a resultset is scrollable in either direction but is insensitive to changes committed by other transactions or other statements in the same transaction.
  • TYPE_SCROLL_SENSITIVE specifies that a resultset is scrollable in either direction and is affected by changes committed by other transactions or statements within the same transaction.
Q: What is the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCOLL_SENSITIVE?
A: An insensitive resultset is like the snapshot of the data in the database when query was executed. A sensitive resultset does NOT represent a snapshot of data; rather it contains points to those rows which satisfy the query condition.
After we get the resultset the changes made to data are not visible through the resultset, and hence they are known as insensitive. After we obtain the resultset if the data is modified then such modifications are visible through resultset.
Q: What is the RowSet?
A: 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.
Q: What are the different types of RowSet?
A:The different types are:
  • Connected - A connected RowSet object connects to the database once and remains connected until the application terminates.
  • Disconnected - A disconnected RowSet object connects to the database, executes a query to retrieve the data from the database and then closes the connection. A program may change the data in a disconnected RowSet while it is disconnected. Modified data can be updated in the database after a disconnected RowSet re-establishes the connection with the database.
Q: What is the need of BatchUpdates?
A: The BatchUpdates feature allows us to group SQL statements together and send to database server in one single trip.
Q: What is the data source?
A: 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.
Q: What are the advantages of data source?
A: The advantages 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.
  • The DataSource facility allows developers to implement a DataSource class to take advantage of features like connection pooling and distributed transactions.
Q: What is the main advantage of connection pooling?
A: 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.
Q: What is the multi programming?
A: Multiprogramming is a rapid switching of the CPU back and forth between processes.
Q: What is the difference between TCP and UDP?
A: TCP is designed to provide reliable communication across a variety of reliable and unreliable networks and internets.UDP provides a connectionless so it isbasically an unreliable service. Delivery and duplicate protection are not guaranteed.
Q: What is socket?
A: The combination of an IP address and a port number is called a socket.
Q: What is the advantage of java socket?
A: The advantages are:
  • Sockets are flexible and sufficient.
  • Efficient socket based programming can be easily implemented for general communications.
  • Sockets cause low network traffic.
Q: What is the disadvantage of java socket?
A: The disadvantages are:
  • Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from, and to nowhere else on the network.
  • Despite all of the useful and helpful Java features, Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.
  • Since the data formats and protocols remain application specific, the re-use of socket based implementations is limited.
Q: What is RMI?
A: It stands for Remote Method Invocation. RMI is a set of APIs that allows to build distributed applications. RMI uses interfaces to define remote objects to turn local method invocations into remote method invocations.
Q: What is socket()?
A: The socket () is very similar to socketPair() except that only one socket is created instead of two. This is most commonly used when if the process you wish to communicate with is not the child process.
Q: What is ServerSocket?
A: The ServerSocket class is used to create serverSocket. This object is used to communicate with client.
Q: What is bind()?
A: It binds the socket to the specified server and port in the SocketAddress object. Use this method if you instantiated the ServerSocket using the no-argument constructor.
Q: What is the Datagram?
A: A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
Q: What is getLocalPort()?
A: It returns the port that the server socket is listening on. This method is useful if you passed in 0 as the port number in a constructor and let the server find a port for you.
Q: What is accept()?
A: It waits for an incoming client. This method blocks until either a client connects to the server on the specified port or the socket times out, assuming that the time-out value has been set using the setSoTimeout() method. Otherwise, this method blocks indefinitely.
Q: What is the network interface?
A: A network interface is the point of interconnection between a computer and a private or public network. A network interface is generally a network interface card (NIC), but does not have to have a physical form.
Q: What is the encapsulation technique?
A: Hiding data within the class and making it available only through the methods. This technique is used to protect your class against accidental changes to fields, which might leave the class in an inconsistent state.
Q: How does the race condition occur?
A: It occurs when two or more processes are reading or writing some shared data and the final result depends on who runs precisely when.
Q: What information is needed to create a TCP Socket?
A: Socket is created from this information:
  • Local System's: IP Address and Port Number
  • Remote System’s: IPAddress and Port Number
q

No comments:

Post a Comment