【问题标题】:Get data from database and return it in form of pojo object从数据库中获取数据并以 pojo 对象的形式返回
【发布时间】:2017-02-25 10:40:04
【问题描述】:

我有一个返回类型是客户的方法,它是一个 pojo。当我从数据库中获得所需的customerId 时,我想返回带有customerId 对应数据的客户对象。这意味着它有客户姓名、地址等。如何处理?

public class Customer verifyCustomerId(cutomerId){
    statement = connection.createStatement();

    resultSet = statement.executeQuery("select customerid from customer");

    while (resultSet.next()) {
        if (cutomerId == resultSet.getInt(1)) {

            // return data corresponding to this id in the form of object of pojo customer

        }
    }

    return null;
}

【问题讨论】:

  • 这个逻辑开头有customerId还是需要先找到?

标签: java database jdbc pojo


【解决方案1】:

您可以创建一个Customer 对象并在其中设置您的属性,如下所示:

Customer custemer;

if (resultSet.next()) {
   customer = new Customer(resultSet.getInt("customerid"));
}

return custemer;

如果你想得到一个结果,你不需要使用while(..),你可以创建一个if,你的query应该有一个条件"select customerid from customer where ...",因为你的查询可以得到多个结果,如果你想要得到一个列表,你可以像这样使用while

List<Customer> listCustemer = new ArrayList<>();

while (resultSet.next()) {
   listCustemer.add(new Customer(resultSet.getInt("customerid")));
}

return listCustemer;

编辑

您可以更改您的构造函数并设置您想要的字段,例如姓名、地址和...,如下所示:Customer(int id, String name, String address, ...)

所以你可以像这样使用这个构造函数来创建一个新的对象:

listCustemer.add(new Customer(resultSet.getInt("customerid"), 
                 resultSet.getString("name"), resultSet.getString("address"), ...));

【讨论】:

  • 是的,你是对的,但我在客户实体中没有很多属性,所以还有其他方法或者我应该这样做吗?
  • @payalgala 您可以创建一个构造函数,它采用多个值,例如new Customer(par, par2, par2, par2, par2..)
  • 我认为这不能回答问题。没有提到获取其他客户字段,例如姓名和地址。
【解决方案2】:

您的 SQL 语句需要选择要从数据库中取回的数据。您当前的语句将返回客户表中所有行的 customerId。

将您的陈述更改为:

PreparedStatement ps = con.prepareStatement("select name from customer where customerId = ?");
ps.setInt(1, cutomerId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    // use ResultSet to populate Customer pojo
}

我在这里从客户表中选择了名称,但假设存在这样的列。修改它以选择您想要的列。

这是一个关于 PreparedStatements 的教程: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

这是使用它们的原因: How does a PreparedStatement avoid or prevent SQL injection?

【讨论】:

    【解决方案3】:

    我没有看到任何阻止您获取这些详细信息的东西,您需要做的是编辑您尝试从数据库中获取的 QUERY。

    public class Customer verifyCustomerId(cutomerId){
    statement = connection.createStatement();
    CustomerPoJo customer;
    resultSet = statement.executeQuery("select * from customer");
    
    while (resultSet.next()) {
        if (cutomerId == resultSet.getInt(1)) {
    
    // Fill in the details accordingly
            String customername = resultSet.getString(2);
            String customeraddress = resultSet.getString(3);
            ...
    
            customer = new CustomerPoJo();
            customer.setCustomerId(customerId);
            customer.setCustomerName(customername);
            customer.setCustomerAddress(customeraddress);
            ...
    
        }
    }
    // Instead send your customer object
    return customer;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 2010-10-09
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多