postgresql - how to invoke a pl/pgsql from java -
my pl/pgsql function works fine. tested it
create or replace function topalerte() returns void $$ declare max_var_risk varchar(70); max_mvar_risk varchar(70); max_incvar_risk varchar(70); max_cvar_risk varchar(70); amount varchar(70); delta varchar(70); net_exposure varchar(70); begin truncate tops; select names risk risk.var_mc_risk =(select max(risk.var_mc_risk) risk) max_var_risk ; select names risk risk.mvar_mc_risk =(select max(risk.mvar_mc_risk) risk) max_mvar_risk ; select names risk risk.inc_var_mc_risk =(select max(risk.inc_var_mc_risk) risk) max_incvar_risk ; select names risk risk.cvar_mc_risk =(select max(risk.cvar_mc_risk) risk) max_cvar_risk ; select names risk risk.amount =(select max(risk.amount) risk) amount ; select names risk risk.delta =(select max(risk.delta) risk) delta; select names risk risk.net_exposure =(select max(risk.net_exposure) risk) net_exposure ; insert tops values (max_var_risk, max_mvar_risk, max_incvar_risk, max_cvar_risk,amount,delta,net_exposure); end ; $$ language 'plpgsql'; i want invoke spring boot, here dao:
public interface toprepository { public list gettopsalert()throws throwable; } here service(interface implementation):
public class topalertmetier implements toprepository{ @override public list gettopsalert() throws throwable { list<string> mylist= new arraylist<>(); class.forname("org.postgresql.driver"); connection connect= null; connect = (connection) drivermanager.getconnection("jdbc:postgresql://localhost:5432/bourse","postgres","123456"); java.sql.callablestatement proc = connect.preparecall("{topalerte()}"); proc.registeroutparameter(1, java.sql.types.varchar); proc.executequery(); resultset results = (resultset) proc.getobject(1); mylist.add(results.getstring(0)); mylist.add(results.getstring(1)); mylist.add(results.getstring(2)); mylist.add(results.getstring(3)); mylist.add(results.getstring(4)); mylist.add(results.getstring(5)); mylist.add(results.getstring(6)); return mylist; } and controller :
@restcontroller public class topcontroller { @autowired toprepository toprepository; @requestmapping(value="/alert",method = requestmethod.get) public @responsebody list alert() throws throwable{ return toprepository.gettopsalert(); } the list shows names of max values ,so have contain values of table "tops" when running server : mvn spring-boot: run log show exception :
field toprepository in com.friendly_road.flight.controller.topcontroller required bean of type 'com.friendly_road.flight.dao.toprepository' not found.
this way use stored procedures within dao kind of "handmade" , dangerous. implemented configuration steps done on every method invocation , error-prone. it's not configurable anymore related database configuration , on.
the text says using spring boot, why don't use datasourceautoconfiguration , maintain database configuration within application.properties / application.yml file? way let spring manage datasource. ease transactional handling , using connection pooling , it's lot more convenient.
here nice article that.
in addition that, if use spring data jpa repositories interface not have need implement procedure invocation on own. stored procedure call easy realized in manner (assuming have top entity):
@repository public interface toprepository extends crudrepository<top, long> { ... @procedure(procedurename = "topalerte") list gettopalerte(); ... }
Comments
Post a Comment