java dao:Getting 来自数据库 (mysql) 的特定 id 数据
java dao:Getting data from database (mysql) by specific id
我正在尝试使用 DAO 设计模式从 mysql 数据库获取数据。我可以成功使用 "getAll" 方法但不能使用 "getById" 方法。它 returns 在 class with main 方法中为 null 但是数据存在于 DaoImpl class.
CustomersBean.java
public class CustomersBean {
private int id;
private String firstName;
private String lastName;
private String email;
private String password;
private String phoneNumber;
private String address;
private String address2;
private String city;
private String state;
private String pincode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}
CustomerDao.java(接口)
public interface CustomerDao {
public List<CustomersBean> getAllCustomers();
public CustomersBean getCustomerById(int id);
public void addCustomer(CustomersBean cb);
public void updateCustomer(CustomersBean cb);
public void deleteCutomer(CustomersBean cb);
}
CustomerDaoImpl.java
getAllCustomers() 工作正常但 getCustomerById returns 在主要方法中为空。
public class CustomerDaoImpl implements CustomerDao {
Connection con = ConnectionProvider.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
@Override
public List<CustomersBean> getAllCustomers() {
List<CustomersBean> customer = new ArrayList<>();
//con = ConnectionProvider.getConnection();
try {
ps = con.prepareStatement("select * from customer");
rs = ps.executeQuery();
while (rs.next()) {
CustomersBean cb = new CustomersBean();
cb.setId(rs.getInt(1));
cb.setFirstName(rs.getString(2));
cb.setLastName(rs.getString(3));
cb.setEmail(rs.getString(4));
cb.setPassword(rs.getString(5));
cb.setAddress(rs.getString(6));
cb.setAddress2(rs.getString(7));
cb.setCity(rs.getString(8));
cb.setState(rs.getString(9));
cb.setPincode(rs.getString(10));
customer.add(cb);
}
} catch (SQLException e) {
e.printStackTrace();
}
return customer;
}
@Override
public CustomersBean getCustomerById(int id) {
CustomersBean cb = new CustomersBean();
cb.setId(id);
try {
ps = con.prepareStatement("select * from customer where id="+id);
//ps.setInt(1, id);
rs = ps.executeQuery();
System.out.println("Execute statement");
rs.next();
cb.setLastName(rs.getString(3));
return cb;
} catch (SQLException ex) {
Logger.getLogger(CustomerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
con.close();
ps.close();
rs.close();
} catch (SQLException ex) {
Logger.getLogger(CustomerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
return cb;
}
主要方法:
public class GetAllCustomers {
public static void main(String[] args) {
CustomerDao c=new CustomerDaoImpl();
for(CustomersBean cb:c.getAllCustomers()){
System.out.println(cb.getFirstName()+cb.getLastName());
}
c.getCustomerById(1);
CustomersBean cb=new CustomersBean();
System.out.println(cb.getLastName());
}
}
输出:
拉胡尔帕里亚尼
执行语句
空
您可以尝试下一种方法:
preparedStatement = dbConnection.prepareStatement("select * from customer where id=?");
preparedStatement.setInt(1, id);
// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();
如果您观察您的代码
c.getCustomerById(1);
CustomersBean cb=new CustomersBean();
System.out.println(cb.getLastName());
您将看到您正在调用方法 getCustomerById
但没有将其 return 值设置为 CustomersBean cb
改用此代码
CustomersBean cb = c.getCustomerById(1);
System.out.println(cb.getLastName());
我正在尝试使用 DAO 设计模式从 mysql 数据库获取数据。我可以成功使用 "getAll" 方法但不能使用 "getById" 方法。它 returns 在 class with main 方法中为 null 但是数据存在于 DaoImpl class.
CustomersBean.java
public class CustomersBean {
private int id;
private String firstName;
private String lastName;
private String email;
private String password;
private String phoneNumber;
private String address;
private String address2;
private String city;
private String state;
private String pincode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}
CustomerDao.java(接口)
public interface CustomerDao {
public List<CustomersBean> getAllCustomers();
public CustomersBean getCustomerById(int id);
public void addCustomer(CustomersBean cb);
public void updateCustomer(CustomersBean cb);
public void deleteCutomer(CustomersBean cb);
}
CustomerDaoImpl.java
getAllCustomers() 工作正常但 getCustomerById returns 在主要方法中为空。
public class CustomerDaoImpl implements CustomerDao {
Connection con = ConnectionProvider.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
@Override
public List<CustomersBean> getAllCustomers() {
List<CustomersBean> customer = new ArrayList<>();
//con = ConnectionProvider.getConnection();
try {
ps = con.prepareStatement("select * from customer");
rs = ps.executeQuery();
while (rs.next()) {
CustomersBean cb = new CustomersBean();
cb.setId(rs.getInt(1));
cb.setFirstName(rs.getString(2));
cb.setLastName(rs.getString(3));
cb.setEmail(rs.getString(4));
cb.setPassword(rs.getString(5));
cb.setAddress(rs.getString(6));
cb.setAddress2(rs.getString(7));
cb.setCity(rs.getString(8));
cb.setState(rs.getString(9));
cb.setPincode(rs.getString(10));
customer.add(cb);
}
} catch (SQLException e) {
e.printStackTrace();
}
return customer;
}
@Override
public CustomersBean getCustomerById(int id) {
CustomersBean cb = new CustomersBean();
cb.setId(id);
try {
ps = con.prepareStatement("select * from customer where id="+id);
//ps.setInt(1, id);
rs = ps.executeQuery();
System.out.println("Execute statement");
rs.next();
cb.setLastName(rs.getString(3));
return cb;
} catch (SQLException ex) {
Logger.getLogger(CustomerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
con.close();
ps.close();
rs.close();
} catch (SQLException ex) {
Logger.getLogger(CustomerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
return cb;
}
主要方法:
public class GetAllCustomers {
public static void main(String[] args) {
CustomerDao c=new CustomerDaoImpl();
for(CustomersBean cb:c.getAllCustomers()){
System.out.println(cb.getFirstName()+cb.getLastName());
}
c.getCustomerById(1);
CustomersBean cb=new CustomersBean();
System.out.println(cb.getLastName());
}
}
输出:
拉胡尔帕里亚尼 执行语句 空
您可以尝试下一种方法:
preparedStatement = dbConnection.prepareStatement("select * from customer where id=?");
preparedStatement.setInt(1, id);
// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();
如果您观察您的代码
c.getCustomerById(1);
CustomersBean cb=new CustomersBean();
System.out.println(cb.getLastName());
您将看到您正在调用方法 getCustomerById
但没有将其 return 值设置为 CustomersBean cb
改用此代码
CustomersBean cb = c.getCustomerById(1);
System.out.println(cb.getLastName());