Öncelikle veritabanı kodlarımızla başlayalım;
SELECT product.productId, product.productName
,category.categoryName,category.categoryId,product.price
FROM
product
LEFT JOIN category on
product.categoryId=category.categoryId
WHERE
category.categoryId="değer"
Yazmış olduğum SQL Cümlesinde Product ve Category adlı tabloları LEFT JOIN aracılığı ile birbirine bağladık. Ardından ise JList'ten gelecek verinin değerini karşılaması için WHERE operatörünü kullandık... Test ettik MYSQL'de çalışıyor.
Ardından ise bunu Java kısmına uyarlıyoruz Aşağıda ki gibi;
public static List<ProductDomain> categoryList(int a) {
List<ProductDomain>
selectItem = new ArrayList<ProductDomain>();
Connection
conn = ConnectionDao.getConnnection();
try {
Statement query = conn.createStatement();
ResultSet rs = query.executeQuery("SELECT
product.productId, product.productName,category.categoryName,category.categoryId,product.price "
+
"FROM product "
+ "LEFT JOIN category on
product.categoryId=category.categoryId "
+
"WHERE category.categoryId=" + a + "");
while (rs.next()) {
ProductDomain sec = new ProductDomain();
sec.setCategoryId(rs.getInt("category.categoryId"));
sec.setProductId(rs.getInt("product.productId"));
sec.setProductName(rs.getString("product.productName"));
sec.setCategoryName(rs.getString("category.categoryName"));
sec.setPrice(rs.getDouble("price"));
selectItem.add(sec);
}
Ardından ise arayüz kısmına geçip JList'te yapılan hareketle bir faliyete geçmesi için JList'te addListSelectionListener metodunu uygulayacağım...
Aşağıda ki gibi;
productCategoryList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
});
Ardından oluşturduğum Listener'ın içine JList'i cast ediyorum;
CategoryDomain categoryDm = (CategoryDomain) productCategoryList.getSelectedValue();
Ardından ise; cast ederek aldığım değeri bir Integer değişkene atıyorum.
int a = categoryDm.getCategoryId();
Ardından
for ile SQL kısmında oluşturduğum methodu yani yukarıda ki metodu aşağıda ki gibi kullanarak yazıyorum;
for (ProductDomain dm : ProductDao.categoryList(a)) {
Object variable[] = { dm.getProductId(), dm.getProductName(), dm.getCategoryName(), dm.getPrice() };
model.addRow(variable);
model.addRow(variable);
}
productCategoryList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
int rowCount = model.getRowCount();
//JTable'ın içinde ki geçmiş model verilerini temizler yenilerini tekrar for ile yerleştirir
for (int i = rowCount - 1; i >= 0; i--) {
model.removeRow(i);
}
CategoryDomain categoryDm = (CategoryDomain) productCategoryList.getSelectedValue();
int a = categoryDm.getCategoryId();
for (ProductDomain dm : ProductDao.categoryList(a)) {
Object variable[] = { dm.getProductId(), dm.getProductName(), dm.getCategoryName(), dm.getPrice() };
model.addRow(variable);
}
}
});

Hiç yorum yok:
Yorum Gönder