Hint
This article is generated by AI translation.
Programmatic API
The programmatic API uses the core JdbcTemplate class to access the database in a code-driven way. Its biggest advantage is strong flexibility.
Highlights
- Focuses on programming flexibility, with SQL query strings as the primary usage scenario.
1. Create a JdbcTemplate
DataSource dataSource = ...
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
Or
Connection conn = ...
JdbcTemplate jdbc = new JdbcTemplate(conn);
2. Execute SQL
jdbc.execute("create table user_info (id int primary key, name varchar(50))");
// Using raw SQL
jdbc.executeUpdate("insert into user_info (id,name) values (1, 'Bob')");
// Using positional arguments
jdbc.executeUpdate("insert into user_info (id,name) values (?,?)",
new Object[] { 2, "Alice" });
// Using named arguments
Map<String, Object> queryArg = CollectionUtils.asMap("id", 3, "name", "David");
jdbc.executeUpdate("insert into user_info (id,name) values (:id, :name)",
queryArg);
// Map query results to any type
List<User> users = jdbc.queryForList("select * from user_info", User.class);
details