Mysql-Connector- Java与Spring-Jdbc之间的区别
作者:互联网
据我所知,我们使用Mysql-connector jar将Java应用程序连接到数据库.我正在阅读春季教程,并且上述两个内容都已通过maven添加.两者有什么区别?
解决方法:
MySQL Connector是允许Java与MySQL对话的驱动程序.
Spring JDBC是一个使编写JDBC代码更容易的库. JdbcTemplate特别有用.
在JdbcTemplate之前:
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
int count;
try {
connection = dataSource.getConnection();
statement = connection.createStatement();
rs = statement.executeQuery("select count(*) from foo");
if(rs.next()) {
count = rs.getInt(0);
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
} finally {
if(connection != null) {
try { connection.close(); } catch (SQLException exp) {}
}
if(statement != null) {
try { statement.close(); } catch (SQLException exp) {}
}
if(rs != null) {
try { rs.close(); } catch (SQLException exp) {}
}
}
在JdbcTemplate之后:
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
int count = jdbcTemplate.queryForObject("select count(*) from foo", Integer.class);
看看一种方式吸吮少得多吗?
标签:mysql-connector,jdbc,spring,spring-jdbc 来源: https://codeday.me/bug/20191027/1943921.html