Simply do
@Autowired DataSource dataSource;
and we can use the dataSource in our code like:
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();
of course, you need to do a try catch yourself.
Likewise Spring also provided the JDBC template, all we need to do is:
@Autowired JdbcTemplate jdbcTemplate;
and we can use it like:
String sql = "YOUR_SQL_QUERY"; //such as "UPDATE Book SET NAME = '" + String.valueOf(iRandom) + "' WHERE ID = 1" int iAffectedRowCount = jdbcTemplate.update(sql);
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