gpt4 book ai didi

java - createStoredProcedureQuery 未解决

转载 作者:行者123 更新时间:2023-11-30 07:44:05 25 4
gpt4 key购买 nike

这是我的代码,createStoredProcedureQuery在eclipse下面有红线,并且没有解决,我尝试了很多方法仍然无法得到问题所在。

private static EntityManagerFactory factory;

public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory("mypersistenceUnit");
EntityManager em = factory.createEntityManager();

// Create call stored procedure
em.getTransaction().begin();
StoredProcedureQuery storedProcedure = em.**createStoredProcedureQuery**("getCode");
// set parameters
storedProcedure.registerStoredProcedureParameter("name", String.class, ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("num", String.class, ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("code", String.class, ParameterMode.OUT);

// execute SP
storedProcedure.execute();
// get result
String code= (String)storedProcedure.getOutputParameterValue("code");
System.out.println("code is: " + code);
em.getTransaction().commit();
em.close();
}

最佳答案

您需要声明并设置参数:

/*Declare parameters*/

storedProcedure.registerStoredProcedureParameter( "name", String.class,ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter( "num", String.class,ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("code", String.class, ParameterMode.OUT);



/*set parameter value*/
storedProcedure.setParameter("name", "value_name");
storedProcedure.setParameter("num", "value_num");
storedProcedure.execute();
// get result
String code= (String)storedProcedure.getOutputParameterValue("code");

--记住,存储过程中的参数名称必须与声明的相同

关于java - createStoredProcedureQuery 未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com