Ich versuche einen Datensatz zu finden. Mit dieser Option kann ich einen vorhandenen Datensatz in meiner Datenbank mit Stored Procedure suchen. Wenn ich versuchte, vorhandene Daten zu durchsuchen, gibt es mir nicht den Wert, den ich möchte. Wenn ich die Suchschaltfläche drücke, wird der Wert nicht in das Textfeld gedruckt.Der gewünschte Wert kann nicht mit Stored Procedure abgerufen werden
CODE
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);
}
}
Stored Procedure
CREATE PROCEDURE getSECTION_NAME(IN ID INT, OUT NAME VARCHAR(50))
SELECT * FROM allsections_list WHERE SECTION_ID = ID AND SECTION_NAME = NAME
Tabelle
CREATE TABLE
(
SECTION_ID INT PRIMARY KEY AUTO_INCREMENT,
SECTION_NAME VARCHAR(50) NOT NULL
)
Jede mögliche Hilfe werde geschätzt! Vielen Dank!
Aktualisierung! Nach was ich gelesen habe, kann Stored Procedure eine Ergebnismenge zurückgeben. Ich möchte die Werte des Parameters OUT abrufen.
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);
}
}
Ich entfernte die String sectionName = myRs.getString(2); Section_SectionName_TextField.setText(sectionName); Section_SectionName_TextField.setEnabled(true);
aus dem Set Block Ergebnis und im Callable Statement Block setzen. Wenn ich das Programm starte. Die einzige Änderung ist, dass das Textfeld aktiviert wird und mir einen "Null" -Wert gibt.
2. Update! Ich möchte die Werte von OUT-Parameter zurückgegeben. Ich sollte Result Set nicht verwenden, um es abzurufen. Also habe ich Callable Statement-Parameter mit OUT-Parameter der gespeicherten Prozedur nach @Gord Thompson verwendet.
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);
}
}
Sein geben mir noch ein Null-Werte, bei denen ich nicht, warum ich diesen Wert zu bekommen.
Die einzigen Änderungen an meinem GUI ist das Textfeld aktiviert werden und es wird Druck nicht den Wert I in das vorgesehene Feld wollen. :(
Danke für Ihre Reaktion Fühlen Sie sich frei zu äußern
@SashaSalauyou Hallo! Bitte sehen Sie meinen aktualisierten Beitrag. Danke :) – Francisunoxx
@SashaSalauyou Wenn ich die Abfrage ausführen und mir einen Fehler wirft. 'Das Ende;' zeigt nicht nur den Fehler in Zeile 3 an. Danke. – Francisunoxx
Übrigens, Ihr Java-Codebeispiel ist definitiv nicht vollständig, da es mit einem "else" ohne ein "if" beginnt. Und es endet in der Mitte des Codes. Auf diese Weise ist es schwer dir zu helfen. Bitte geben Sie mindestens die vollständige Methode an, wenn nicht sogar den gesamten Klassenquellcode. –