Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC and ADO.Net: API comparison

What are the analogies between the objects found in JDBC and the ones found in ADO.Net?

I know the object model in JDBC and ADO.Net are not exactly the same, but I think some analogies can be found among them (and key differences worth stating).

That would be useful for those who knows one API and wants to learn the other, serving as a starting point maybe, or avoiding misunderstandings caused by assumptions one makes about the API that wants to learn.

e.g.: Which is the ADO.Net object that provides the same functionality/behavior as the JDBC ResultSet? the same for PreparedStatemes, and so on...

like image 263
mmutilva Avatar asked Oct 27 '10 13:10

mmutilva


2 Answers

Here is a simple sequence for ADO.NET:

// 1. create a connection
SqlConnection conn = new SqlConnection(xyz)
// 2. open the connection
conn.Open();
// 3. create a command
 SqlCommand cmd = new SqlCommand("select * from xyz", conn);
// 4. execute and receive the result in a reader
SqlDataReader rdr = cmd.ExecuteReader();
// 5. get the results
while (rdr.Read())
{
//dosomething
}

Here is a simple sequence for JDBC:

// 1. create a connection
Connection con = DriverManager.getConnection(xyz);
// 2. create a statement     
Statement stmt = con.createStatement();
// 3. execute and receive results in a result set
ResultSet rs = stmt.executeQuery("SELECT * from xyz");
// 4. Get the results
while (rs.next()) 
{
// do something
}

And here is the analogy (ADO.NET => JDBC):

SqlConnection => Connection
SqlCommand => Statement
SqlDataReader => ResultSet
like image 81
Amr H. Abd Elmajeed Avatar answered Nov 15 '22 09:11

Amr H. Abd Elmajeed


Not very thorough with jdbc, but from what I know ADO.NET follows a disconnected architecture, where a connection is established only for the time a query has to be executed or read. Once the reader is read, connection can be closed. The data caching are achieved using datasets and data adapters. In ADO.NET only one reader is allowed per connection. While disconnected architecture is certainly possible in jdbc, its built on the concept of having live connection where you can have multiple readers per connection.

Another difference in the API is that there is built in functionality in jdbc to get the last inserted id, while ADO lacks one.

Also read a nice comparison on data caching in ADO and jdbc.

like image 21
nawfal Avatar answered Nov 15 '22 08:11

nawfal