what is the current version of JDBC? and explain its
features?

Answer Posted / dinesh

The current version of JDBC is JDBC 4:
1.SQL 2003support.
2.XML as a First-Class SQL Data Type.
3.Using JDBC 4's SQLXML data type, you could retrieve a
user's blog.
4.Exceptional exceptions:
5,Type-safe querying and results:


In details about above one:
1.SQL 2003support
Connection c = myDataSource.getConnection();
PreparedStatement st = c.prepareStatement("insert into
siteusers (userid, username) values
(?, ?);
st.setRowId(1, rowId1);

When creating a new BLOB or CLOB object through
Connection's appropriate create() method,

the resulting object does not contain the actual binary or
character data. Rather, you would add

the "real" data to the CLOB or BLOB objects:

Connection c = myDataSource.getConnection();
Blob myBlob = c.createBlob();
OutputStream outStream = myBlob.setBinaryStream(0);

2.XML as a First-Class SQL Data Type
create table user_has_blog(userid int, blog_entry xml);

3.Using JDBC 4's SQLXML data type, you could retrieve a
user's blog entries as follows:
Connection c = myDataSource.getConnection();
PreparedStatement st = c.prepareStatement("select userid,
blog_entry from user_has_blog");
ResultSet rs = st.executeQuery();
while (rs.next()) {
SQLXML blog = st.getSQLXML("blog_entry");
javax.xml.stream.XMLStreamReader reader =
blog.createXMLStreamReader();
blog.free();
}

SQLXML's createXMLStreamReader() returns a StAX stream
reader, which is a low-level interface

allowing access to an XML stream . See the reference at
the end of this article about using StAX

to read and write XML data. Whenever you've exhausted the
XML stream, you need to invoke

close() on SQLXML to free resources associated with the XML
data stream.

.The following SQL selects all users with their first and
last names into XML User elements:
create table user (int userid, firstname varchar(128),
lastname varchar(128))
select e.userid,
XMLELEMENT(NAME
"user", e.firstname || e.lastname) as "result" from
employee e;


Creating and inserting into the database a new SQLXML value
is similar to working with BLOBs.

You invoke Connection's createSQLXML() method, and then
populate the resulting SQLXML's

input stream with the XML content:

Connection c = myDataSource.getConnection();
PreparedStatement st = c.prepareStatement("insert into
user_has_blog (userid, blog_entry)

values (?, ?)");
SQLXML blogEntry = c.createSQLXML();
Writer writer = blogEntry.createXMLSteamWriter();
//write XML content to writer
st.setInt(1, 1); //User id
st.setSQLXML(2, blogEntry)


4.Exceptional exceptions:
In JDBC4:SQL Exception has getNextException() and getCasuse
() methods
The method getNextException() :returns either the next
Exception in the exception chain, or null,

when the root of the hierarchy is reached.
The method getCasuse() : return a non-SQLException subtype
SQLTransientException :SQLTransientConnectionException,SQLTi
meoutException

,SQLTransactionRollbackException

SQLNonTransientException:SQLDataException

,SQLIntegrityConstraintViolationException,SQLInvalidAuthoriz
ationSpecException

,SQLNonTransientConnectionException ,SQLSyntaxErrorException


5,Type-safe querying and results:
Consider the class
class User {
int userID;
String name;
String department;
}


Consider the Table:
create table user (int userid, name varchar(128),
department varchar(128));

Template Class
class MyQueries {
public static final String SELECT_ALL_USERS
= "select * from user";
}


interface MyQueries extends BaseQuery {

@Query(sql="select * from user")
DataSet getAllUsers();
}

You are obtain an instance of MyQuery from the Connection
class:
DataSource myDataSource = new DataSource();
Connection c = myDataSource.getConnection();
MyQueries myQueries = c.createQueryObject(MyQuery.class);
DataSet users = myQueries.getAllUsers();
for (User u: users) {
System.out.println("User's name is: " + user.name;
}

//For ex getting dep details
interface MyQueries extends BaseQuery {

@Query(sql="select * from user where department=
{department}")
DataSet getDepartmentUsers(String department);
}

//For update query
interface MyQueries extends BaseQuery {

@Update(sql="update user set department={deparment}
where name= {userName}")
int updateDeparment(String userName, String
department);
}
Dataset allows you to insert and update data in the
database.
Connection c = myDataSource.getConnection();
MyQueries q = c.createQueryObject(MyQueries.class);
DataSet users = q.create();
User user = new User();
user.setUserID(1);
user.setName("Joe");
user.setDeparment("Accounting");
users.insert(user);


This are the advantage in JDBC 4 new version.

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is encrypted connection?

504


What are the main steps in java to make JDBC connectivity?

577


How to get the Database server details in java program?

574


What is JDBC Transaction Management and why do we need it?

549


What are the types of statements in jdbc?

529






How many locking systems are there in jdbc?

559


What does the connection object represents?

561


What is difference between java.util.Date and java.sql.Date?

540


Which is better jpa or jdbc?

507


What is the significance of DataBaseMetaData.tableIndexStatistics? How to obtain and use it?

516


What is JDBC Batch Processing and what are it’s benefits?

514


What are the different types of locking in JDBC?

574


How do I connect to jdbc?

522


Explain how data flows from view to db and reverse

1634


Why is jdbc used?

524