What is the main difference between Spring JDBC VS JDBC?
Let me show you some simple example using JDBC:
final Connection connection = ds.getConnection();
try {
final Statement statement = connection.createStatement();
try {
final ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM Orders");
try {
resultSet.next();
final int c = resultSet.getInt(1);
} finally {
resultSet.close();
}
} finally {
statement.close();
}
} finally {
connection.close();
}
It's much better when try-with-resources though:
try (
Connection connection = ds.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM Orders");
) {
resultSet.next();
final int c = resultSet.getInt(1);
}
Of course you can extract common code and use template method Design Pattern. Effectively you'd reinvent JdbcTemplate
:
final int c = new JdbcTemplate(ds).queryForInt("SELECT COUNT(*) FROM Orders");
Also Spring JDBC provides exception translation (no more checked SQLException
and differences between databases/dialects) and simple ORM capabilities.
Spring JDBC value-add provided by the Spring Framework's on top JDBC layer
- Define connection parameters
- Open the connection
- Specify the statement
- Prepare and execute the statement
- Set up the loop to iterate through the results (if any)
- Do the work for each iteration
- Process any exception
- Handle transactions
- Close the connection
Basically , you don't need to worry about managing and suffering from infrastructure/plumbing code and purely worry about data and its mapping to objects.
Spring utilizes Template pattern to hide all low level details while giving you extension hooks to extend and work with JDBC.
Also, there is a well defined API for database exceptions, that is really developer-friendly when compared to exception hierarchy provided by low level JDBC API's
1.) Spring jdbc module is an abstraction layer on top of jdbc technology, this layer avoids the boiler plate code used in jdbc programming. 2.) Spring ORM module is an abstraction layer on top ORM tools. 3.) When working with ORM tools like a hibernate again we have a Boiler plate code this spring ORM layer avoids boiler plate code of ORM tools.
Spring JDBC? I only know of a couple of Spring JDBC Templates.
They allow you to access JDBC functionality from within Spring container, and they provide some additional simplifications compared to plain JDBC, like connection management and exception handling.
Basically Spring is harder to setup but easier to develop with, so it all depends on the scope of the problem you're dealing with.
In my opinion, JDBC shares some functionalities with JDBCTemplate, but they can be quite different. The main differences that come to my mind at the first glance at this question are:
If you are handling a huge amount of data, say you want to insert into a database millions(or billions) of rows of records, you might want to use JDBC. In less than 4 minutes I inserted one million rows with different information into the database with JDBC and MySQL statements concatenation. In contrast, it will take me probably more than ten minutes to do the same thing with JDBC Template.
However, if you are using JDBC, more codes must be written to get things right, compared to the use of JDBC Template. Tomasz Nurkiewicz has illustrated this point by code and Santosh Gokak has provided a summary of steps to use JDBC. But there's one more thing implicit behind the scene, and that is the configuration of Spring, since JDBC Template is a part of Spring Framework. If you are new to XML/Annotation configuration for Spring, things might be a little complicated, while JDBC, though requiring more steps, is to some extent more intuitive.
© 2022 - 2024 — McMap. All rights reserved.