2017-08-10 60 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的問題 –