www.vorhilfe.de
- Förderverein -
Der Förderverein.

Gemeinnütziger Verein zur Finanzierung des Projekts Vorhilfe.de.
Hallo Gast!einloggen | registrieren ]
Startseite · Mitglieder · Impressum
Forenbaum
^ Forenbaum
Status VH e.V.
  Status Vereinsforum

Gezeigt werden alle Foren bis zur Tiefe 2

Navigation
 Startseite...
 Suchen
 Impressum
Das Projekt
Server und Internetanbindung werden durch Spenden finanziert.
Organisiert wird das Projekt von unserem Koordinatorenteam.
Hunderte Mitglieder helfen ehrenamtlich in unseren moderierten Foren.
Anbieter der Seite ist der gemeinnützige Verein "Vorhilfe.de e.V.".
Partnerseiten
Weitere Fächer:

Open Source FunktionenplotterFunkyPlot: Kostenloser und quelloffener Funktionenplotter für Linux und andere Betriebssysteme
Forum "Java" - Java und Oracle-DB
Java und Oracle-DB < Java < Programmiersprachen < Praxis < Informatik < Vorhilfe
Ansicht: [ geschachtelt ] | ^ Forum "Java"  | ^^ Alle Foren  | ^ Forenbaum  | Materialien

Java und Oracle-DB: Verbindung zur Oracle-DB
Status: (Frage) beantwortet Status 
Datum: 11:32 Mi 24.09.2008
Autor: airliner

Aufgabe
Ich soll ein Programm erstellen, welches zur Oracle-DB Verbindung herstellt und Daten auslesen bzw manipulieren kann.
Dabei habe ich derzeit eine GUI erstellt mit einem jTextField, einer jTextArea, einer jComboBox und zwei jButton.
Nun sollen die in das TextField eingegeben SQL-Befehle an die Oracle-DB gesendet werden und die Ergebnisse bzw Fehlermeldungen in der TextArea ausgegeben werden.
Wie realisier ich das auf knopfdurck?

Anbei ist der gesamte Quellcode, der bis jetzt geschrieben wurde

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;

import oracle.jdbc.driver.Const;

/**
  *
  * @Beschreibung: Kleine einfache GUI zum Verbinden mit der Pumacy Oracle-Test-DB,
  * Daten auslesen und verändern mittels SQL-Befehle
  *
  * @version 1.1 vom 23.09.2008
  * @author: Florian Adler
  */

public class Datenbank_auslesen extends JFrame {
  // Anfang Attribute
  private static String[] jComboBox1Data = {};
  private static JComboBox jComboBox1 = new JComboBox(jComboBox1Data);
  private static JTextField jTextField1 = new JTextField();
  private JButton jButton1 = new JButton();
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private static JTextArea jTextArea1 = new JTextArea("");
  private static JComboBox jComboBox2 = new JComboBox();
  private JLabel jLabel3 = new JLabel();
  private JButton jButton2 = new JButton();

   // Ende Attribute

  public Datenbank_auslesen(String title) {
    // Frame-Initialisierung
    super(title);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    int frameWidth = 730;
    int frameHeight = 575;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    Container cp = getContentPane();
    cp.setLayout(null);
    // Anfang Komponenten
    Font f= new Font("ComicSans", Font.BOLD, 12);
  

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

    jTextField1.setBounds(288, 48, 393, 80);
    jTextField1.setText(""+jComboBox1.getSelectedItem());
    jTextField1.repaint();
    cp.add(jTextField1);


    jButton1.setBounds(288, 152, 150, 41);
    jButton1.setText("Ausführen");
    cp.add(jButton1);


    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);

    

    jTextArea1.setText("");
    jTextArea1.setBounds(64, 220, 593, 225);
    jTextArea1.setEditable(false);
    cp.add(jTextArea1);


    jButton2.setBounds(64, 152, 139, 41);
    jButton2.setText("Trennen");
    
    jButton2.addActionListener(al2);
    cp.add(jButton2);
    
    // Ende Komponenten

    setResizable(false);
    setVisible(true);

  }

  public static void main(String[] args) {
    new Datenbank_auslesen("Oracle-Test-Datenbank-Zugriff");
    Connection con = null;
        Statement stmt = null;
        ResultSet result = null;
        String db_Url = "jdbc:oracle:thin:@kmmaster-ts:1521:XE";
        String db_UserName = "xxxx";
        String db_UserPw = "zzzz";

   try {
          Class.forName("oracle.jdbc.driver.OracleDriver");
          }catch (Exception e) {
                jTextArea1.setText("Treiber konnte nicht gefunden [mm] werden\n [/mm] "+e);
                }

     try{
         con = DriverManager.getConnection(db_Url, db_UserName, db_UserPw);
      }catch(Exception e) {
       e.printStackTrace();
      }
      finally{

          if(null != result) {
              try{result.close();} catch(Exception ex) {
              jTextArea1.setText(""+result);}
           }

           if(null != stmt) {
              try{stmt.close();} catch(Exception ex) {}
           }

          
        }
        }
  static ActionListener al = new ActionListener() {
      public void actionPerformed( ActionEvent e )
      {
          jTextField1.setText(""+jComboBox1.getSelectedItem());
      }
  };
  
  static ActionListener al2 = new ActionListener() {
      public void actionPerformed( ActionEvent e )
      {
    
     try{
     con.close();
     }catch(Exception e1){
     jTextArea1.setText("Verbindung bereits beendet");
     }
     }
   };
  
}

        
Bezug
Java und Oracle-DB: Antwort
Status: (Antwort) fertig Status 
Datum: 21:28 Mi 24.09.2008
Autor: piet.t

Hallo,

ich gehe mal davon aus, dass du den ganzen GUI-Kram im Griff hast und auch an das eingegebene SQL-Statement kommst - nennen wir es mal sqlString. Eine Datenbankverbindung con hast Du ja schon erzeugt (via DriverManager.getConnection).Bleibt also noch die frage, wie man den Befehl an die Datenbank schickt und das Ergebnis entsprechend aufbereitet.
Dazu schaust Du Dir am besten mal die Interfaces im Paket java.sql an, denn mit denen muss man hier ausgiebig arbeiten.

Der erste Schritt ist einmal, sich von der Connection ein Statement geben zu lassen:
Statement st = con.createStatement();

Den nächsten Schritt könnte man etwas einfacher gestalten, wenn man schon weiss, welche Art von Statement denn eingegeben wurde, da man dann auf dem Statement executeQuery(sqlString) oder executeUpdate(sqlString) aufrufen und würde direkt das Ergebnis zurückbekommen.
Wenn man das nicht weiss, muss man leider execute() verwenden:
boolean hasResultSet = st.execute(sqlString);

Ist hasResultSet == false, dann haben wir ein update ausgeführt und nur die Anzahl der geänderten Zeilen zurückbekommen, die man mittels st.getUpdateCount() abrufen kann.
Andernfalls gibt es ein ResultSet, dass man mit
ResultSet res = st.getResultSet();
abrufen kann.

Nun muss nur noch das Ergebnis aufbereitet werden. Dazu holst Du dir am besten via
ResultSetMetaData rsm = res.getMetaData();
die Metadaten, aus denen Du dann die Spaltennamen (und typen) ermitteln kannst.
Anschließend musst Du über das ResultSet iterieren und die einzelnen Ergebniszeilen ausgeben:
1: while(res.next()) {
2:    for(int i=0;i<rsm.getColumnCount();i++) {
3:       String value = res.getString(i);
4:       //und dann value ausgeben...
5:    }
6: }


Das wars wohl mal im groben. Ein paar Details musst Du natürlich noch ausarbeiten, denn wir wollen ja nicht alles vorkauen.

Gruß

piet

P.S.: ...und alles was man nicht mehr brauch muss man natürlich auch wieder schkließen: das ResultSet, das Statement und ganz zum SChluss die Connection...

Bezug
Ansicht: [ geschachtelt ] | ^ Forum "Java"  | ^^ Alle Foren  | ^ Forenbaum  | Materialien


^ Seitenanfang ^
ev.vorhilfe.de
[ Startseite | Mitglieder | Impressum ]