2014-09-06 2 views
0

동료가 Java 및 MySQL을 {send/receive} = [IN/OUT] 매개 변수에 HashMap으로 구성하고 결과를 HashMap으로 반환합니다.Java 및 JDBC를 사용하여 HashMap을 데이터베이스로 보내거나받는 방법은 무엇입니까?

구성하거나 타사 패키지를 구성해야합니까?

public void sendValuesTODatabase(String user_id, String userName) {   
    HashMap hm1 = new HashMap(); 
    hm1.put("1", user_id);  // Parameters according to the Type 
    hm1.put("2", username); 


    int nooftable = 3;   /* represents number of select statements used in Stored   
            Procedure */ 

    Vector vCols = new Vector(); 
    vCols.addElement("1");  // One Column used for selection in Mysql select query 1 
    vCols.addElement("2");  // Two Column used for selection in Mysql select query 2 
    vCols.addElement("1");  // one Column used for selection in Mysql select query 3 

    BlobValues bls = new BlobValues(); 
    HashMap hmap = (HashMap)bls.getGeneric(hm1,"DB_SCHEMA_NAME.SP_PACKAGE_NAME.PROCEDURE_NAME", 
        nooftable, vCols); 

    HashMap dBResult1 = (HashMap)hmap.get("GENERICJAVA1"); // Select stmt result1 in HashMap 
    HashMap dBResult2 = (HashMap)hmap.get("GENERICJAVA2"); // Select stmt result2 in HashMap 
    HashMap dBResult3 = (HashMap)hmap.get("GENERICJAVA3"); // Select stmt result3 in HashMap 
} 

답변

0

스프링 프레임 워크는이 시나리오에 대한 해결책을 제공합니다.

org.springframework.jdbc.object.StoredProcedure 또는 org.springframework.jdbc.core.simple.SimpleJdbcCall 클래스를 사용하고 입력 매개 변수 Map을 execute() 메소드에 전달할 수 있습니다.

반복 할 수있는 출력 매개 변수 Map을 반환합니다.

다음은 StoredProcedure 클래스의 예입니다. SimpleJdbcCall의

@Component 
public class ItemInsert extends StoredProcedure { 
public static final String SPROC_NAME = "schema.oracle_pkg.proc_name"; 
public static final String INPUT_PARAM = "input_prm_name"; 
public static final String OUPUT_PARAM = "output_prm_name"; 

@Inject 
public ItemInsert(DataSource ds) { 
super(ds, SPROC_NAME); 
declareParameter(new SqlParameter(INPUT_PARAM, Types.VARCHAR)); 
declareParameter(new SqlOutParameter(OUTPUT_PARAM, Types.NUMERIC)); 
compile(); 
} 

public Item insert(Item item) 
    throws DataAccessException { 
Map<String, Object> inputs = new HashMap<String, Object>(); 
inputs.put(INPUT_PARAM, item.getSomething()); 
Map<String, Object> output = super.execute(inputs); 
Object newId = output.get(OUTPUT_PARAM); 
if (newId != null) { 
    item.setId(Long.parseLong(newId.toString())); 
} 
return item; 
} 

예는 http://www.pretechsol.com/2014/04/how-to-call-stored-procedure-using.html#.VAtktsWSw1I

SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(template) 
    .withProcedureName("customerDetails"); 
    Map<String, Object> inParamMap = new HashMap<String, Object>(); 
    inParamMap.put("id", 1); 
    SqlParameterSource in = new MapSqlParameterSource(inParamMap); 
    Map<String, Object> simpleJdbcCallResult = simpleJdbcCall.execute(in); 
    System.out.println(simpleJdbcCallResult); 
+0

하이에서 찾을 수 있습니다 감사합니다 :) – randxy

관련 문제