Hazırlanan Deyimi kullanarak bir kayıt seçmeye çalıştığımda, her zaman son eklediğim son eklenen değerler veriyor.Neden select deyimi her zaman son eklenen değerleri döndürür?
İlk yaptığım ilk tablodaki bir kaydı aramak. Kayıt varsa, yabancı anahtar tablosu değerleri doldurur. Birincil ve Yabancı Anahtar tablolarım iyi çalışıyor. İlgili bileşenlerine uygun şekilde doldurulan değerler ancak bana doğru değerleri vermiyor. Herhangi bir yardım?
Bu 2 tablodur Yabancı anahtar tabloya başvuran Birincil Anahtar tablodur.
Seç Sorgu:
String searchSECTIONNAME = "SELECT * FROM allsections_list WHERE SECTION_NAME = ?";//1st Select Statement
String searchSECTIONSETTINGS = "SELECT allsections_list.`SECTION_ID`, allsections_settings.ADVISER_ASSIGNED, allsections_settings.SECTION_POPULIMIT,\n" +
"allsections_settings.ROOM_ASSGN, allsections_settings.YRLEVEL_ASSGN, allsections_settings.SCHOOL_YEAR, allsections_settings.SESSION_ASSIGNED\n" +
"FROM allsections_list\n" +
"RIGHT JOIN allsections_settings\n" +
"ON allsections_list.`SECTION_ID`=allsections_settings.`SECTION_ID`";//2nd Select Statement
Yani ne burada yaptın Sağ Üyelik kullanarak yabancı anahtar tablosuna SECTION_NAME
sütun katılmak olduğunu. Kayıt varsa, iki tabloya katılır.
Kodu:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String searchSection = Section_SearchSection_Textfield.getText();
try (Connection myConn = DBUtil.connect();
PreparedStatement myFirstPs = myConn.prepareStatement(searchSECTIONNAME);)
{
myFirstPs.setString(1, searchSection);
try (ResultSet myFirstRs = myFirstPs.executeQuery())
{
int resultCounter = 0;
while (myFirstRs.next())
{
String mySectionName = myFirstRs.getString(2);//Get the value of SECTION_NAME
Section_SectionName_TextField.setText(mySectionName);
Section_SectionName_TextField.setEnabled(true);
try (PreparedStatement mySecondPs = myConn.prepareStatement(searchSECTIONSETTINGS))
{
try (ResultSet mySecondRs = mySecondPs.executeQuery())
{
while (mySecondRs.next())
{
String myAdviserAssigned = mySecondRs.getString(2);
Section_Student_Limit_ComboBox1.setSelectedItem(myAdviserAssigned);
Section_Student_Limit_ComboBox1.setEnabled(true);
String mySectionPopulation = mySecondRs.getString(3);
Section_Student_Limit_ComboBox.setSelectedItem(mySectionPopulation);
Section_Student_Limit_ComboBox.setEnabled(true);
String myRoomAssigned = mySecondRs.getString(4);
Section_Room_Assignment_ComboBox.setSelectedItem(myRoomAssigned);
Section_Room_Assignment_ComboBox.setEnabled(true);
String myYearLevelAssigned = mySecondRs.getString(5);
Section_Session_Level_ComboBox.setSelectedItem(myYearLevelAssigned);
Section_Session_Level_ComboBox.setEnabled(true);
String mySchoolYear = mySecondRs.getString(6);
Section_SchooYear_ComboBox.setSelectedItem(mySchoolYear);
Section_SchooYear_ComboBox.setEnabled(true);
String mySessionAssigned = mySecondRs.getString(7);
Section_Session_Settings_ComboBox.setSelectedItem(mySessionAssigned);
Section_Session_Settings_ComboBox.setEnabled(true);
resultCounter++;
}//end of loop mySecondRs (ResultSet)
}//end of try mySecondRs (ResultSet)
}//end of try mySecondPs (PreparedStatement)
}//end of loop myFirstRs (ResultSet)
if (resultCounter == 1)//If exist
{
JOptionPane.showMessageDialog(null, "Data Found");
}
else//If not exist
JOptionPane.showMessageDialog(null, "No Data Found");
}//end of try myFirstRs (ResultSet)
}//end of try myFirstPs (PreparedStatement)
catch (SQLException e)
{
DBUtil.processException(e);
}//end of catch
}
Burada görebileceğiniz gibi. İlk ResultSetmyFirstRs
'umda mevcut bir SECTION_NAME
numaralı telefonu ararken yabancı anahtar değerleri girecektir. Döngünümdeki bir şey beni düzeltirse. Şimdiden teşekkürler!
Güncelleştirme!
2. Select Query'de bir ORDER BY cümlesi ekliyorum.
String searchSECTIONSETTINGS = "SELECT allsections_list.`SECTION_ID`, allsections_settings.ADVISER_ASSIGNED, allsections_settings.SECTION_POPULIMIT,\n" +
"allsections_settings.ROOM_ASSGN, allsections_settings.YRLEVEL_ASSGN, allsections_settings.SCHOOL_YEAR, allsections_settings.SESSION_ASSIGNED\n" +
"FROM allsections_list\n" +
"RIGHT JOIN allsections_settings\n" +
"ON allsections_list.`SECTION_ID` = allsections_settings.`SECTION_ID`" +
"ORDER BY allsections_list.SECTION_ID";
Projeyi çalıştırdığınızda Hala beni yanlış değerler veren: Çünkü bu olmadan veritabanı ben yaptın yani ne, ne istediğini dönecektir sorguyu değiştirebilir ve bunun gibi ORDER BY yan tümcesi eklemektir. Bunu NetBeans sorgusunda çalıştırmaya ve ASC düzeninde bana değerler vermeye çalıştım.
Neden "resultCounter == 1" yerine "> 0" olup olmadığını kontrol ediyor musunuz? – callyalater
@callyalater "resultCounter> 0" kullanıldığında hala bana son değerler veriyor. Cevap verdiğin için teşekkürler. – Francisunoxx