private String kelimeyiCek() {
String resimURL = null;
String cumle;
// Daha önce seçilen kelime ID'sini saklamak için değişken
int oncekiKelimeID = 0;
// Bilinenler tablosundan bir kelime seç
sql = "SELECT ingilizce_kelime, turkce_karsiligi, resim_url, cumle_kullanimi, kelimeID, status, tarih FROM bilinenler WHERE status >= 1 AND kelimeID != ? ORDER BY RAND() LIMIT 1";
try (PreparedStatement statement = baglanti.prepareStatement(sql)) {
statement.setInt(1, oncekiKelimeID); // Önceki kelimeyi hariç tut
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String kelime = resultSet.getString("ingilizce_kelime");
dogruCevap = resultSet.getString("turkce_karsiligi");
resimURL = resultSet.getString("resim_url");
kelimeId = resultSet.getInt("kelimeID");
cumle = resultSet.getString("cumle_kullanimi");
status = resultSet.getInt("status");
upTarih = resultSet.getString("tarih");
txt_cumle1.setText(cumle);
txt_sinavKelimesi.setText(kelime);
txt_statusID.setText(String.valueOf(status));
txt_upTarihId.setText(upTarih);
// Önceki kelime ID'sini güncelle
oncekiKelimeID = kelimeId;
cumleleriGoster(kelimeId);
} else {
// Bilinenler tablosundan kelime seçilemedi, kalan soruları kelimeler tablosundan al
sql = "SELECT ingilizce_kelime, turkce_karsiligi, resim_url, cumle_kullanimi, kelimeID, status, tarih FROM kelimeler ORDER BY RAND() LIMIT 1";
try (PreparedStatement kalanSorularStatement = baglanti.prepareStatement(sql)) {
ResultSet kalanSorularResultSet = kalanSorularStatement.executeQuery();
if (kalanSorularResultSet.next()) {
String kelime = kalanSorularResultSet.getString("ingilizce_kelime");
dogruCevap = kalanSorularResultSet.getString("turkce_karsiligi");
resimURL = kalanSorularResultSet.getString("resim_url");
kelimeId = kalanSorularResultSet.getInt("kelimeID");
cumle = kalanSorularResultSet.getString("cumle_kullanimi");
status = kalanSorularResultSet.getInt("status");
upTarih = kalanSorularResultSet.getString("tarih");
txt_cumle1.setText(cumle);
txt_sinavKelimesi.setText(kelime);
txt_statusID.setText(String.valueOf(status));
txt_upTarihId.setText(upTarih);
cumleleriGoster(oncekiKelimeID);
System.out.println("çalıştı");
} else {
txt_sinavKelimesi.setText("");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return resimURL;
}
What I want to do in these codes is to pull all the data from the "bilinenler" table and then pull the remaining data from the "kelimeler" table, but I can not enter the else block, only the data from the "bilinenler" table appears on the screen. If there is no data in the "bilinenler" table, then the data in the "kelimeler" table is displayed.
I wrote a control method for this problem and tried to use it, but I made my code worse. If you want, you can solve the problem with this control mechanism. I also share this method with you.
private boolean bilinenlerTablosundaSorukalmadiMi() throws SQLException {
String kontrolSorgusu = "SELECT COUNT(*) AS sayi FROM bilinenler WHERE status >= 1";
try (PreparedStatement kontrolStatement = baglanti.prepareStatement(kontrolSorgusu)) {
ResultSet kontrolResultSet = kontrolStatement.executeQuery();
if (kontrolResultSet.next()) {
int soruSayisi = kontrolResultSet.getInt("sayi");
return soruSayisi == 0; // Eğer 0 ise daha fazla soru yok demektir
}
}
return false; // Hata durumu
}
I want to pull random data from the "kelimeler" table after pulling all the data from the "bilinenler" table. I defined a user input that I had previously determined for this program. For example, when the user requests 10 data, if there are 6 data in the "bilinenler" table, the user will present all of them and select the remaining 4 randomly from the "kelimeler" table.
Answers
It seems like the issue lies in the logic of your kelimeyiCek()
method. Let's address the problem step by step.
-
Debugging
kelimeyiCek()
Method:- In your
kelimeyiCek()
method, you first check if there are any records in thebilinenler
table. If there are, you retrieve a random record from there. - If there are no records in the
bilinenler
table, you attempt to retrieve a random record from thekelimeler
table. - However, you mentioned that you cannot enter the else block, which suggests that there might always be records in the
bilinenler
table, even if it's empty.
- In your
-
Potential Issue:
- The issue might be related to the initial value of the
oncekiKelimeID
variable. IfoncekiKelimeID
is always initialized to0
, then it's likely that there are always records in thebilinenler
table becausestatus >= 1
includes all records wherestatus
is greater than or equal to 1.
- The issue might be related to the initial value of the
-
Suggested Fix:
- You need to ensure that
oncekiKelimeID
is set to0
only when there are no records in thebilinenler
table. Otherwise, it should be set to the ID of the last retrieved record from thebilinenler
table. - You can achieve this by moving the initialization of
oncekiKelimeID
outside of thekelimeyiCek()
method and ensuring it retains its value across method calls.
- You need to ensure that
Here's how you can modify your code to fix the issue:
// Initialize oncekiKelimeID outside the method
private int oncekiKelimeID = 0;
private String kelimeyiCek() {
String resimURL = null;
String cumle;
// Bilinenler tablosundan bir kelime seç
sql = "SELECT ingilizce_kelime, turkce_karsiligi, resim_url, cumle_kullanimi, kelimeID, status, tarih FROM bilinenler WHERE status >= 1 AND kelimeID != ? ORDER BY RAND() LIMIT 1";
try (PreparedStatement statement = baglanti.prepareStatement(sql)) {
statement.setInt(1, oncekiKelimeID); // Önceki kelimeyi hariç tut
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
// Retrieve data from bilinenler table
String kelime = resultSet.getString("ingilizce_kelime");
dogruCevap = resultSet.getString("turkce_karsiligi");
resimURL = resultSet.getString("resim_url");
kelimeId = resultSet.getInt("kelimeID");
cumle = resultSet.getString("cumle_kullanimi");
status = resultSet.getInt("status");
upTarih = resultSet.getString("tarih");
txt_cumle1.setText(cumle);
txt_sinavKelimesi.setText(kelime);
txt_statusID.setText(String.valueOf(status));
txt_upTarihId.setText(upTarih);
// Önceki kelime ID'sini güncelle
oncekiKelimeID = kelimeId;
cumleleriGoster(kelimeId);
} else {
// Bilinenler tablosundan kelime seçilemedi, kalan soruları kelimeler tablosundan al
sql = "SELECT ingilizce_kelime, turkce_karsiligi, resim_url, cumle_kullanimi, kelimeID, status, tarih FROM kelimeler ORDER BY RAND() LIMIT 1";
try (PreparedStatement kalanSorularStatement = baglanti.prepareStatement(sql)) {
ResultSet kalanSorularResultSet = kalanSorularStatement.executeQuery();
if (kalanSorularResultSet.next()) {
// Retrieve data from kelimeler table
String kelime = kalanSorularResultSet.getString("ingilizce_kelime");
dogruCevap = kalanSorularResultSet.getString("turkce_karsiligi");
resimURL = kalanSorularResultSet.getString("resim_url");
kelimeId = kalanSorularResultSet.getInt("kelimeID");
cumle = kalanSorularResultSet.getString("cumle_kullanimi");
status = kalanSorularResultSet.getInt("status");
upTarih = kalanSorularResultSet.getString("tarih");
txt_cumle1.setText(cumle);
txt_sinavKelimesi.setText(kelime);
txt_statusID.setText(String.valueOf(status));
txt_upTarihId.setText(upTarih);
cumleleriGoster(oncekiKelimeID);
System.out.println("çalıştı");
} else {
txt_sinavKelimesi.setText("");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return resimURL;
}
By initializing oncekiKelimeID
outside the method and updating its value based on the last retrieved record from the bilinenler
table, you should be able to correctly retrieve data from both tables as intended.