1.
What is JDBC?
JDBC is a
layer of abstraction that allows users to choose between databases. It allows
you to change to a different database engine and to write to a single API. JDBC
allows you to write database applications in Java without having to concern
yourself with the underlying details of a particular database.
2.
What are the common tasks or steps of JDBC?
1. Create an
instance of a JDBC driver or load JDBC drivers through jdbc.drivers;
2. Register a driver;
3. Specify a database;
4. Open a database connection;
5. Submit a query;
6. Receive results
2. Register a driver;
3. Specify a database;
4. Open a database connection;
5. Submit a query;
6. Receive results
3.
What Class.forName will do while loading drivers of JDBC?
It is used
to create an instance of a driver and register it with the DriverManager. When
you have loaded a driver, it is available for making a connection with a DBMS.
4. Difference between PreparedStatement & Statement?
preparedstatement
extends statement with added advantage of taking the arguments at run
time.
preparedStatement
pstmt =con.prepareStatement("update FIRST_TABLE set job_code = ? where
name = ? ");
pstmt.setInt(1,2);
pstmt.setString(2,"JOHN");
When the
PreparedStatement is executed, the DBMS can just run the PreparedStatement ’s
SQL statement without having to compile it first.
Statement is
used to execute static queries in the databases. It can’t take the parameters
at run time.
stmt.executeQuery("select
* from FIRST_TABLE");
5. What is SQLException?
The SQLException class and its subtypes provide information
about errors and warnings that occur while a data source is being accessed.
The base class for exceptions that occur while running JDBC
applications is SQLException. Every method of the JDBC API is declared as being
able to throw SQLExceptions. SQLException is an extension of
java.lang.Exception and provides additional information related to failures
that happen in a database context. Specifically, the following information is
available from an SQLException:
- Text description
- SQLState
- Error code
- A reference to any other exceptions that also occurred
6. What is ResultSet object?
A
ResultSet
object maintains a cursor pointing to
its current row of data. Initially the cursor is positioned before the first
row. The next
method moves the cursor to the next
row, and because it returns false
when there are no more rows in the ResultSet
object, it can be used in a while
loop to iterate through the result
set.
A default
ResultSet
object is not updatable and has a
cursor that moves forward only. Thus, we can iterate through it only once and
only from the first row to the last row. It is possible to produce ResultSet
objects that are scrollable and/or
updatable.
7. What packages are used by JDBC?
There are 8
packages: java.sql.Driver, Connection,Statement, PreparedStatement,
CallableStatement, ResultSet, ResultSetMetaData, DatabaseMetaData.
8. What is the difference between
executequery () and executeupdate ()?
executeQuery() - is for operation select of Sql by
PreparedStatement or Statement.
executeUpdata()- is for the operations such as insert, update or delete on SQL by PreparedStatement or Statement.
9. What is TableModel?
The
TableModel
interface specifies the methods the JTable
will use to interrogate a tabular
data model.
The
JTable
can be set up to display any data
model which implements the TableModel
interface with a couple of lines of
code: TableModel myData = new TableModel();
JTable table = new JTable(myData);
10. What is DefaultMutableTreeNode & DefaultTreeModel?
A
DefaultMutableTreeNode
is a general-purpose node in a
tree data structure. DefaultMutableTreeNode
provides operations for examining and modifying a node's parent and children
and also operations for examining the tree that the node is a part of. This
class provides enumerations for efficiently traversing a tree or subtree in
various orders or for following the path between two nodes. A DefaultMutableTreeNode
may also
hold a reference to a user object, the use of which is left to the user. Asking
a DefaultMutableTreeNode
for its string representation with toString()
returns the string representation of its user
object.
DefaultTreeModel is a simple tree data model that uses
TreeNodes.
Evidence Question for JDBC:
- a. Create a database named inventory in MySql using the command prompt.
b. Create a table named product in that database with required field and data types to store the product list from the application.
c. In NetBeans, create a project and create necessary frames to purchase and sale product. The product quantity should be updated properly after purchase and sale a product. Display the product quantity on hand of specific product.
d. Display all product lists in a table on a frame.
Tags
Advanced Java