Download Questions and Answers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Oracle Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Serializability wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Concurrency control wikipedia , lookup

SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Versant Object Database wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Questions and Answers
Q.1) What is, in terms of JDBC, a DataSource?
A. A DataSource is the basic service for managing a set of JDBC drivers
e.
co
m
B. A DataSource is the Java representation of a physical data source
C. A DataSource is a registry point for JNDI-services
D. A DataSource is a factory of connections to a physical data source
ANSWER : A DataSource is a factory of connections to a physical data source
SOLUTION :
In the JDBC API, databases are accessed by using DataSource objects. A DataSource
has a set of properties that identify and describe the real-world data source that it
represents. These properties include such information as the location of the database
server, the name of the database, the network protocol to use to communicate with the
server, and so on. In the GlassFish Server, a data source is called a JDBC resource.
nl
in
Applications access a data source by using a connection, and a DataSource object can
be thought of as a factory for connections to the particular data source that the
DataSource instance represents. In a basic DataSource implementation, a call to the
getConnection method returns a connection object that is a physical connection to the
data source.
tio
Q.2) How can you retrieve information from a ResultSet?
w
.a
p
A. By invoking the method get(..., String type) on the ResultSet, where type is the
database type
B. By invoking the method get(..., Type type) on the ResultSet, where Type is an object
which represents a database type
C. By invoking the method getValue(...), and cast the result to the desired Java type.
D. By invoking the special
getBoolean (...), getClob(...),...
gettermethods
on
the
ResultSet:
getString(...),
w
w
ANSWER : By invoking the special gettermethods on the ResultSet:
getString(...), getBoolean (...), getClob(...),...
SOLUTION :
A ResultSet object maintains a cursor that points to the current row in the result set. The
term "result set" refers to the row and column data contained in a ResultSet object.
When iterating the ResultSet you want to access the column values of each record. You
do so by calling one or more of the many getXXX() methods. You pass the name of the
column to get the value of, to the many getXXX() methods. For instance:
There are a lot of getXXX() methods you can call, which return the value of the column
as a certain data type, e.g. String, int, long, double, BigDecimal etc. They all take the
name of the column to obtain the column value for, as parameter.
Q.3) Which type of driver converts JDBC calls into the network protocol used by the
database man-agement system directly?
A. Type 1 driver
B. Type 2 driver
C. Type 3 driver
D. Type 4 driver
ANSWER : Type 4 driver
SOLUTION :
The JDBC type 4 driver, also known as the Direct to Database Pure Java Driver, is a
database driver implementation that converts JDBC calls directly into a vendor-specific
database protocol.
Written completely in Java, type 4 drivers are thus platform independent. They install
inside the Java Virtual Machine of the client. This provides better performance than the
type 1 and type 2 drivers as it does not have the overhead of conversion of calls into
ODBC or database API calls. Unlike the type 3 drivers, it does not need associated
software to work.
Page 1
Questions and Answers
Q.4) What information may be obtained from a ResultSetMetaData object?
e.
co
m
A. Number of columns in the result set
B. Number of rows in the result set
C. Database URL and product name
D. JDBC driver name and version
ANSWER : Number of columns in the result set
SOLUTION :
The ResultSetMetaData. getColumnType(int column) returns a int value specifying the
column type found in java.sql.Types.
The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of column, column name, column
type etc. , ResultSetMetaData interface is useful because it provides methods to get
metadata from the ResultSet object.
in
Q.5) How do you use a savepoint?
A. A savepoint is realised by calling setAutoCommit(true) on the connection
nl
B. A savepoint is activated by the method setSavePoint(mysavepoint^A
tio
Q.6) What happens if you call deleteRow() on a ResultSet object?
w
.a
p
A. The row you are positioned on is deleted from the ResultSet, but not from the
database.
B. The row you are positioned on is deleted from the ResultSet and from the database
C. The result depends on whether the property synchronizeWithDataSource is set to
true or false
D. You will get a compile error: the method does not exist because you can not delete
rows from a ResultSet
w
w
ANSWER : The row you are positioned on is deleted from the ResultSet and from the
database
SOLUTION :
We Know That , A ResultSet object maintains a cursor that points to the current row in
the result set. The term "result set" refers to the row and column data contained in a
ResultSet object.
When we called deleteRow()on ResultSet it delete Current record Pointed by ResultSet
Object.
Q.7) What is correct about DDL statements (create, grant,...)?
A. DDL statements are treated as normal SQL statements, and are executed by calling
the execute() method on a Statement (or a sub interface thereof) object
B. To execute DDL statements, you have to install additional support files
C. DDL statements can not be executed by making use of JDBC, you should use the
native database tools for this.
D. Support for DDL statements will be a feature of a future release of JDBC
ANSWER : DDL statements are treated as normal SQL statements, and are executed
by calling the execute() method on a Statement (or a sub interface thereof) object
SOLUTION :
Data definition language (DDL) is the set of SQL statements (ALTER, CREATE, DROP,
GRANT) that let you create, alter, or destroy (drop) the objects that make up a relational
database
DDL statements are treated as normal SQL statements, and are executed by calling the
execute() method on a Statement (or a sub interface thereof) object
Q.8) How can you start a database transaction in the database?
Page 2
Questions and Answers
A. By asking a Transaction object to your Connection, and calling the method begin() on
it
B. By asking a Transaction object to your Connection, and setting the autoCommit
property of the Transaction to false
C. By calling the method beginTransaction() on the Connection object
e.
co
m
D. By setting the autoCommit property of the Connection to false, and execute a
statement in the database
nl
in
ANSWER : By setting the autoCommit property of the Connection to false, and execute
a statement in the database
SOLUTION :
If your JDBC Connection is in auto-commit mode, which it is by default, then every SQL
statement is committed to the database upon its completion. That may be fine for simple
applications, but there are three reasons why you may want to turn off auto-commit and
manage your own transactions:
To increase performance
To maintain the integrity of business processes
To use distributed transactions
Transactions enable you to control if, and when, changes are applied to the database. It
treats a single SQL statement or a group of SQL statements as one logical unit, and if
any statement fails, the whole transaction fails.
tio
Q.9) Which statements about JDBC are true? (2 answers)
A. JDBC is an API to connect to relational-, object- and XML data sources
w
.a
p
B. JDBC stands for Java DataBase Connectivity
C. JDBC is an API to access relational databases, spreadsheets and flat files
D. JDBC is an API to bridge the object-relational mismatch between OO programs and
rela-tional databases
w
ANSWER : JDBC stands for Java DataBase Connectivity
JDBC is an API to access relational databases, spreadsheets and flat files
w
SOLUTION :
JDBC stands for Java Database Connectivity,
which is a standard Java API for database-independent connectivity
between the Java programming language and a wide range of databases.
Fundamentally, JDBC is a specification that provides a complete set of
interfaces that allows for portable access to an underlying database. Java can be used
to write different types of executables, such as:
Java Applications
Java Applets
Java Servlets
Java ServerPages (JSPs)
Enterprise JavaBeans (EJBs)
Q.10) What statements are correct about batched insert and updates? (2 answers)
A. To create a batch of insert and update statements, you create an object of type
Batch, and call the method addStatement(String statement) for each statement you
want to execute in the batch
B. Batch insert and updates are only possible when making use of parameterized
queries.
C. To do a batched update/insert, you call addBatch(String statement) on a Statement
object for each statement you want to execute in the batch
D. To execute a batched update/insert, you call the executeBatch() method on a
State-ment object
ANSWER : To do a batched update/insert, you call addBatch(String statement) on a
Page 3
Questions and Answers
Statement object for each statement you want to execute in the batch
To execute a batched update/insert, you call the executeBatch() method on a
State-ment object
e.
co
m
SOLUTION :
Batch Processing allows you to group related SQL statements into a batch and submit
them with one call to the database.
When you send several SQL statements to the database at once, you reduce the
amount of communication overhead, thereby improving performance.
Using addBatch(String statement) we can add multiple sql statement
And to execute that statement we need to call executeBatch() method on a Statement
object
Q.11) Which type of Statement can execute parameterized queries?
nl
in
A. PreparedStatement
B. ParameterizedStatement
C. ParameterizedStatement and CallableStatement
D. All kinds of Statements (i.e. which implement a sub interface of Statement)
w
w
.a
p
tio
ANSWER : PreparedStatement
SOLUTION :
The main feature of a PreparedStatement object is that, unlike a Statement object, it is
given a SQL statement when it is created. The advantage to this is that in most cases,
this SQL statement is sent to the DBMS right away, where it is compiled. As a result, the
PreparedStatement object contains not just a SQL statement, but a SQL statement that
has been precompiled. This means that when the PreparedStatement is executed, the
DBMS can just run the PreparedStatement SQL statement without having to compile it
first.
Although PreparedStatement objects can be used for SQL statements with no
parameters, you probably use them most often for SQL statements that take
parameters. The advantage of using SQL statements that take parameters is that you
can use the same statement and supply it with different values each time you execute it.
Examples of this are in the following sections.
Q.12) Which packages contain the JDBC classes?
w
A. java.jdbc and javax.jdbc
B. java.jdbc and java.jdbc.sql
C. java.sql and javax.sql
D. java.rdb and javax.rdb
ANSWER : java.sql and javax.sql
SOLUTION :
Before you can invoke JDBC methods, you need to be able to access all or parts of
various Java(TM) packages that contain those methods. You can do that either by
importing the packages or specific classes, or by using the fully-qualified class names.
You might need the following packages or classes for your JDBC program:
java.sql
Contains the core JDBC API.
javax.naming
Contains classes and interfaces for Java Naming and Directory Interface (JNDI), which
is often used for implementing a DataSource.
javax.sql
Contains JDBC 2.0 standard extensions.
Q.13) How can you execute a stored procedure in the database?
A. Call method execute() on a CallableStatement object
B. Call method executeProcedure() on a Statement object
C. Call method execute() on a StoredProcedure object
Page 4
Questions and Answers
D. Call method run() on a ProcedureCommand object
w
.a
p
tio
nl
in
e.
co
m
ANSWER : Call method execute() on a CallableStatement object
SOLUTION :
Just as a Connection object creates the Statement and PreparedStatement objects, it
also creates the CallableStatement object which would be used to execute a call to a
database stored procedure.
Example:
Suppose, you need to execute the following Oracle stored procedure:
CREATE OR REPLACE PROCEDURE getEmpName
(EMP_ID IN NUMBER,
EMP_FIRST OUT VARCHAR) AS
BEGIN
SELECT first INTO EMP_FIRST
FROM Employees
WHERE ID = EMP_ID;
END;
CallableStatement cstmt = null;
try
{
String SQL = "{call getEmpName (?, ?)}";
cstmt = conn.prepareCall (SQL);
...
}
catch (SQLException e)
{
...
}
Finally
{
...
}
Q.14) Which of the following statements are true?
w
w
A. PreparedStatement is a subinterface of Statement
B. The parameters in a prepared statement is denoted using the ? sign.
C. PreparedStatement is for SQL query statements only. You cannot create a
PreparedStatement for SQL update statements.
D. PreparedStatement is efficient for repeated executions.
ANSWER : 1) PreparedStatement is a subinterface of Statement 2)The parameters in a
prepared statement is denoted using the ? sign. 3) PreparedStatement is efficient for
repeated executions.
SOLUTION :
The PreparedStatement interface provides the following principal functions:
Execution of SQL statements in which the ? parameter is specified
Specification of the ? parameter
Generation and return of a ResultSet object as a retrieval result
Return of the number of updated rows as an updating result
Because the PreparedStatement interface is a subinterface of the Statement interface, it
inherits all of the Statement interface functions.
Q.15) How can you execute DML statements (i.e. insert, delete, update) in the
database?
A. By making use of the InsertStatement, DeleteStatement or UpdateStatement classes
B. By invoking the execute(...) or executeUpdate(...) method of a normal Statement
object or a sub-interface object thereof
C. By invoking the executeInsert(...), executeDelete(...) or executeUpdate(...) methods
of the DataModificationStatement object
D. By making use of the execute(...) statement of the DataModificationStatement object
Page 5
Questions and Answers
ANSWER : By invoking the execute(...) or executeUpdate(...) method of a normal
Statement object or a sub-interface object thereof
SOLUTION :
The Statement Interface provides a Following Methods. That Facility Acording to
requirement.
e.
co
m
executeQuery() This is used generally for reading the content of the database. The
output will be in the form of ResultSet. Generally SELECT statement is used.
executeUpdate() This is generally used for altering the databases. Generally DROP
TABLE or DATABASE, INSERT into TABLE, UPDATE TABLE, DELETE from TABLE
statements will be used in this. The output will be in the form of int. This int value
denotes the number of rows affected by the query.
in
execute() If you dont know which method to be used for executing SQL statements, this
method can be used. This will return a boolean. TRUE indicates the result is a ResultSet
and FALSE indicates it has the int value which denotes number of rows affected by the
query.
nl
Q.16) Are ResultSets updateable?
w
.a
p
tio
A. Yes, but only if you call the method openCursor() on the ResultSet, and if the driver
and database support this option
B. Yes, but only if you indicate a concurrency strategy when executing the statement,
and if the driver and database support this option
C. Yes, but only if the ResultSet is an object of class UpdateableResultSet, and if the
driver and database support this option
D. No, ResultSets are never updateable. You must explicitly execute DML statements
(i.e. insert, delete and update) to change the data in the underlying database
w
w
ANSWER : Yes, but only if you indicate a concurrency strategy when executing the
statement, and if the driver and database support this option
SOLUTION :
To use the result set update methods, the concurrency mode for the result set must be
ResultSet.CONCUR_UPDATABLE
Updatable result sets in Derby can be updated by using result set update methods
(updateRow(),deleteRow() and insertRow()), or by using positioned update or delete
queries.
Both scrollable and non-scrollable result sets can be updatable in Derby.
If the query which was executed to create the result set is not updatable, Derby will
downgrade the concurrency mode to ResultSet.CONCUR_READ_ONLY, and add a
warning about this on the ResultSet. The compilation of the query fails if the result set
cannot be updatable, and contains a FOR UPDATE clause.
Q.17) Which of the following are interfaces?
A. DriverManager
B. Connection
C. Statement
D. ResultSet
ANSWER : Connection,Statement ,ResultSet
SOLUTION :
Beacause Only DriverManager is a class which extends Object Class Which is Cosmic
Super Class The DriverManager provides a basic service for managing a set of JDBC
drivers.
Others are the interfaces that provides a classes and methods over the JDBC
Q.18) What statements are correct about JDBC transactions (2 correct answers)?
A. A transaction is a set of successfully executed statements in the database
Page 6
Questions and Answers
B. A transaction is finished when commit() or rollback() is called on the Connection
object,
C. A transaction is finished when commit() or rollback() is called on the Transaction
object
D. A transaction is finished when close() is called on the Connection object.
e.
co
m
ANSWER : 1.A transaction is finished when commit() or rollback() is called on the
Connection object,
2.A transaction is finished when commit() or rollback() is called on the Transaction
object
SOLUTION :
In Case of JDBC transaction, there are two cases to complete the transaction
in
1. The changes had done was save permanently on database When we call commit On
JDBC object. Same thing appends when rollback( ) method of transaction is Called All
all the Changes done are rollbacked.
w
w
w
.a
p
tio
nl
2.Second one is when we called close( ). At that time a autoCommit ( )is called
automatically And save all changes in database permanently.
Page 7