Java – differences between JDBC
•
Java
What is the main difference between spring JDBC and JDBC?
Solution
Let me look at a simple example of 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 to try to use resources:
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 design patterns using the template method Effectively, you will recreate the jdbctemplate:
final int c = new JdbcTemplate(ds).queryForInt("SELECT COUNT(*) FROM Orders");
Spring JDBC also provides exception conversion (no longer checking the difference between sqlexception and database / dialect) and simple ORM functions
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码