Advertisement
Guest User

Untitled

a guest
May 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.17 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5.  
  6.  
  7. public class AbfrageDB extends Applet implements MouseListener,ActionListener
  8. {
  9.    //Datenbankvariablen
  10.     Connection dbVerbindung=null;
  11.     Statement sqlStatement=null;
  12.     ResultSet resultSet=null;
  13.     ResultSetMetaData rsmd = null;
  14.  
  15.    // graph. Objekte globalisieren
  16.    Image lahnverlauf;
  17.    String sqlString="",strkanukl,strkanugr,strpreis,geskost,anzkl,anzgr,gesanz;
  18.     double dbkanukl,dbkanugr,kostkl,kostgr,dbanzkl,dbanzgr,kostges,dbkanu,anzges;
  19.     Label lbkorrekt,lbort,lbstart,lbziel,lbanzkanus,lbklkanus,lbgrkanus,lbdaten,lbvnme,lbnnme,lbstrnr,lbplzort,lbmail;
  20.     TextField tfpreis, tfklein,tfgross,tfnnme,tfvnme,tfstr,tfnr,tfplz,tfort,tfmail;
  21.     TextArea ausgabe;
  22.     Button preis,buchen,korrekt,abbruch;
  23.     Choice c1,c2;
  24.  
  25.    //Initialisierung
  26.     public void init()
  27.     {
  28.       //Layout
  29.       this.setLayout(null);
  30.  
  31.       //Bilder
  32.       lahnverlauf = getImage (getDocumentBase (), "Lahnverlauf.GIF");
  33.  
  34.       //Labels
  35.       lbort=new Label("Bitte geben sie Start- und Zielort an:");
  36.       lbort.setLocation(350,5);
  37.       lbort.setSize(350,15);
  38.       this.add(lbort);
  39.       lbstart=new Label("Start");
  40.       lbstart.setLocation(350,30);
  41.       lbstart.setSize(50,15);
  42.       this.add(lbstart);
  43.       lbziel=new Label("Ziel");
  44.       lbziel.setLocation(500,30);
  45.       lbziel.setSize(50,15);
  46.       this.add(lbziel);
  47.       lbanzkanus=new Label("Wieviele Kanus werden benötigt?");
  48.       lbanzkanus.setLocation(350,150);
  49.       lbanzkanus.setSize(350,15);
  50.       this.add(lbanzkanus);
  51.       lbklkanus=new Label("Kleine Kanus:");
  52.       lbklkanus.setLocation(350,175);
  53.       lbklkanus.setSize(100,15);
  54.       this.add(lbklkanus);
  55.       lbgrkanus=new Label("Große Kanus:");
  56.       lbgrkanus.setLocation(350,200);
  57.       lbgrkanus.setSize(100,15);
  58.       this.add(lbgrkanus);
  59.       lbdaten=new Label("Bitte persönliche Daten angeben:");
  60.       lbdaten.setLocation(10,250);
  61.       lbdaten.setSize(250,20);
  62.       this.add(lbdaten);
  63.       lbvnme=new Label("Vorname:");
  64.       lbvnme.setLocation(10,280);
  65.       lbvnme.setSize(60,20);
  66.       this.add(lbvnme);
  67.       lbnnme=new Label("Nachname:");
  68.       lbnnme.setLocation(10,305);
  69.       lbnnme.setSize(70,20);
  70.       this.add(lbnnme);
  71.       lbstrnr=new Label("Straße / Hausnr.:");
  72.       lbstrnr.setLocation(10,330);
  73.       lbstrnr.setSize(100,20);
  74.       this.add(lbstrnr);
  75.       lbplzort=new Label("Plz. / Ort:");
  76.       lbplzort.setLocation(10,355);
  77.       lbplzort.setSize(70,20);
  78.       this.add(lbplzort);
  79.       lbmail=new Label("eMail:");
  80.       lbmail.setLocation(10,380);
  81.       lbmail.setSize(50,20);
  82.       this.add(lbmail);
  83.       lbkorrekt=new Label("Sind alle Angaben korrekt?");
  84.       lbkorrekt.setLocation(450,50);
  85.       lbkorrekt.setSize(150,20);
  86.       this.add(lbkorrekt);
  87.       lbkorrekt.setVisible(false);
  88.  
  89.       //Textfelder
  90.       tfklein=new TextField();
  91.       tfklein.setLocation(450,175);
  92.       tfklein.setSize(30,20);
  93.       tfklein.setText("0");
  94.       this.add(tfklein);
  95.       tfgross=new TextField();
  96.       tfgross.setLocation(450,200);
  97.       tfgross.setSize(30,20);
  98.       tfgross.setText("0");
  99.       this.add(tfgross);
  100.       tfvnme=new TextField();
  101.       tfvnme.setLocation(130,280);
  102.       tfvnme.setSize(150,20);
  103.       this.add(tfvnme);
  104.       tfnnme=new TextField();
  105.       tfnnme.setLocation(130,305);
  106.       tfnnme.setSize(150,20);
  107.       this.add(tfnnme);
  108.       tfstr=new TextField();
  109.       tfstr.setLocation(130,330);
  110.       tfstr.setSize(100,20);
  111.       this.add(tfstr);
  112.       tfnr=new TextField();
  113.       tfnr.setLocation(235,330);
  114.       tfnr.setSize(45,20);
  115.       this.add(tfnr);
  116.       tfplz=new TextField();
  117.       tfplz.setLocation(130,355);
  118.       tfplz.setSize(45,20);
  119.       this.add(tfplz);
  120.       tfort=new TextField();
  121.       tfort.setLocation(180,355);
  122.       tfort.setSize(100,20);
  123.       this.add(tfort);
  124.       tfmail=new TextField();
  125.       tfmail.setLocation(130,380);
  126.       tfmail.setSize(150,20);
  127.       this.add(tfmail);
  128.  
  129.       //Textausgabe
  130.       ausgabe=new TextArea();
  131.       ausgabe.setLocation(400,80);
  132.       ausgabe.setSize(250,190);
  133.       this.add(ausgabe);
  134.       ausgabe.setVisible(false);
  135.  
  136.       //Buttons
  137.       preis=new Button("Kanus buchen");
  138.       preis.setLocation(350,230);
  139.       preis.setSize(100,30);
  140.       preis.addActionListener(this);
  141.       this.add(preis);
  142.       korrekt=new Button("Bestätigen");
  143.       korrekt.setLocation(470,270);
  144.       korrekt.setSize(100,30);
  145.       korrekt.addActionListener(this);
  146.       this.add(korrekt);
  147.       korrekt.setVisible(false);
  148.       abbruch=new Button("Abbrechen");
  149.       abbruch.setLocation(470,320);
  150.       abbruch.setSize(100,30);
  151.       abbruch.addActionListener(this);
  152.       this.add(abbruch);
  153.       abbruch.setVisible(false);
  154.  
  155.       //Dropdowns
  156.       c1=new Choice();
  157.       c1.add("Wetzlar");
  158.       c1.add("Weilburg");
  159.       c1.add("Arfurt");
  160.       c1.add("Limburg");
  161.       c1.setLocation(350,50);
  162.       c1.setSize(100,50);
  163.       this.add(c1);
  164.       c2=new Choice();
  165.       c2.add("Wetzlar ");
  166.       c2.add("Weilburg ");
  167.       c2.add("Arfurt ");
  168.       c2.add("Limburg ");
  169.       c2.setLocation(500,50);
  170.       c2.setSize(100,50);
  171.       this.add(c2);
  172.  
  173.        // Datenbanktreiber laden
  174.        try
  175.        {
  176.          Class.forName("com.mysql.jdbc.Driver");
  177.            System.out.println("Treiber erfolgreich geladen...");
  178.        }//try
  179.        catch(ClassNotFoundException e)
  180.        {
  181.           System.out.println("Fehler beim Laden des Treibers "+e);
  182.            System.exit(0);//Abbruch des Programm
  183.        }// catch(ClassNotFoundException e)
  184.  
  185.  
  186.        // Datenbankverbindung herstellen
  187.        try
  188.        {
  189.            dbVerbindung = DriverManager.getConnection("jdbc:mysql://localhost/kanuverleih","root","");
  190.            System.out.println("Verbindung erfolgreich...");
  191.        }//try
  192.        catch(SQLException e)
  193.        {
  194.           System.out.println("DB-Verbindung Fehler: "+e);
  195.            System.exit(0);
  196.        }// catch(SQLException e)
  197.     }//Init() Ende
  198.  
  199.    // Paint - Methode
  200.     public void paint (Graphics g)
  201.     {
  202.       g.drawImage(lahnverlauf,0,0,this);
  203.       g.setColor( Color.black );
  204.       g.drawRect(0,0,799,599);
  205.    }
  206.  
  207.    // Mouse Listener
  208.    public void mousePressed(MouseEvent evt){ }
  209.     public void mouseEntered(MouseEvent evt) { }
  210.     public void mouseExited(MouseEvent evt) { }
  211.     public void mouseClicked(MouseEvent evt) { }
  212.     public void mouseReleased(MouseEvent evt) { }
  213.  
  214.    // Methode, die auf Ereignisse (z.B. Button betaetigen reagiert)
  215.     public void actionPerformed(ActionEvent e)
  216.     {
  217.       String command = e.getActionCommand();
  218.        if(command=="Kanus buchen");
  219.       {
  220.          sqlString="SELECT Kosten FROM preis";
  221.  
  222.          // Anwenden der SQL Abfrage + Berechnung der Eingaben
  223.          try
  224.          {
  225.             sqlStatement = dbVerbindung.createStatement();
  226.             System.out.println("SQL-Statement erzeugt...");
  227.             resultSet = sqlStatement.executeQuery(sqlString);
  228.  
  229.             // nächste Datensatz holen bis Tabellenende erreicht
  230.             while (resultSet.next())
  231.             {
  232.  
  233.                dbkanu=resultSet.getDouble(1);
  234.                anzkl=tfklein.getText();
  235.                anzgr=tfgross.getText();
  236.                dbanzkl=Double.parseDouble(anzkl);
  237.                dbanzgr=Double.parseDouble(anzgr);
  238.                anzges=dbanzkl+dbanzgr;
  239.                kostges=anzges*dbkanu;
  240.                geskost=Double.toString(kostges);
  241.             }
  242.  
  243.  
  244.             // Tabelle schliessen
  245.             resultSet.close();
  246.             System.out.println("resultSet-Objekt zerstoert...");
  247.             sqlStatement.close();
  248.             System.out.println("sqlStatementobjekt zerstoert");
  249.             System.out.println("Verbindung geschlossen...");
  250.          }//try
  251.             catch(SQLException ex)
  252.             {
  253.             System.out.println("Fehler beim DB-Zugriff!"+ex);
  254.             System.exit(0);
  255.       }// catch(SQLException ex)
  256.  
  257.  
  258.       lbort.setVisible(false);
  259.       lbstart.setVisible(false);
  260.       lbziel.setVisible(false);
  261.       lbanzkanus.setVisible(false);
  262.       lbklkanus.setVisible(false);
  263.       lbgrkanus.setVisible(false);
  264.       lbdaten.setVisible(false);
  265.       lbvnme.setVisible(false);
  266.       lbnnme.setVisible(false);
  267.       lbstrnr.setVisible(false);
  268.       lbplzort.setVisible(false);
  269.       lbmail.setVisible(false);
  270.       tfklein.setVisible(false);
  271.       tfgross.setVisible(false);
  272.       tfnnme.setVisible(false);
  273.       tfvnme.setVisible(false);
  274.       tfstr.setVisible(false);
  275.       tfnr.setVisible(false);
  276.       tfplz.setVisible(false);
  277.       tfort.setVisible(false);
  278.       tfmail.setVisible(false);
  279.       preis.setVisible(false);
  280.       c1.setVisible(false);
  281.       c2.setVisible(false);
  282.  
  283.       lbkorrekt.setVisible(true);
  284.       ausgabe.setVisible(true);
  285.       korrekt.setVisible(true);
  286.       abbruch.setVisible(true);
  287.  
  288.       ausgabe.setText(tfvnme.getText()+" "+tfnnme.getText()+"\n"+tfstr.getText()+" "+tfnr.getText()+"\n"+tfplz.getText()+" "+tfort.getText()+"\n"+tfmail.getText()
  289.                   +"\n\n"+tfklein.getText()+" kleine Kanus"+"\n"+tfgross.getText()+" Große Kanus"+"\n\n"+"Von "+c1.getSelectedItem()+" nach "+c2.getSelectedItem()+"\n\n"+geskost+" Euro Gesamt");
  290.       }
  291.  
  292.       if(command=="Bestätigen")
  293.       {
  294.          try
  295.          {
  296.             sqlStatement = dbVerbindung.createStatement();
  297.             PreparedStatement ps=dbVerbindung.prepareStatement("insert into kunden (Vorname,Name,Strasse,Hausnummer,Postleitzahl,Ort,eMail) values(?,?,?,?,?,?,?)");
  298.             ps.setString(1,tfvnme.getText());
  299.             ps.setString(2,tfnnme.getText());
  300.             ps.setString(3,tfstr.getText());
  301.             ps.setString(4,tfnr.getText());
  302.             ps.setString(5,tfplz.getText());
  303.             ps.setString(6,tfort.getText());
  304.             ps.setString(7,tfmail.getText());
  305.             ps.execute();
  306.             sqlStatement.close();
  307.          }
  308.          catch (SQLException s)
  309.          {
  310.             System.out.println("SQL statement is not executed!");
  311.             s.printStackTrace();
  312.             System.exit(0);
  313.          }
  314.       }
  315.  
  316.       if(command=="Abbrechen")
  317.       {
  318.          lbort.setVisible(true);
  319.          lbstart.setVisible(true);
  320.          lbziel.setVisible(true);
  321.          lbanzkanus.setVisible(true);
  322.          lbklkanus.setVisible(true);
  323.          lbgrkanus.setVisible(true);
  324.          lbdaten.setVisible(true);
  325.          lbvnme.setVisible(true);
  326.          lbnnme.setVisible(true);
  327.          lbstrnr.setVisible(true);
  328.          lbplzort.setVisible(true);
  329.          lbmail.setVisible(true);
  330.          tfklein.setVisible(true);
  331.          tfgross.setVisible(true);
  332.          tfnnme.setVisible(true);
  333.          tfvnme.setVisible(true);
  334.          tfstr.setVisible(true);
  335.          tfnr.setVisible(true);
  336.          tfplz.setVisible(true);
  337.          tfort.setVisible(true);
  338.          tfmail.setVisible(true);
  339.          preis.setVisible(true);
  340.          c1.setVisible(true);
  341.          c2.setVisible(true);
  342.  
  343.          lbkorrekt.setVisible(false);
  344.          ausgabe.setVisible(false);
  345.          korrekt.setVisible(false);
  346.          abbruch.setVisible(false);
  347.       }
  348.    }// actionPerformed() Ende
  349. }// Class Ende
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement