2017-08-10 351 views
0

我想从使用DAO设计模式的mysql数据库获取数据。我可以成功使用“getAll”方法,但不能使用“getById”方法。它使用main方法在类中返回null,但数据存在于DaoImpl类中。java dao:通过特定的ID从数据库(mysql)获取数据

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在主方法返回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 执行语句 空

+0

以及也许' id'不等于'1' - 为什么不把'id'打印到'for'循环中 –

+0

错误的查询。试试这个:'ps = con.prepareStatement(“select * from customer where id ='”+ id +“'”);' –

+0

@BrijeshJain'id'是一个数字字段 –

回答

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()); 
+0

非常感谢! @ScaryWombat ...这工作 –

0

您可以尝试下一个方法:

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

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

这是一个更好的方法,但不能解决OP的问题 –

相关问题