Is Spring too complicated for JDBC operations?

Just looked at the Spring framework for JDBC - it looks like there is a bit of a learning curve - and I still can't find a good updated Spring / JDBC quick start tutorial on any quality!

Is there anything easier than Spring for basic JDBC operations, or who has some good links for tutorials.

Many thanks

+3
source share
5 answers

Quite the contrary. Spring JDBC support is very simple. Here is a basic example:

dataSource = ... obtain data source... (e.g. via Spring config)
SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
Map<String, Object> row = jdbcTemplate.queryForMap(
        "SELECT * FROM MyTable WHERE ID=? LIMIT 1", 100);

JdbcTemplate and SimpleJdbcTemplate contain many query methods that you might find useful. To match strings with your objects, take a look at RowMapper and ParameterizedRowMapper <T>.

DataSource . BasicDataSource :

BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("driverClassName");
ds.setUrl("jdbc://...");
ds.setUsername("username");
ds.setPassword("password");
+17

http://static.springframework.org/spring/docs/2.5.x/reference/jdbc.html, ( 'automagic' Spring , ) JdbcTemplate.

,

int countOfActorsNamedJoe =
    this.jdbcTemplate.queryForInt(
        "select count(0) from t_actors where first_name = ?",
        new Object[]{"Joe"});

, . , Spring JDBC , Spring JDBC. , .. Spring, .

+6

Spring .

Spring , Spring .., .

Spring - LOT JDBC. - DBCP

+2

Spring JDBC was good in version 1.0, but they reorganized it quite a bit in version 2.5 to make it even easier. Take a look at the JdbcTemplate and classes in the org.springframework.jdbc.core.simple package. They are new to Spring 2.5, so you will not find them in old books. It is best to look at reference documents online.

+1
source

Source: https://habr.com/ru/post/1702640/


All Articles