vendredi 6 mars 2015

how to make a simple call to a storeprocedure or function in spring mvc using JdbcTemplate

i have read a lot of questions about how to call a storeprocedure using JdbcTemplate there are a lot of methods like using a beanMapper, creating a rowMapper, using callableStatement, but i have seen a lot of persons that say this: "For simple procedures you may use jdbcTemplate's update method:"



jdbcTemplate.update("call SOME_PROC (?, ?)", param1, param2);


i tried doing that at and my jdbcTemplate variable is always null, this is my stored procedure



CREATE OR REPLACE PROCEDURE addProce
(
num1 IN number,
num2 IN number,
result OUT number
)
IS
BEGIN
result := num1 + num2;

END;
/


and this is the class where i call it



public class UsuerDAOImpl implements UserDAO {

private JdbcTemplate jdbcTemplate;

public UsuerDAOImpl () {}

public UsuerDAOImpl (DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}

public int addmethodJdbc()
{
int result= 0;
int someValue= jdbcTemplate.update("addProce(?, ?, ?)", 2, 1, result);

return someValue;
}
}


i have this method in my class and it works my jdbctemplate is not null in there



public void insertUser(User user) {

try {
String sql = "INSERT INTO USER"
+ "(a, b, c, d)"
+ " VALUES (?, ?, ?, ?)";

jdbcTemplate.update(sql, user.getA(),
usaurio.getB(),
usaurio.getC(),
usaurio.getD());
}
catch (DataAccessException dataAccessException)
{

dataAccessException.printStackTrace();
}
catch(Exception e)
{

e.printStackTrace();
}
}


i also try with a function here it is :



CREATE OR REPLACE FUNCTION addFunct
(
num1 IN number,
num2 IN number
)
return number
IS
resultado number;
BEGIN
result := num1 + num2;
return (result);
END;
/


but still dont work my jdbcTemplate is null too


i already know how to call them with the other ways, but i wanted to know how to call them in this easy way



jdbcTemplate.update("call SOME_PROC (?, ?)", param1, param2);

Aucun commentaire:

Enregistrer un commentaire