DB-Ausgabe in jTextArea

airliner

Erfahrenes Mitglied
Jaja, schlagt mich, aber ich bin's wieder, aber dies wird das letzte Mal sein (zumindest für dieses Projekt ;) )

Dieses Mal scheint es ein recht banales "Problem" zu sein, aber ich komme halt nich drauf.
Und :google: hilft mir nich wirklich weiter, was das angeht.

Ihr kennt ja mein DB-Auslese-Programm.
Nun hätte ich gern die Ausgabe in meine jTextArea.
Brauche ich dazu Streams oder etwas in der Richtung?
Die Fehlerausgabe funktioniert ja soweit ganz gut (Hab die alle mal durchgespielt), nur die DB-Ausgabe nicht...

Vielen Dank nochmal!
 
Anbindung an die DB:

PHP:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.driver.Const;

	public class Database {
    private static Database instance    = null;
    public  Connection      con         = null;
    private String          db_Url      = "jdbc:oracle:thin:@kmmaster-ts:1521:XE";
    private String          db_UserName = "florian";
    private String          db_UserPw   = "florian";

    private Database() {}
    public static Database getInstance() {
    	if (instance == null) {
            instance = new Database();
           }

           return instance;
           }

    public void connect() throws SQLException, ClassNotFoundException {
             Class.forName("oracle.jdbc.driver.OracleDriver");
             con = DriverManager.getConnection(db_Url, db_UserName, db_UserPw);
          }

    public void close() throws SQLException {
    	if (con != null) {
            con.close();
            con = null;
            }
          }

    public boolean isConnected() {
         if (con != null) {
             return true;
              }
             return false;
           }
       
     public void executeDBCommand(String command) throws SQLException {
     Statement stmt = con.createStatement();
               stmt.execute(command);
          }

      public ResultSet getQuery(String query) throws SQLException {
             Statement stmt = con.createStatement();
             ResultSet rs   = stmt.executeQuery(query);

             return rs;
          }

       public void executeTableCommand(String command) throws SQLException {
              Statement stmt = con.createStatement();
              stmt.executeUpdate(command);

          }

      }

PHP:
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;

public class Mainframe extends JFrame {
       private static String[]   data        = {};
       private static JComboBox  cb_commands = new JComboBox(data);
       private static JTextField tx_sql      = new JTextField();
       private static JTextArea  ta_output   = new JTextArea("");
       private JButton           b_execute   = new JButton();
       private JLabel            jLabel1     = new JLabel();
       private JLabel            jLabel2     = new JLabel();
       private Font              f           = new Font("ComicSans", Font.BOLD, 12);
       private Database          db          = Database.getInstance();
       private JButton           b_close     = new JButton();

       public Mainframe(String title) {
       super(title);
       initialize();

          try {
               db.connect();
              } catch (SQLException ex) {
                  ta_output.setText(ex.toString());
              } catch (ClassNotFoundException ex) {
                  ta_output.setText(ex.toString());
              }
          }

       private void initialize() {
              Container cp = getContentPane();
              cp.setLayout(null);
              cb_commands.setFont(f);
              cb_commands.setBounds(24, 110, 210, 24);
              cb_commands.setBackground(Color.WHITE);
              cb_commands.setForeground(Color.BLACK);
              cb_commands.addItem("..........");
              cb_commands.addItem(" 0-9 ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" A ... ");
              cb_commands.addItem("Action");
              cb_commands.addItem("Action2Systemrole");
              cb_commands.addItem("AuthenticationInstrument");
              cb_commands.addItem("AuthenticationMethod");
              cb_commands.addItem("AuthenticationMethod2Channel");
              cb_commands.addItem("Author2Document");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" B ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" C ...");
              cb_commands.addItem("Category");
              cb_commands.addItem("Category Keyword");
              cb_commands.addItem("Change Request");
              cb_commands.addItem("Channel");
              cb_commands.addItem("Commentaries");
              cb_commands.addItem("Community");
              cb_commands.addItem("CommunityTemplates");
              cb_commands.addItem("CommunityUser");
              cb_commands.addItem("CommunityUsergroup");
              cb_commands.addItem("ComminutyWorkflow");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" D ...");
              cb_commands.addItem("Document");
              cb_commands.addItem("DocumentArchive");
              cb_commands.addItem("DocumentField");
              cb_commands.addItem("DocumentFieldArchive");
              cb_commands.addItem("DocumentHistory");
              cb_commands.addItem("DocumentParticipants");
              cb_commands.addItem("DocumentRating");
              cb_commands.addItem("DocumentSignature");
              cb_commands.addItem("DocumentTypeCategory");
              cb_commands.addItem("DocumentTypeSequence");
              cb_commands.addItem("Document2Category");
              cb_commands.addItem("Document2Document");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" E ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" F ...");
              cb_commands.addItem("Favorites");
              cb_commands.addItem("FQSFDocument");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" G ...");
              cb_commands.addItem("Group2WFRole");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" H ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" I ...");
              cb_commands.addItem("IntendedReleasePeriodDate");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" J ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" K ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" L ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" M ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" N ...");
              cb_commands.addItem("News");
              cb_commands.addItem("Notification");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" O ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" P ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" Q ...");
              cb_commands.addItem("QSFDocument");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" R ...");
              cb_commands.addItem("RatingItem");
              cb_commands.addItem("RatingType");
              cb_commands.addItem("ReviewReleaseDate");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" S ...");
              cb_commands.addItem("Systemrole");
              cb_commands.addItem("Systemuser");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" T ...");
              cb_commands.addItem("Template");
              cb_commands.addItem("TemplateCategory");
              cb_commands.addItem("TemplateField");
              cb_commands.addItem("TemplateWorkflow");
              cb_commands.addItem("TimeForInitialTraining");
              cb_commands.addItem("TrainingParicipant");
              cb_commands.addItem("TrainingSession");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" U ...");
              cb_commands.addItem("Usergroup");
              cb_commands.addItem("User2Systemrole");
              cb_commands.addItem("User2Usergroup");
              cb_commands.addItem("User2WFRole");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" V ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" W ...");
              cb_commands.addItem("WFRole");
              cb_commands.addItem("WFRole2WFStatus");
              cb_commands.addItem("WFRole2WFTrans");
              cb_commands.addItem("WFStatus");
              cb_commands.addItem("WFStatusphases");
              cb_commands.addItem("WFStatus Properties");
              cb_commands.addItem("WFTransition");
              cb_commands.addItem("WFTransitionConditions");
              cb_commands.addItem("WFTransitionConstraints");
              cb_commands.addItem("WFTransitionFinalizers");
              cb_commands.addItem("WFTransitionProperties");
              cb_commands.addItem("WFWorkflow");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" X ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" Y ...");
              cb_commands.addItem("--------------------------------------------");
              cb_commands.addItem(" Z ...");
              cb_commands.addItemListener(new ItemListener() {

                  public void itemStateChanged(ItemEvent e) {
                      tx_sql.setText(cb_commands.getSelectedItem().toString());
                  }
              });
              cp.add(cb_commands);
              tx_sql.setBounds(288, 48, 393, 80);
              cp.add(tx_sql);
              b_execute.setBounds(310, 152, 175, 41);
              b_execute.setText("SQL-Befehl ausführen");
              b_execute.addActionListener(new ActionListener() {
 
                

				public void actionPerformed(ActionEvent e) {
 
                      try {
                           db.executeDBCommand(tx_sql.getText());
                           
                           } catch (SQLException ex) {
                           ta_output.setText(ex.toString());
                       }
                   }
               });
 
              cp.add(b_execute);
              jLabel1.setBounds(24, 95, 150, 16);
              jLabel1.setText("Tabelle auswählen");
              jLabel1.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
              cp.add(jLabel1);
 
              jLabel2.setBounds(296, 24, 131, 16);
              jLabel2.setText("SQL-Befehl eingeben:");
              jLabel2.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
              cp.add(jLabel2);
              
              ta_output.setText("");
              ta_output.setBounds(64, 220, 593, 225);
              ta_output.setEditable(false);
              cp.add(ta_output);
              
              b_close.setBounds(64, 152, 175, 41);
              b_close.setText("Verbindung trennen");
              b_close.addActionListener(new ActionListener() {
 
                  public void actionPerformed(ActionEvent e) {
                       try {
                           db.close();
                           ta_output.setText("Verbindung beendet");
                       } catch (SQLException ex) {
                          ta_output.setText(ex.toString());
                      }
                  }
              });
              cp.add(b_close);
          }


          public static void main(String[] args) {

              Mainframe frame = new Mainframe("Oracle-Test-Datenbank-Zugriff");

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(730, 575);
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);

          }

      }

Ich hab mir das Gnaze so vorgestellt:
Der SQL-Befehl im jTextField soll eingelesen werden, an die DB geschickt und dirt verarbeitet werden. Das Ergebnis, das zurückkommt, soll dann in die jTextArea geschrieben werden (alles auf Knopfdruck).

Frage ist nun, was muss ich in ActionPerformed() eintragen, damit das funktioniert.
Der Button hat soweit schon den ActionListener, den letzten kleinen Rest brauche ich nun noch...


Nochmals ein großes DANKESCHÖN an Xandro(!)
 
Wenn du nur die executeDBCommand Methode von Xandro benutzt kannst du nur was Ausführen aber nichts "Auslesen" bentz diese von Xandro:
Java:
ResultSet res = db.getQuery("...");

Dann kannst du anfangen auszugeben
Java:
while(res.next()){
jTextArea.setText(res.getString(1)); 
// oder appered("..."); getString(1) holt den eintrag aus der ersten Spalte.
//Bei nächsten next springt er in die nächste Zeile  und holt wieder den eintrag der ersten Spalte
//next() liefert ein False zurück wenn keine Zeile mehr vorhanden ist
//d.h er springt automatisch aus der ausgabe raus
}
 
Zuletzt bearbeitet von einem Moderator:
Zurück