2014-03-04 30 views
0

如何使用散列映射或映射編寫相同的代碼。所以,我可以在表retrive值..如何使用散列映射編寫相同的代碼

 Class.forName("oracle.jdbc.driver.OracleDriver"); 
     Connection con=DriverManager.getConnection("jdbc:Oracle:thin:@localhost:1521:IHDB","pummy","pummy"); 

     PreparedStatement ps=con.prepareStatement("select workitemid,empname,salary from emp where product_id=? and operation_id=?"); 
     ps.setInt(1,prod_id); 
     ps.setInt(2,opn_id); 
     ResultSet rs=ps.executeQuery(); 
     System.out.println("ProdId: "+prod_id); 
     System.out.println(rs); 
     while(rs.next()) 
      { 
       BigDecimal workitemid = rs.getBigDecimal(1); 
       String empname=(String) rs.getString(2); 
       int salary= rs.getInt(3); 
       } 
+0

我猜你想返回一個'名單<地圖<字符串,對象>>' –

+0

你認爲任何張貼的答案是正確的答案呢?如果是這樣,upvote。 – Henrik

回答

0

我認爲,結構化的方式將是像下面創建Employee pojo class -

public class Employee{ 
    public BigDecimal workitemid; 
    public String empName; 
    public int salary; 
    <getter & setter> 
} 

現在創建一個Map - Map<BigDecimal,Employee> empMap = new HashMap<>();

while(rs.next()){ 
    Employee emp = new Employee(); 
    BigDecimal workitemid = rs.getBigDecimal(1); 
    emp.setWorkitemid(workitemid); 
    emp.setEmpName(rs.getString(2)); 
    emp.setSalary(rs.getInt(3)); 
    map.put(workitemid, emp); 
} 
0

你在。

PreparedStatement ps=con.prepareStatement("select workitemid,empname,salary from emp where product_id=? and operation_id=?"); 
      ps.setInt(1,prod_id); 
      ps.setInt(2,opn_id); 
      ResultSet rs=ps.executeQuery(); 
      System.out.println("ProdId: "+prod_id); 
      Map<BigDecimal,Employee> empMap= new HashMap<BigDecimal,Employee>(); 
      System.out.println(rs); 
      while(rs.next()) 
       { 
        Employee emp = new Employee(); 
        emp.setWorkItemId = rs.getBigDecimal(1); 
        emp.setEmpName(rs.getString(2)); 
        emp.setSalary(rs.getInt(3)); 
        empMap.put(emp.getWorkItemId,emp); 


        } 

    private class Employee{ 
     private String empName; 
     private int salary; 
     private BigDecimal workItemId; 
    //getters and setters 

    } 
0
PreparedStatement ps=con.prepareStatement("select workitemid,empname,salary from emp where product_id=? and operation_id=?"); 
    ps.setInt(1,prod_id); 
    ps.setInt(2,opn_id); 
    ResultSet rs=ps.executeQuery(); 
    System.out.println("ProdId: "+prod_id); 
    System.out.println(rs); 

    Map<BigDecimal, Employee> employees = new HashMap<BigDecimal, Employee>();   

    while(rs.next()) 
     { 
      BigDecimal workitemid = rs.getBigDecimal(1); 
      String empname=(String) rs.getString(2); 
      int salary= rs.getInt(3); 
      Employee employee = new Employee(workitemid, empname, salary); 

      employees.put(workitemid , employee); 
     } 

//object to hold information about an employee and to be able to use a map. 
    public class Employee 
    { 
    private BigDecimal workitemid; 
    private String empName; 
    private int salary; 

    //constructor 
    public Employee(BigDecimal workitemid, String empName, int salary) 
    { 
     this.workitemid = workitemid; 
     this.empName = empName; 
     this.salary = salary; 
    } 


    //setters and getter methods for workitemid, empName, salary. 
    } 
相關問題