import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@GrabConfig(systemClassLoader=true)
@Grab(group='mysql', module='mysql-connector-java', version='5.1.6')
@Grab(group='org.springframework', module='spring-jdbc', version='3.0.5.RELEASE')
DataSource dataSource = new SimpleDriverDataSource(
Class.forName("com.mysql.jdbc.Driver").newInstance(),
"jdbc:mysql://localhost:3306/myDatabase",
"login",
"password");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
println jdbcTemplate.queryForList( """SELECT 1;""" )
C'est très pratique pour debugguer les erreurs du type :
Caught: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at test.run(test.groovy:19)
Aucun commentaire:
Enregistrer un commentaire