Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- public class AbfrageDB extends Applet implements MouseListener,ActionListener
- {
- //Datenbankvariablen
- Connection dbVerbindung=null;
- Statement sqlStatement=null;
- ResultSet resultSet=null;
- ResultSetMetaData rsmd = null;
- // graph. Objekte globalisieren
- Image lahnverlauf;
- String sqlString="",strkanukl,strkanugr,strpreis,geskost,anzkl,anzgr,gesanz;
- double dbkanukl,dbkanugr,kostkl,kostgr,dbanzkl,dbanzgr,kostges,dbkanu,anzges;
- Label lbkorrekt,lbort,lbstart,lbziel,lbanzkanus,lbklkanus,lbgrkanus,lbdaten,lbvnme,lbnnme,lbstrnr,lbplzort,lbmail;
- TextField tfpreis, tfklein,tfgross,tfnnme,tfvnme,tfstr,tfnr,tfplz,tfort,tfmail;
- TextArea ausgabe;
- Button preis,buchen,korrekt,abbruch;
- Choice c1,c2;
- //Initialisierung
- public void init()
- {
- //Layout
- this.setLayout(null);
- //Bilder
- lahnverlauf = getImage (getDocumentBase (), "Lahnverlauf.GIF");
- //Labels
- lbort=new Label("Bitte geben sie Start- und Zielort an:");
- lbort.setLocation(350,5);
- lbort.setSize(350,15);
- this.add(lbort);
- lbstart=new Label("Start");
- lbstart.setLocation(350,30);
- lbstart.setSize(50,15);
- this.add(lbstart);
- lbziel=new Label("Ziel");
- lbziel.setLocation(500,30);
- lbziel.setSize(50,15);
- this.add(lbziel);
- lbanzkanus=new Label("Wieviele Kanus werden benötigt?");
- lbanzkanus.setLocation(350,150);
- lbanzkanus.setSize(350,15);
- this.add(lbanzkanus);
- lbklkanus=new Label("Kleine Kanus:");
- lbklkanus.setLocation(350,175);
- lbklkanus.setSize(100,15);
- this.add(lbklkanus);
- lbgrkanus=new Label("Große Kanus:");
- lbgrkanus.setLocation(350,200);
- lbgrkanus.setSize(100,15);
- this.add(lbgrkanus);
- lbdaten=new Label("Bitte persönliche Daten angeben:");
- lbdaten.setLocation(10,250);
- lbdaten.setSize(250,20);
- this.add(lbdaten);
- lbvnme=new Label("Vorname:");
- lbvnme.setLocation(10,280);
- lbvnme.setSize(60,20);
- this.add(lbvnme);
- lbnnme=new Label("Nachname:");
- lbnnme.setLocation(10,305);
- lbnnme.setSize(70,20);
- this.add(lbnnme);
- lbstrnr=new Label("Straße / Hausnr.:");
- lbstrnr.setLocation(10,330);
- lbstrnr.setSize(100,20);
- this.add(lbstrnr);
- lbplzort=new Label("Plz. / Ort:");
- lbplzort.setLocation(10,355);
- lbplzort.setSize(70,20);
- this.add(lbplzort);
- lbmail=new Label("eMail:");
- lbmail.setLocation(10,380);
- lbmail.setSize(50,20);
- this.add(lbmail);
- lbkorrekt=new Label("Sind alle Angaben korrekt?");
- lbkorrekt.setLocation(450,50);
- lbkorrekt.setSize(150,20);
- this.add(lbkorrekt);
- lbkorrekt.setVisible(false);
- //Textfelder
- tfklein=new TextField();
- tfklein.setLocation(450,175);
- tfklein.setSize(30,20);
- tfklein.setText("0");
- this.add(tfklein);
- tfgross=new TextField();
- tfgross.setLocation(450,200);
- tfgross.setSize(30,20);
- tfgross.setText("0");
- this.add(tfgross);
- tfvnme=new TextField();
- tfvnme.setLocation(130,280);
- tfvnme.setSize(150,20);
- this.add(tfvnme);
- tfnnme=new TextField();
- tfnnme.setLocation(130,305);
- tfnnme.setSize(150,20);
- this.add(tfnnme);
- tfstr=new TextField();
- tfstr.setLocation(130,330);
- tfstr.setSize(100,20);
- this.add(tfstr);
- tfnr=new TextField();
- tfnr.setLocation(235,330);
- tfnr.setSize(45,20);
- this.add(tfnr);
- tfplz=new TextField();
- tfplz.setLocation(130,355);
- tfplz.setSize(45,20);
- this.add(tfplz);
- tfort=new TextField();
- tfort.setLocation(180,355);
- tfort.setSize(100,20);
- this.add(tfort);
- tfmail=new TextField();
- tfmail.setLocation(130,380);
- tfmail.setSize(150,20);
- this.add(tfmail);
- //Textausgabe
- ausgabe=new TextArea();
- ausgabe.setLocation(400,80);
- ausgabe.setSize(250,190);
- this.add(ausgabe);
- ausgabe.setVisible(false);
- //Buttons
- preis=new Button("Kanus buchen");
- preis.setLocation(350,230);
- preis.setSize(100,30);
- preis.addActionListener(this);
- this.add(preis);
- korrekt=new Button("Bestätigen");
- korrekt.setLocation(470,270);
- korrekt.setSize(100,30);
- korrekt.addActionListener(this);
- this.add(korrekt);
- korrekt.setVisible(false);
- abbruch=new Button("Abbrechen");
- abbruch.setLocation(470,320);
- abbruch.setSize(100,30);
- abbruch.addActionListener(this);
- this.add(abbruch);
- abbruch.setVisible(false);
- //Dropdowns
- c1=new Choice();
- c1.add("Wetzlar");
- c1.add("Weilburg");
- c1.add("Arfurt");
- c1.add("Limburg");
- c1.setLocation(350,50);
- c1.setSize(100,50);
- this.add(c1);
- c2=new Choice();
- c2.add("Wetzlar ");
- c2.add("Weilburg ");
- c2.add("Arfurt ");
- c2.add("Limburg ");
- c2.setLocation(500,50);
- c2.setSize(100,50);
- this.add(c2);
- // Datenbanktreiber laden
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Treiber erfolgreich geladen...");
- }//try
- catch(ClassNotFoundException e)
- {
- System.out.println("Fehler beim Laden des Treibers "+e);
- System.exit(0);//Abbruch des Programm
- }// catch(ClassNotFoundException e)
- // Datenbankverbindung herstellen
- try
- {
- dbVerbindung = DriverManager.getConnection("jdbc:mysql://localhost/kanuverleih","root","");
- System.out.println("Verbindung erfolgreich...");
- }//try
- catch(SQLException e)
- {
- System.out.println("DB-Verbindung Fehler: "+e);
- System.exit(0);
- }// catch(SQLException e)
- }//Init() Ende
- // Paint - Methode
- public void paint (Graphics g)
- {
- g.drawImage(lahnverlauf,0,0,this);
- g.setColor( Color.black );
- g.drawRect(0,0,799,599);
- }
- // Mouse Listener
- public void mousePressed(MouseEvent evt){ }
- public void mouseEntered(MouseEvent evt) { }
- public void mouseExited(MouseEvent evt) { }
- public void mouseClicked(MouseEvent evt) { }
- public void mouseReleased(MouseEvent evt) { }
- // Methode, die auf Ereignisse (z.B. Button betaetigen reagiert)
- public void actionPerformed(ActionEvent e)
- {
- String command = e.getActionCommand();
- if(command=="Kanus buchen");
- {
- sqlString="SELECT Kosten FROM preis";
- // Anwenden der SQL Abfrage + Berechnung der Eingaben
- try
- {
- sqlStatement = dbVerbindung.createStatement();
- System.out.println("SQL-Statement erzeugt...");
- resultSet = sqlStatement.executeQuery(sqlString);
- // nächste Datensatz holen bis Tabellenende erreicht
- while (resultSet.next())
- {
- dbkanu=resultSet.getDouble(1);
- anzkl=tfklein.getText();
- anzgr=tfgross.getText();
- dbanzkl=Double.parseDouble(anzkl);
- dbanzgr=Double.parseDouble(anzgr);
- anzges=dbanzkl+dbanzgr;
- kostges=anzges*dbkanu;
- geskost=Double.toString(kostges);
- }
- // Tabelle schliessen
- resultSet.close();
- System.out.println("resultSet-Objekt zerstoert...");
- sqlStatement.close();
- System.out.println("sqlStatementobjekt zerstoert");
- System.out.println("Verbindung geschlossen...");
- }//try
- catch(SQLException ex)
- {
- System.out.println("Fehler beim DB-Zugriff!"+ex);
- System.exit(0);
- }// catch(SQLException ex)
- lbort.setVisible(false);
- lbstart.setVisible(false);
- lbziel.setVisible(false);
- lbanzkanus.setVisible(false);
- lbklkanus.setVisible(false);
- lbgrkanus.setVisible(false);
- lbdaten.setVisible(false);
- lbvnme.setVisible(false);
- lbnnme.setVisible(false);
- lbstrnr.setVisible(false);
- lbplzort.setVisible(false);
- lbmail.setVisible(false);
- tfklein.setVisible(false);
- tfgross.setVisible(false);
- tfnnme.setVisible(false);
- tfvnme.setVisible(false);
- tfstr.setVisible(false);
- tfnr.setVisible(false);
- tfplz.setVisible(false);
- tfort.setVisible(false);
- tfmail.setVisible(false);
- preis.setVisible(false);
- c1.setVisible(false);
- c2.setVisible(false);
- lbkorrekt.setVisible(true);
- ausgabe.setVisible(true);
- korrekt.setVisible(true);
- abbruch.setVisible(true);
- ausgabe.setText(tfvnme.getText()+" "+tfnnme.getText()+"\n"+tfstr.getText()+" "+tfnr.getText()+"\n"+tfplz.getText()+" "+tfort.getText()+"\n"+tfmail.getText()
- +"\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");
- }
- if(command=="Bestätigen")
- {
- try
- {
- sqlStatement = dbVerbindung.createStatement();
- PreparedStatement ps=dbVerbindung.prepareStatement("insert into kunden (Vorname,Name,Strasse,Hausnummer,Postleitzahl,Ort,eMail) values(?,?,?,?,?,?,?)");
- ps.setString(1,tfvnme.getText());
- ps.setString(2,tfnnme.getText());
- ps.setString(3,tfstr.getText());
- ps.setString(4,tfnr.getText());
- ps.setString(5,tfplz.getText());
- ps.setString(6,tfort.getText());
- ps.setString(7,tfmail.getText());
- ps.execute();
- sqlStatement.close();
- }
- catch (SQLException s)
- {
- System.out.println("SQL statement is not executed!");
- s.printStackTrace();
- System.exit(0);
- }
- }
- if(command=="Abbrechen")
- {
- lbort.setVisible(true);
- lbstart.setVisible(true);
- lbziel.setVisible(true);
- lbanzkanus.setVisible(true);
- lbklkanus.setVisible(true);
- lbgrkanus.setVisible(true);
- lbdaten.setVisible(true);
- lbvnme.setVisible(true);
- lbnnme.setVisible(true);
- lbstrnr.setVisible(true);
- lbplzort.setVisible(true);
- lbmail.setVisible(true);
- tfklein.setVisible(true);
- tfgross.setVisible(true);
- tfnnme.setVisible(true);
- tfvnme.setVisible(true);
- tfstr.setVisible(true);
- tfnr.setVisible(true);
- tfplz.setVisible(true);
- tfort.setVisible(true);
- tfmail.setVisible(true);
- preis.setVisible(true);
- c1.setVisible(true);
- c2.setVisible(true);
- lbkorrekt.setVisible(false);
- ausgabe.setVisible(false);
- korrekt.setVisible(false);
- abbruch.setVisible(false);
- }
- }// actionPerformed() Ende
- }// Class Ende
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement