views:

1627

answers:

5

Just been looking at the Spring framework for JDBC - it looks like there is a bit of a learning curve - and I'm still not able to find a nice up to date quick start Spring/JDBC tutorial of any quality!

Is there something lighter than Spring for basic JDBC operations - or has anyone got any good links for tutorials

Many thanks

+4  A: 

yes, it has JdbcTemplate for that.

http://www.techfaq360.com/tutorial/spring/JdbcTemplate.jsp

01
+14  A: 

Quite the opposite. JDBC support in Spring is very simple. Here is 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 has lot of query methods you may find useful. For mapping rows to your objects, take a look at RowMapper and ParameterizedRowMapper < T >.

For your datasource you usually want to use some advanced DataSource with pooling support. For testing, simple BasicDataSource will do:

BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("driverClassName");
ds.setUrl("jdbc://...");
ds.setUsername("username");
ds.setPassword("password");
Peter Štibraný
+1. That is how I first got into Spring. JDBCTemplate lib shrank my code enormously b/c I was no longer in try catch finally hell. Straight JDBC is extremely verbose and tedious with all those checked exceptions everywhere.
Julien Chastang
Plus, SQLExceptions may not even be related to SQL - The DataAccessException thrown captures this point much better. If the network goes down, your SQL could have been perfect, but...SQLException!
MetroidFan2002
Yeah, I *love* DataAccessException hierarchy. It is much much easier to catch specific problems and not to be bothered by DB-specific error codes.
Peter Štibraný
+2  A: 

The Spring documentation is pretty good.

If that doesn't help the various Spring books such as Spring in Action etc are very good.

Spring is worth learning - you can get rid of a LOT of boiler plate JDBC code. It does a very good job of connection management - together with DBCP

Fortyrunner
The Spring documentation is definitely thorough. Not terribly easy to parse though. It degenerates into alphabet soup pretty rapidly. A bit dense for new to web people to get their heads around.
Brian Knoblauch
+6  A: 

Check out http://static.springframework.org/spring/docs/2.5.x/reference/jdbc.html to choose a style (full 'automagic' Spring vs. most of the work done by the programmer) and learn about the basic operations on JdbcTemplate.

The site has nice examples, like

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

Anyhow, you will need to invest some time into it. No matter which tutorial on Spring JDBC you will use, it will still be Spring JDBC underneath. And in this case it doesn't hurt to learn from the source, i.e. the Spring docs, which are quite well written.

eljenso
+1  A: 

Spring JDBC was good in version 1.0, but they refactored it quite a bit in version 2.5 to make it even simpler. Have a look at JdbcTemplate and the classes in the org.springframework.jdbc.core.simple package. They're new to Spring 2.5, so you won't find them in the older books. Best to look at the reference docs on-line.

duffymo
Is it best to stick with version 2.5x or move over to 3?
Vidar
I haven't moved to 3 myself, so I can't say. I don't believe there's much new in JDBC: http://www.devoxx.com/pages/viewpage.action?pageId=1704462
duffymo