简体   繁体   English

如何使用hashmap编写相同的代码

[英]How can I write the same code using hashmap

How can I write the same code using hashmap or map. 如何使用hashmap或map编写相同的代码。 So that I can retrive values in a table.. 这样我就可以检索表中的值。

        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);
              }

I think the structured way would be by creating Employee pojo class like below - 我认为结构化方式是通过创建Employee pojo class如下所示-

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

now create a Map - Map<BigDecimal,Employee> empMap = new HashMap<>(); 现在创建一个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);
}

There you are. 你在这。

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

    }
    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.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免重复代码初始化 hashmap 的 hashmap? - How can I avoid repeating code initializing a hashmap of hashmap? 使用Java,如何在不重复比较的情况下将HashMap中的每个条目与同一HashMap中的每个其他条目进行比较? - Using Java, how can I compare every entry in HashMap to every other entry in the same HashMap without duplicating comparisons? 如何使用HashMap模拟缓存 - How can I simulate cache using HashMap 如何使用esapi验证哈希图? - How can I validate hashmap using esapi? 如何组合包含相同类型的两个 HashMap 对象? - How can I combine two HashMap objects containing the same types? 如何在Java中创建哈希图的哈希图? - How can I create hashmap of hashmap in java? 如何使用pdfbox编写带有语法突出显示的java代码 - how can I write java code with syntax highlighting using pdfbox 如何在棋盘游戏中使用循环编写较短的代码 - How can I write shorter code by using loops in a board game 如何使用泛型结合两个包含相同类型的HashMap对象,并控制重复的情况 - How can I combine two HashMap objects containing the same types using Generics and control what happens in case of duplicates 我可以在不使用序列化的情况下编写此代码吗? - Can I write this code without using serialization?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM