Difference Between Spring JDBC Vs Plain JDBC?
Asked Answered
D

5

30

What is the main difference between Spring JDBC VS JDBC?

Dung answered 27/2, 2012 at 17:47 Comment(1)
Related question #51923560Vacuva
O
38

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.

Ormazd answered 27/2, 2012 at 18:9 Comment(0)
N
4

Spring JDBC value-add provided by the Spring Framework's on top JDBC layer

  1. Define connection parameters
  2. Open the connection
  3. Specify the statement
  4. Prepare and execute the statement
  5. Set up the loop to iterate through the results (if any)
  6. Do the work for each iteration
  7. Process any exception
  8. Handle transactions
  9. 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

Nessi answered 27/2, 2012 at 18:2 Comment(1)
Please cite your sources when you can: static.springsource.org/spring/docs/3.0.x/… All the more in this case since some of the items listed are not provided by Spring.Louislouisa
K
3

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.

Khasi answered 7/11, 2017 at 7:4 Comment(0)
S
1

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.

Slat answered 27/2, 2012 at 18:2 Comment(0)
D
1

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.

Deaver answered 1/11, 2018 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.