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

时间:2017-08-10 07:59:52

标签: java mysql dao

我正在尝试使用DAO设计模式从mysql db获取数据。我可以成功使用" 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;
}
}

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在main方法中返回null。

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

输出:

RahulParyani 执行语句 空

2 个答案:

答案 0 :(得分:1)

如果你观察你的代码

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

您将看到您正在调用方法getCustomerById,但未将其返回值设置为CustomersBean cb

使用此代码

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

答案 1 :(得分:0)

您可以尝试下一种方法:

preparedStatement = dbConnection.prepareStatement("select * from customer where id=?");
preparedStatement.setInt(1, id);

// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();