java dao:通过特定id从数据库(mysql)获取数据

java dao:通过特定id从数据库(mysql)获取数据,java,mysql,dao,Java,Mysql,Dao,我试图使用DAO设计模式从mysql数据库获取数据。我可以成功地使用“getAll”方法,但不能使用“getById”方法。它在带有main方法的类中返回null,但是数据存在于DaoImpl类中 CustomersBean.java public class CustomersBean { private int id; private String firstName; private String lastName; private String email; private Strin

我试图使用DAO设计模式从mysql数据库获取数据。我可以成功地使用“getAll”方法,但不能使用“getById”方法。它在带有main方法的类中返回null,但是数据存在于DaoImpl类中

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;
}
}
java(接口)

输出:

拉胡尔帕里亚尼 执行语句

您可以尝试下一种方法:

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
,但没有将其返回值设置为
CustomerBean cb

请改用此代码

CustomersBean cb = c.getCustomerById(1);
System.out.println(cb.getLastName());

可能
id
不等于
1
-为什么不在
中添加打印
id
,以执行
错误查询。试试这个:“ps=con.prepareStatement(“select*fromcustomer,其中id=”“+id+””);”@BrijeshJain
id
是一个数字field@ScaryWombat不过,由于它是一个参数,因此必须以相同的格式传递。这是一种更好的方法,但不能解决OP的问题
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());
CustomersBean cb = c.getCustomerById(1);
System.out.println(cb.getLastName());