2017-08-10 7 views
0

DAOデザインパターンを使用してmysql dbからデータを取得しようとしています。私は正常に "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の問題を解決しません –

関連する問題