Spring JDBC与普通JDBC之间的区别? [英] Difference Between Spring JDBC Vs Plain JDBC?
本文介绍了Spring JDBC与普通JDBC之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Spring JDBC VS JDBC之间的主要区别是什么?
What is the main difference between Spring JDBC VS JDBC?
推荐答案
让我向您展示一些使用JDBC的简单示例: / p>
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);
}
当然你可以提取常用代码并使用模板方法设计模式。有效地你重新发明 JdbcTemplate
:
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");
Spring JDBC也提供异常转换(不再检查 SQLException
和数据库/方言之间的差异)和简单的ORM功能。
Also Spring JDBC provides exception translation (no more checked SQLException
and differences between databases/dialects) and simple ORM capabilities.
这篇关于Spring JDBC与普通JDBC之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文