Professional Documents
Culture Documents
JDBC
JDBC
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 1 of Resources Pvt. Ltd.
Programming in Java
Introduction to RDBMS
• RDBMS or relational database management system is a software
meant to store and maintain huge amount of data in a structured
form of rows and columns.
• RDBMS is based on relational model which maintains tables with
enforced relationships.
• RDBMS helps us to access records by multiple users.
• Examples: Oracle, MySQL, MS SQL Server, IBM DB2
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 2 of Resources Pvt. Ltd.
Programming in Java
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 3 of Resources Pvt. Ltd.
Programming in Java
JDBC
JDBC is a Java API that connects to a relational databases,
JDBC helps us to use DCL, DDL, DML and DQL languages of SQL to
operate on RDBMS
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 4 of Resources Pvt. Ltd.
Programming in Java
JDBC connectivity (contd)
• Java has defined its own API (JDBC API) using JDBC drivers
(written in Java language). Hence JDBC achieves true platform
independence.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 5 of Resources Pvt. Ltd.
Programming in Java
Data flow from Java application to database
JAVA applications
JDBC API
JDBC DB Driver
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 6 of Resources Pvt. Ltd.
Programming in Java
Scope of the tutorial
• We will see a step by step approach to connect Java application to a
database like MYSQL using JDBC.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 7 of Resources Pvt. Ltd.
Programming
Example in Java
to connect MySQL database
Before going deep inside the concepts of JDBC and connectivity between MySQL
database, we will see a sample program that illustrates various steps involved in
connectivity from Java application and RDBMS
import java.sql.*; /* Creating query statement*/
class MysqlCon{ Statement stmt=con.createStatement();
public static void main(String args[]) ResultSet rs=stmt.executeQuery("select * from
{ emp");
try{
/* Extracting result from ResultSet */
/* Registering driver */ while(rs.next())
Class.forName("com.mysql.jdbc.Driver"); System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));
/* Establishing connection */
Connection con=DriverManager.getConnectio /* Close the connection */
n("jdbc:mysql://localhost:3306/ con.close();
sys","root","root"); }
catch(Exception e){ System.out.println(e);}
/* Here localhost is the DB Server }
Sys is the database, root and root are }
credentials*/
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 8 of Resources Pvt. Ltd.
Programming in Java
DATABASE CREATION
Create Database
CREATE DATABASE is the SQL command for creating a database.
Example
CREATE DATABASE College;
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 9 of Resources Pvt. Ltd.
Programming in Java
Creating a table in the database
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 10 of Resources Pvt. Ltd.
Programming in Java
Downloading JDBC Driver
JDBC Driver is a software component that enables java application to
interact with the database.
Out of the four, Network protocol driver and Thin driver are widely used
as they are written fully in Java.
oracle.jdbc.driver.OracleDriver
com.mysql.jdbc.Driver
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 11 of Resources Pvt. Ltd.
Programming in Java
Register the Driver class
The forName() method of Class class is used to register the driver class.
public static void forName(String className)throws ClassNotFound
Exception
Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 12 of Resources Pvt. Ltd.
Programming in Java
Create Connection Object
1) public static Connection getConnection(String url)throws SQLExce
ption
2) public static Connection getConnection(String url,String name,Strin
g password) throws SQLException
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:db_name","system","password");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_name","root","root");
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 13 of Resources Pvt. Ltd.
Programming in Java
public Statement createStatement()throws SQLException
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 14 of Resources Pvt. Ltd.
Programming in Java
Execute the Query
public ResultSet executeQuery(String sql) throws SQLException
ResultSet rs=stmt.executeQuery("select * from students");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 15 of Resources Pvt. Ltd.
Programming in Java
Close the connection
public void close()throws SQLException
Con.close();
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 16 of Resources Pvt. Ltd.
Programming
Example in Java
to connect Oracle database
In this example, we are connecting to an Oracle database and getting data
from students table. Here, system and oracle are the username and password
of the Oracle database
import java.sql.*; //step4 execute query
class OracleCon{ ResultSet rs=stmt.executeQuery("select *
public static void main(String args[]) from students");
{ while(rs.next())
try{ System.out.println(rs.getInt(1)+" "+rs.ge
//step1 load the driver class tString(2)+" "+rs.getString(3));
Class.forName("oracle.jdbc.driver.Oracle
Driver"); //step5 close the connection object
con.close();
//step2 create the connection object
Connection con=DriverManager.getCon }catch(Exception e)
nection( { System.out.println(e);}
"jdbc:oracle:thin:@localhost:1521:xe","sy
stem","oracle"); }
}
//step3 create the statement object
Statement stmt=con.createStatement();
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 17 of Resources Pvt. Ltd.
Programming
Example in Java
to connect Oracle database
To connect java application with the Oracle database ojdbc14.jar file is required
to be loaded.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 18 of Resources Pvt. Ltd.
Programming
Example in Java
to connect MySQL database
In this example, we are connecting to an Oracle database and getting data
from students table. Here, root and root are the username and password of
the MySQL database
import java.sql.*; while(rs.next())
class MysqlCon{ System.out.println(rs.getInt(1)+" "+rs.ge
public static void main(String args[]) tString(2)+" "+rs.getString(3));
{ con.close();
try{ }
Class.forName("com.mysql.jdbc.Driver") catch(Exception e)
; { System.out.println(e);}
Connection con=DriverManager.getCon }
nection( }
"jdbc:mysql://localhost:3306/
sys","root","root");
//
here sonoo is database name, root is user
name and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select
* from emp");
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 19 of Resources Pvt. Ltd.
Programming
Example in Java
to connect MySQL database
To connect java application with the mysql database, mysqlconnector.jar file
is required to be loaded.
MySQL provides standards-based drivers for JDBC, ODBC, and .Net enabling
developers to build database applications in their language of choice. In addition,
a native C library allows developers to embed MySQL directly into their
applications.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 20 of Resources Pvt. Ltd.
Programming
Statement in Java
interfaces
To interact with database, we must use various type of commands in JDBC.
These interfaces define the methods and properties needed to send SQL or
PL/SQL commands and receive data from the database.
They also define methods that help bridge data type differences between Java
and SQL data types used in a database.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 21 of Resources Pvt. Ltd.
Programming
Statement in Java
interfaces
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 22 of Resources Pvt. Ltd.
Programming
Creating in Java
Statement Object
Once a connection is obtained we can interact with the database.
The JDBC Statement is used only to perform static operations without any
parameters, this can be useful only for select statement without parameters.
In order to perform interfaces define the methods and properties that enables to
send SQL or PL/SQL commands and receive data from the database like
DDL or DML queries PreparedStatement and CallableStatement objects are
used
They also define methods that help bridge data type differences between Java
and SQL data types used in a database.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 23 of Resources Pvt. Ltd.
Programming
Creating in Java
Prepared Statement Object
The DML operations like INSERT,DELETE and UPDATE— which are used to to
write operations are done using prepareStatement() method of the Connection
object created above. A call to this method takes variable bind parameters as
input parameters and creates an object instance of the PreparedStatement class.
dml_stmt.setInt(1, val);
Here 1 denotes that the first bind parameter is being set and val denotes an
integer variable holding a value.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 24 of Resources Pvt. Ltd.
Programming
Creating in Java
Prepared Statement Object
Once a PreparedStatement object has been constructed, the next step is to
execute the associated INSERT or UPDATE statement. This is done by using
the executeUpdate() method of the PreparedStatement object. The following line
of code illustrates this using the dml_stmt object created above:
dml_stmt.executeUpdate();
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 25 of Resources Pvt. Ltd.
Programming
Creating in Java
Prepared Statement Object
Sample code snippet
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 26 of Resources Pvt. Ltd.
Programming
Creating in Statement
Callable Java Object
Callable Statements
Callable statements are used for calling Oracle/MySQL stored procedures from
Java application.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 27 of Resources Pvt. Ltd.
Programming
Creating in Statement
Callable Java Object
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 28 of Resources Pvt. Ltd.
Programming
Result Sets in Java
The SQL statements that read data from a database query, return the data in a
result set. The SELECT statement is the standard way to select rows from a
database and view them in a result set.
The java.sql.ResultSet interface represents the result set of a database query.
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.
The methods of the ResultSet interface can be broken down into three categories
Navigational methods: Used to move the cursor around.
Get methods: Used to view the data in the columns of the current row being
pointed by the cursor.
Update methods: Used to update the data in the columns of the current row.
The updates can then be updated in the underlying database as well.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 29 of Resources Pvt. Ltd.
Programming
Result Sets in Java
The cursor is movable based on the properties of the ResultSet. These properties
are designated when the corresponding Statement that generates the ResultSet is
created.
The first argument indicates the type of a ResultSet object and the second
argument is one of two ResultSet constants for specifying whether a result set is
read-only or updatable.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 30 of Resources Pvt. Ltd.
Programming
Result Sets in Java
Navigating a Result Set
There are several methods in the ResultSet interface that involve moving the
cursor, including −
S.N. Methods & Description
1 public void beforeFirst() throws SQLExceptionMoves the cursor just before the first row.
2 public void afterLast() throws SQLExceptionMoves the cursor just after the last row.
3 public boolean first() throws SQLExceptionMoves the cursor to the first row.
4 public void last() throws SQLExceptionMoves the cursor to the last row.
5 public boolean absolute(int row) throws SQLExceptionMoves the cursor to the specified row.
6 public boolean relative(int row) throws SQLExceptionMoves the cursor the given number of rows forward or backward,
from where it is currently pointing.
7 public boolean previous() throws SQLExceptionMoves the cursor to the previous row. This method returns false if the
previous row is off the result set.
8 public boolean next() throws SQLExceptionMoves the cursor to the next row. This method returns false if there are no more
rows in the result set.
9 public int getRow() throws SQLExceptionReturns the row number that the cursor is pointing to.
10 public void moveToInsertRow() throws SQLExceptionMoves the cursor to a special row in the result set that can be used to
insert a new row into the database. The current cursor location is remembered.
11 public void moveToCurrentRow() throws SQLExceptionMoves the cursor back to the current row if the cursor is currently
at the insert row; otherwise, this method does nothing
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 31 of Resources Pvt. Ltd.
Programming
Result Sets in Java
Viewing a Result Set
The ResultSet interface contains dozens of methods for getting the data of the
current row.
There is a get method for each of the possible data types, and each get method
has two versions −
For example, if the column you are interested in viewing contains an int, you
need to use one of the getInt() methods of ResultSet −
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 32 of Resources Pvt. Ltd.
Programming
Result Sets in Java
Updating a Result Set
The ResultSet interface contains a collection of update methods for updating the
data of a result set.
As with the get methods, there are two update methods for each data type −
One that takes in a column name.
One that takes in a column index.
For example, to update a String column of the current row of a result set, you
would use one of the following updateString() methods −
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 33 of Resources Pvt. Ltd.
Programming
Result in Java
Set Metadata
Java ResultSetMetaData Interface
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.
Method Description
public String getColumnTypeName(int it returns the column type name for the
index)throws SQLException specified index.
public String getTableName(int it returns the table name for the specified
index)throws SQLException column index.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 34 of Resources Pvt. Ltd.
Programming in Java
DataBaseMetadata
Java DatabaseMetaData interface
DatabaseMetaData interface provides methods to get meta data of a database
such as database product name, database product version, driver name, name of
total number of tables, name of total number of views etc.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 35 of Resources Pvt. Ltd.
Programming
BLOB inObjects
and CLOB Java
In the JDBC API,
java.sql.Blob is the mapping for the SQL BLOB (binary large object)
type;
java.sql.Clob is the mapping for the SQL CLOB (character large object)
type.
BLOB and CLOB objects are collectively referred to as LOBs (large objects).
Use the SQL CLOB type for columns which hold very large string values.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 36 of Resources Pvt. Ltd.
Programming
Storing inDatabase
image in Java
One can store images in the database in java by the help
of PreparedStatement interface.
1) public void setBinaryStream(int paramIndex,InputStream stream)
throws SQLException
2) public void setBinaryStream(int paramIndex,InputStream stream,long leng
th) throws SQLException
For storing image into the database, BLOB (Binary Large Object) datatype is
used in the table. For example:
CREATE TABLE "IMGTABLE"
( "NAME" VARCHAR2(4000),
"PHOTO" BLOB
)
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 37 of Resources Pvt. Ltd.
Programming
Retrieving in in
image Java
Database
To retrieve image from Oracle database
By the help of PreparedStatement we can retrieve and store the image in the database.
PreparedStatement ps=con.prepareStatement("select * from imgtable");
ResultSet rs=ps.executeQuery();
if(rs.next()){//now on 1st row
Blob b=rs.getBlob(2);//2 refers to data in 2nd column
byte barr[]=b.getBytes(1,(int)b.length());//1 refers to 1st image
FileOutputStream fout=new FileOutputStream("d:\\sample.jpg");
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 38 of Resources Pvt. Ltd.
Programming
Storing in Java
file in Oracle database
The setCharacterStream() method of PreparedStatement is used to set character
information into the parameterIndex.
For storing file into the database, CLOB (Character Large Object) datatype is used in the
table. For example:
CREATE TABLE "FILETABLE"
( "ID" NUMBER,
"NAME" CLOB
)
// Code snippet
PreparedStatement ps=con.prepareStatement(
"insert into filetable values(?,?)");
File f=new File("d:\\myfile.txt");
FileReader fr=new FileReader(f);
ps.setInt(1,101);
ps.setCharacterStream(2,fr,(int)f.length());
int i=ps.executeUpdate();
con.close();
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 39 of Resources Pvt. Ltd.
Programming
Retrieving in Java
file from Oracle database
The getClob() method of PreparedStatement is used to get file information from the
database.
// Code snippet
PreparedStatement ps=con.prepareStatement("select * from filetable");
ResultSet rs=ps.executeQuery();
rs.next();//now on 1st row
Clob c=rs.getClob(2);
Reader r=c.getCharacterStream();
FileWriter fw=new FileWriter("d:\\retrivefile.txt");
int i;
while((i=r.read())!=-1)
fw.write((char)i);
fw.close();
con.close();
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 40 of Resources Pvt. Ltd.
Programming
Transaction in Java in JDBC
management
A database transaction is a sequence of actions that are treated as a single unit of work.
These actions should either complete entirely or take no effect at all. Transaction
management is an important part of RDBMS-oriented enterprise application to ensure
data integrity and consistency.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 41 of Resources Pvt. Ltd.
Programming
Transaction in Java in JDBC
management
Method Description
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 42 of Resources Pvt. Ltd.
Programming
Batch in in
processing Java
JDBC
Instead of executing a single query, we can execute a batch (group) of queries. It makes the
performance fast.
Method Description
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 43 of Resources Pvt. Ltd.
Programming
New in Java
features added in JDBC4.0
• Auto loading of JDBC driver class
• Connection management enhancements
• Support for ROWID in SQL. ROWID is a pseudocolumn
that defines a single row in a table
• Exception handling
• A new interface for SQLXML is added which is a mapping
for theXML data type in SQL
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 44 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 45 of Resources Pvt. Ltd.
Programming in Java
Answer:
b) Atomicity, Consistency, Isolation and Durability
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 46 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 47 of Resources Pvt. Ltd.
Programming in Java
Answer:
c) java.sql and javax.sql
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 48 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 49 of Resources Pvt. Ltd.
Programming in Java
Answer:
b)ResultSetMetaData
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 50 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 51 of Resources Pvt. Ltd.
Programming in Java
Answer:
c)PreparedStatements
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 52 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 53 of Resources Pvt. Ltd.
Programming in Java
Answer:
b) Moves the cursor to a row to the record of given index
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 54 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 55 of Resources Pvt. Ltd.
Programming in Java
Answer:
c) CallableStatements
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 56 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 57 of Resources Pvt. Ltd.
Programming in Java
Answer:
d) Both b and C
PreparedStatements &CallableStatements
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 58 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 59 of Resources Pvt. Ltd.
Programming in Java
Answer:
b) executeUpdate
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 60 of Resources Pvt. Ltd.
Programming in Java
MCQs
9.How do you get to know the warnings that are generated by SQL in your application
a.You must catch the checked SQLException which is thrown by the method that executes
the Statement
b.You must catch the unchecked SQLException which is thrown by the method that
executes the Statement
c.You must invoke the getWarnings method on the Statement object
d.You must query the ResultSet about the warnings.
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 61 of Resources Pvt. Ltd.
Programming in Java
Answer:
c) You must invoke the getWarnings method on the Statement object
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 62 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 63 of Resources Pvt. Ltd.
Programming in Java
Answer:
d) A transaction is finished when close() is called on the Connection
object
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 64 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 65 of Resources Pvt. Ltd.
Programming in Java
Answer:
c) Fast because it accesses the database only during commit Fast
because it accesses the database only during commit
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 66 of Resources Pvt. Ltd.
Programming in Java
MCQs
12. What happens if you call the method close() on a ResultSet object?
a. the method close() does not exist for a ResultSet. Only Connections can be closed.
b. the database and JDBC resources are released
c. you will get a SQLException, because only Statement objects can close ResultSets
d. the ResultSet, together with the Statement which created it and the Connection from
which the Statement was retrieved, will be closed and release all database and JDBC
resources
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 67 of Resources Pvt. Ltd.
Programming in Java
Answer:
b) the database and JDBC resources are released
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 68 of Resources Pvt. Ltd.
Programming in Java
MCQs
13. Which is the best way to store an attribute of large String value in SQL
a.SQL CLOB
b.SQL BLOB
c.SQL SLOB
d.SQL FLOB
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 69 of Resources Pvt. Ltd.
Programming in Java
Answer:
a) SQL CLOB
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 70 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 71 of Resources Pvt. Ltd.
Programming in Java
Answer:
c)Pseudo column that uniquely identifies a single row
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 72 of Resources Pvt. Ltd.
Programming in Java
MCQs
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 73 of Resources Pvt. Ltd.
Programming in Java
Answer:
c) executeBatch
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 74 of Resources Pvt. Ltd.
Programming in Java
Thank You!!!!
SMART
Ver 1.0 TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training
Slide 75 of Resources Pvt. Ltd.