Kullanarak Seçme alınamıyor Bir kayıt bulmaya çalışıyorum. Hangi benim Stored Procedure kullanarak benim veritabanında mevcut bir kayıt bulmayı seçmeme izin verin. Mevcut bir veriyi aramaya çalıştığımda bana istediğim değeri vermez. Arama düğmesine bastığımda, değeri metin alanına yazdırmıyor.İstenen değeri Kayıtlı Yordam
KOD Prosedürü
CREATE PROCEDURE getSECTION_NAME(IN ID INT, OUT NAME VARCHAR(50))
SELECT * FROM allsections_list WHERE SECTION_ID = ID AND SECTION_NAME = NAME
Tablo
CREATE TABLE
(
SECTION_ID INT PRIMARY KEY AUTO_INCREMENT,
SECTION_NAME VARCHAR(50) NOT NULL
)
Herhangi bir yardım ederim Saklanan
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String searchSection = Section_SearchSection_Textfield.getText();
String searchSection_Name = Section_SectionName_TextField.getText();
int sectionID = 0;
if (searchSection.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please fill up this fields");
}
else
try (Connection myConn = DBUtil.connect())
{
try (CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
{
myFirstCs.setInt(1, sectionID);// I set the ID for Primary Key
myFirstCs.registerOutParameter(2, Types.VARCHAR);
myFirstCs.setString(2, searchSection_Name);
boolean hasresults = myFirstCs.execute();
if (hasresults)
{
try (ResultSet myRs = myFirstCs.getResultSet())
{
int resultsCounter = 0;
while (myRs.next())
{
sectionID = myRs.getInt("SECTION_ID");
String sectionName = myRs.getString(2);
Section_SectionName_TextField.setText(sectionName);//Set the value of text
Section_SectionName_TextField.setEnabled(true);//Set to enable
resultsCounter++;
}//end of while
}//end of if
}//end of resultset
}//end of callablestatement
}//end of connection
catch (SQLException e)
{
DBUtil.processException(e);
}
}
takdir edilmek! Teşekkürler!
Güncelleştirme! Kayıtlı Prosedürü okuduğuma göre bir sonuç kümesi döndürebilir. OUT parametresinin değerlerini almak istiyorum.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String searchSection = Section_SearchSection_Textfield.getText();
String searchSection_Name = Section_SectionName_TextField.getText();
if (searchSection.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please fill up this fields");
}
else
try (Connection myConn = DBUtil.connect();
CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
{
myFirstCs.setInt(1, sectionID);// I set the ID for Primary Key
myFirstCs.registerOutParameter(2, Types.VARCHAR);
boolean hasresults = myFirstCs.execute();
if (hasresults)
{
try (ResultSet myRs = myFirstCs.getResultSet())
{
while (myRs.next())
{
sectionID = myRs.getInt("SECTION_ID");
System.out.print(sectionID);
}//end of while
}//end of resultset
}//end of if
String sectionName = myFirstCs.getString(2);
Section_SectionName_TextField.setText(sectionName);//Set the value of text
Section_SectionName_TextField.setEnabled(true);//Set to enable
System.out.print(sectionName);
}//end of connection
catch (SQLException e)
{
DBUtil.processException(e);
}
}
Ben Sonuç Kümesi bloktan String sectionName = myRs.getString(2); Section_SectionName_TextField.setText(sectionName); Section_SectionName_TextField.setEnabled(true);
çıkarılıp çağrılabilir Bildirimi bloğunda koydu. Programı çalıştırdığımda. Tek değişiklik, metin alanının etkinleştirilmesi ve bana "boş" bir değer yazdırmasıdır. 2 Güncelleme! Ben almak için Sonuç Seti kullanmamam gereken OUT parametresi değerlerini döndürmek istiyorum. Bu yüzden @Gord Thompson'a göre saklanan prosedürün OUT parametresiyle Callable Statement parametresini kullandım.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String searchSection = Section_SearchSection_Textfield.getText();
String searchSection_Name = Section_SectionName_TextField.getText();
if (searchSection.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please fill up this fields");
}
else
try (Connection myConn = DBUtil.connect();
CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
{
myFirstCs.setInt(1, 2);// I set the ID for Primary Key
myFirstCs.registerOutParameter(2, Types.VARCHAR);
myFirstCs.execute();
String sectionName = myFirstCs.getString(2); // retrieve value from OUT parameter
Section_SectionName_TextField.setText(sectionName);//Set the value of text
Section_SectionName_TextField.setEnabled(true);//Set to enable
System.out.println(sectionName);
}//end of connection
catch (SQLException e)
{
DBUtil.processException(e);
}
}
Bana hala bu değeri almamın neden olmadığı boş değerler veriyor.
benim GUI değişiklik yapılan tek bir metin alanıdır etkin hale gelir ve ben şu yazı bölümünde istediğiniz değeri baskı yer etmedi. yanıt için :(
sayesinde yorum yapmaktan çekinmeyin
@SashaSalauyou Hi! Lütfen güncellenmiş gönderiye bakın. Teşekkürler :) – Francisunoxx
@SashaSalauyou Sorguyu çalıştırdığımda ve bana bir hata atar. 'Son;' sadece satır 3 yakınındaki hatayı göstermiyor. Teşekkürler. – Francisunoxx
Bu arada, Java kod örneğiniz kesinlikle tamamlanmadı, çünkü "if" olmadan bir "else" ile başlıyor. Ve kodun ortasında biter. Bu şekilde size yardımcı olmak çok zor. Tüm sınıf kaynak kodu bile olmasa bile en azından eksiksiz yöntemi sağlayın. –