从数据库中获取数据并 return 以 pojo 对象的形式
Get data from database and return it in form of pojo object
我有一个方法,其 return 类型是 customer,它是一个 pojo。当我从数据库中获得需要 customerId
时,我想 return 具有 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;
}
您的 SQL 语句需要 select 您想要从数据库中取回的数据。您当前的语句将 return customer table.
中所有行的 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
}
我这里有 select 客户 table 的姓名,但前提是存在这样的一列。将其修改为 select 你想要的列。
这是关于 PreparedStatements 的教程:
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
这是使用它们的原因:
How does a PreparedStatement avoid or prevent SQL injection?
您可以创建一个 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"), ...));
我没有看到任何阻止您获取这些详细信息的因素,您需要做的是编辑您试图从数据库中获取的 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;
}
我有一个方法,其 return 类型是 customer,它是一个 pojo。当我从数据库中获得需要 customerId
时,我想 return 具有 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;
}
您的 SQL 语句需要 select 您想要从数据库中取回的数据。您当前的语句将 return customer table.
中所有行的 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
}
我这里有 select 客户 table 的姓名,但前提是存在这样的一列。将其修改为 select 你想要的列。
这是关于 PreparedStatements 的教程: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
这是使用它们的原因: How does a PreparedStatement avoid or prevent SQL injection?
您可以创建一个 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"), ...));
我没有看到任何阻止您获取这些详细信息的因素,您需要做的是编辑您试图从数据库中获取的 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;
}