Spring - JDBC

springmvc

https://www.tutorialspoint.com/spring/spring_jdbc_example.htm
https://spring.io/guides/gs/relational-data-access/
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html
https://www.mkyong.com/spring/maven-spring-jdbc-example/
http://www.onlinetutorialspoint.com/spring/spring-jdbctemplate-crud-application.html
http://www.beingjavaguys.com/2013/07/spring-jdbc-template-with-spring-mvc.html
http://www.javatpoint.com/spring-JdbcTemplate-tutorial
http://stackoverflow.com/questions/21949897/how-to-connect-database-using-spring-xml-in-spring

How can we use Spring JDBC?

jdbcTemplate.update(“Your query”, parameter1)
jdbcTemplate.query(“Your query”, parameter1);

How can we implement a RowMapper?

class TodoMapper implements RowMapper<Todo> {
  public Todo mapRow(ResultSet rs, int rowNum) throws SQLException {
    Todo todo = new Todo();
    todo.setId(rs.getInt(1));
    todo.setDescription(rs.getString(2));
    todo.setDone(rs.getBoolean(3));
    return todo;
  }
}

How can we use Spring JDBC?

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.companyName.jdbc.hsql.HsqlDatabase;
import com.companyName.jdbc.model.Todo;

HsqlDatabase db = new HsqlDatabase();
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(db.conn, false));

jdbcTemplate.update(
  "INSERT INTO TODO(DESCRIPTION,IS_DONE) VALUES(?, ?)", 
  todo.getDescription(),
  todo.isDone()
);

jdbcTemplate.update("DELETE FROM TODO WHERE ID=?", id);

jdbcTemplate.query(“SELECT * FROM TODO”, new BeanPropertyRowMapper<ToDo>(Todo.class));

return jdbcTemplate.query("SELECT * FROM TODO", new TodoMapper());

class TodoMapper implements RowMapper<Todo> {
    public Todo mapRow(ResultSet rs, int rowNum) throws SQLException {
        Todo todo = new Todo();
        todo.setId(rs.getInt(1));
        todo.setDescription(rs.getString(2));
        todo.setDone(rs.getBoolean(3));
        return todo;
    }
}

jdbcTemplate.query(“SELECT * FROM TODO”, new RowMapper<Todo>() {
  return new Todo(rs.getInt(1), rs.getString(2), rs.getBoolean(3);
});
String trainingDataSourceURL = this.getPropValue("TRAINING_DATASOURCE_URL");
String trainingDataSourceUsername = this.getPropValue("TRAINING_DATASOURCE_USERNAME");
String trainingDataSourcePassword = this.getPropValue("TRAINING_DATASOURCE_PASSWORD");
SingleConnectionDataSource trainingDataSource = new SingleConnectionDataSource(
        "oracle.jdbc.driver.OracleDriver", trainingDataSourceURL, trainingDataSourceUsername,
        trainingDataSourcePassword, true);
//trainingDataSource.getConnection().setAutoCommit(true);
return new JdbcTemplate(trainingDataSource);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License