Show pageBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Spring Using DataSource or JdbcTemplate ====== ===== DataSource ===== Simply do <code> @Autowired DataSource dataSource; </code> and we can use the dataSource in our code like: <code> String sql = "YOUR_SQL_QUERY"; //such as "UPDATE Book SET NAME = '" + String.valueOf(iRandom) + "' WHERE ID = 1" Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement(); int iAffectedRowCount = statement.executeUpdate(sql); connection.close(); </code> of course, you need to do a try catch yourself. ===== JdbcTemplate ===== Likewise Spring also provided the JDBC template, all we need to do is: <code> @Autowired JdbcTemplate jdbcTemplate; </code> and we can use it like: <code> String sql = "YOUR_SQL_QUERY"; //such as "UPDATE Book SET NAME = '" + String.valueOf(iRandom) + "' WHERE ID = 1" int iAffectedRowCount = jdbcTemplate.update(sql); </code> of course, you need to do a try catch yourself. For more detail on how to use JdbcTemplate, go to the reference https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html springboot/spring_using_datasource_or_jdbctemplate.txt Last modified: 2020/06/01 10:32by chongtin