|
JDBC Interview Questions and Answers
DB2 Universal claims to support JDBC 2.0, But I can
only get JDBC 1.0 functionality. What can I do?
DB2 Universal defaults to the 1.0 driver. You have to
run a special program to enable the 2.0 driver and JDK
support. For detailed information, see Setting the
Environment in Building Java Applets and Applications.
The page includes instructions for most supported
platforms.
How do I disallow NULL values in a table?
Null capability is a column integrity constraint,
normally applied at table creation time. Note that some
databases won't allow the constraint to be applied after
table creation. Most databases allow a default value for
the column as well. The following SQL statement displays
the NOT NULL constraint:
CREATE TABLE CoffeeTable (
Type VARCHAR(25) NOT NULL,
Pounds INTEGER NOT NULL,
Price NUMERIC(5, 2) NOT NULL
)
How to get a field's value with ResultSet.getxxx when it
is a NULL? I have tried to execute a typical SQL
statement:
select * from T-name where (clause);
But an error gets thrown because there are some NULL
fields in the table.
You should not get an error/exception just because of
null values in various columns. This sounds like a
driver specific problem and you should first check the
original and any chained exceptions to determine if
another problem exists. In general, one may retrieve one
of three values for a column that is null, depending on
the data type. For methods that return objects, null
will be returned; for numeric ( get Byte(), getShort(),
getInt(), getLong(), getFloat(), and getDouble() ) zero
will be returned; for getBoolean() false will be
returned. To find out if the value was actually NULL,
use ResultSet.wasNull() before invoking another getXXX
method.
How do I insert/update records with some of the columns
having NULL value?
Use either of the following PreparedStatement methods:
public void setNull(int parameterIndex, int sqlType)
throws SQLException
public void setNull(int paramIndex, int sqlType, String
typeName) throws SQLException
These methods assume that the columns are nullable. In
this case, you can also just omit the columns in an
INSERT statement; they will be automatically assigned
null values.
Is there a way to find the primary key(s) for an Access
Database table? Sun's JDBC-ODBC driver does not
implement the getPrimaryKeys() method for the
DatabaseMetaData Objects.
// Use meta.getIndexInfo() will
//get you the PK index. Once
// you know the index, retrieve its column name
DatabaseMetaData meta = con.getMetaData();
String key_colname = null;
// get the primary key information
rset = meta.getIndexInfo(null,null, table_name,
true,true);
while( rset.next())
{
String idx = rset.getString(6);
if( idx != null)
{
//Note: index "PrimaryKey" is Access DB specific
// other db server has diff. index syntax.
if( idx.equalsIgnoreCase("PrimaryKey"))
{
key_colname = rset.getString(9);
setPrimaryKey( key_colname );
}
}
}
Why can't Tomcat find my Oracle JDBC drivers in
classes111.zip?
TOMCAT 4.0.1 on NT4 throws the following exception when
I try to connect to Oracle DB from JSP.
javax.servlet.ServletException :
oracle.jdbc.driver.OracleDriver
java.lang.ClassNotFoundException:
oracle:jdbc:driver:OracleDriver
But, the Oracle JDBC driver ZIP file (classes111.zip)is
available in the system classpath.
Copied the Oracle Driver class file (classes111.zip) in
%TOMCAT_Home - Home%\lib directory and renamed it to
classess111.jar.
Able to connect to Oracle DB from TOMCAT 4.01 via Oracle
JDBC-Thin Driver.
I have an application that queries a database and
retrieves the results into a JTable. This is the code in
the model that seems to be taken forever to execute,
especially for a large result set:
while ( myRs.next() ) {
Vector newRow =new Vector();
for ( int i=1;i<=numOfCols;i++ )
{
newRow.addElement(myRs.getObject(i));
}
allRows.addElement(newRow);
}
fireTableChanged(null);
newRow stores each row of the resultset and allRows
stores all the rows.
Are the vectors here the problem?
Is there another way of dealing with the result set that
could execute faster?
java.util.Vector is largely thread safe, which means
that there is a greater overhead in calling addElement()
as it is a synchronized method. If your result set is
very large, and threading is not an issue, you could use
one of the thread-unsafe collections in Java 2 to save
some time. java.util.ArrayList is the likeliest
candidate here.
Do not use a DefaultTableModel as it loads all of your
data into memory at once, which will obviously cause a
large overhead - instead, use an AbstractTableModel and
provide an implementation which only loads data on
demand, i.e. when (if) the user scrolls down through the
table.
How does one get column names for rows returned in a
ResultSet?
ResultSet rs = ...
...
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
for (int i = 1; i <= numCols; i++)
{
System.out.println("[" + i + "]" +
rsmd.getColumnName(i) + " {" +
rsmd.getColumnTypeName(i) + "}");
}
What are the considerations for deciding on transaction
boundaries?
Transaction processing should always deal with more than
one statement and a transaction is often described as a
Logical Unit of Work ( LUW ). The rationale for
transactions is that you want to know definitively that
all or none of the LUW completed successfully. Note that
this automatically gives you restart capability.
Typically, there are two conditions under which you
would want to use transactions:
* Multiple statements involving a single file - An
example would be inserting all of a group of rows or all
price updates for a given date. You want all of these to
take effect at the same time; inserting or changing some
subset is not acceptable.
* Multiple statements involving multiple files - The
classic example is transferring money from one account
to another or double entry accounting; you don't want
the debit to succeed and the credit to fail because
money or important records will be lost. Another example
is a master/detail relationship, where, say, the master
contains a total column. If the entire LUW, writing the
detail row and updating the master row, is not completed
successfully, you A) want to know that the transaction
was unsuccessful and B) that a portion of the
transaction was not lost or dangling.
Therefore, determining what completes the transaction or
LUW should be the deciding factor for transaction
boundaries.
How can I determine where a given table is referenced
via foreign keys?
DatabaseMetaData.getExportedKeys() returns a ResultSet
with data similar to that returned by
DatabaseMetaData.getImportedKeys(), except that the
information relates to other tables that reference the
given table as a foreign key container.
How can I get information about foreign keys used in a
table?
DatabaseMetaData.getImportedKeys() returns a ResultSet
with data about foreign key columns, tables, sequence
and update and delete rules.
Can I use JDBC to execute non-standard features that my
DBMS provides?
The answer is a qualified yes. As discussed under SQL
Conformance: "One way the JDBC API deals with this
problem is to allow any query string to be passed
through to an underlying DBMS driver. This means that an
application is free to use as much SQL functionality as
desired, but it runs the risk of receiving an error on
some DBMSs. In fact, an application query may be
something other than SQL, or it may be a specialized
derivative of SQL designed for specific DBMSs (for
document or image queries, for example)."
Clearly this means either giving up portability or
checking the DBMS currently used before invoking specific
operations.
Page Numbers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
|