public class Datei implements ActionListener
{
private Container con = null;
// Layout
private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gbc = null;
private JButton btnSpeichern = new JButton("Speichern");
private JLabel jlTitel = new JLabel("Datenbankverbindung");
private JLabel jlHost = new JLabel("Host: ");
private JLabel jlDatenbank = new JLabel("DB-Name: ");
private JLabel jlBenutzer = new JLabel("Benutzername: ");
private JLabel jlpw = new JLabel("Passwort: ");
private JTextField jtfHost = new JTextField("localhost");
private JTextField jtfDatenbank = new JTextField("test");
private JTextField jtfBenutzername = new JTextField("root");
private JTextField jtfpw = new JTextField("");
// DB-Login
private String dbHost = null;
private String dbName = null;
private String dbBenutzer = null;
private String dbPass = null;
/**
* Konstruktor - Abfrage, ob Datei mit DB-Daten existiert oder nicht und den
* entsprechenden Reaktionen
*
* @param con
* Container für den Inhalt
*/
public Datei(Container con)
{
this.con = con;
try
{
new FileReader("vokabel.dat");
boolean erg=this.readFile();
if (!erg)
{
JOptionPane
.showMessageDialog(this.con,
"Es gibt ein Problem mit der Datenbank.\nBitte überprüfen Sie ihre angaben.");
}
}
catch (FileNotFoundException e)
{
this.createFile();
}
}
/**
* Liest die DB-Daten aus einer angelegten( createFile() ) Datei.
*
* @return ret Boolean, ob Datei gelesen werden konnte
*/
private boolean readFile()
{
boolean ret = false;
FileReader fr = null;
StringBuffer zeile = new StringBuffer();
int temp;
String[] split;
try
{
fr = new FileReader("vokabel.dat");
while ((temp = fr.read()) != -1)
{
zeile.append((char) temp);
}
split = zeile.toString().split(";");
this.dbHost = split[0];
this.dbName = split[1];
this.dbBenutzer = split[2];
this.dbPass = split[3];
System.out.println(this.dbHost + " - " + this.dbName + " - "
+ this.dbBenutzer + " - " + this.dbPass);
ret = true;
}
catch (FileNotFoundException e)
{
this.createFile();
}
catch (IOException e)
{
System.out.println("readFile() - " + e.getMessage());
}
finally
{
if (fr != null)
{
try
{
fr.close();
}
catch (IOException e)
{
System.out
.println("readFile() - Konnte FileReader nicht schließen!");
}
}
}
return ret;
}
/**
* Zeichnet die Eingabemaske.
*/
private void createFile()
{
this.con.removeAll();
this.con.repaint();
this.con.setLayout(this.gbl);
// Zeile 1
zeichneElement(this.con, this.jlTitel, 0, 0, 2, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
this.jlTitel.setFont(new Font("Arial", Font.BOLD, 20));
// Zeile 2
zeichneElement(this.con, this.jlHost, 0, 1, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
zeichneElement(this.con, this.jtfHost, 1, 1, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
// Zeile 3
zeichneElement(this.con, this.jlDatenbank, 0, 2, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
zeichneElement(this.con, this.jtfDatenbank, 1, 2, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
// Zeile 4
zeichneElement(this.con, this.jlBenutzer, 0, 3, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
zeichneElement(this.con, this.jtfBenutzername, 1, 3, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
// Zeile 5
zeichneElement(this.con, this.jlpw, 0, 4, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
zeichneElement(this.con, this.jtfpw, 1, 4, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
// Zeile 6
zeichneElement(this.con, this.btnSpeichern, 1, 5, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5), 0, 0);
this.btnSpeichern.addActionListener(this);
}
/**
* Speichert die DB-Daten ab.
*/
public void actionPerformed(ActionEvent e)
{
FileWriter fw = null;
try
{
fw = new FileWriter("vokabel.dat");
String str = (this.jtfHost.getText() + ";"
+ this.jtfDatenbank.getText() + ";"
+ this.jtfBenutzername.getText() + ";" + this.jtfpw
.getText());
fw.write(str);
}
catch (FileNotFoundException e1)
{
System.out.println("Action - " + e1.getMessage());
}
catch (IOException e2)
{
System.out.println("Action - " + e2.getMessage());
}
finally
{
if (fw != null)
{
try
{
fw.close();
}
catch (IOException e1)
{
System.out
.println("Fehler beim schließen von RandomAccessFile:\n"
+ e1.getMessage());
}
}
}
readFile();
}
/**
* Gibt den DB-Host zurück.
*
* @return dbHost Hostname
*/
public String getDbHost()
{
return this.dbHost;
}
/**
* Gibt den DB-Namen zurück.
*
* @return dbName Datenbankname
*/
public String getDbName()
{
return this.dbName;
}
/**
* Gibt den DB-Benutzernamen zurück.
*
* @return dbBenutzer Benutzername der DB
*/
public String getDbBenutzer()
{
return this.dbBenutzer;
}
/**
* Gibt das DB-Passwort zurück.
*
* @return dbPass Passwort für die DB
*/
public String getDbPass()
{
return this.dbPass;
}
/**
* Zeichnet die einzelnen Elemente die Dargestellt werden und benutzt dabei
* das GridBagLayout.
*
* @param co
* Container
* @param com
* Componente
* @param gridx
* x-Position
* @param gridy
* y-Position
* @param gridwidth
* benutzte Platz in x-Richtung
* @param gridheight
* benutzte Platz in y-Richtung
* @param weightx
* Vorranggrösse für benutzten Platz
* @param weighty
* Vorranggrösse für benutzten Platz
* @param anchor
* Aussfüllverhalten des Elements
* @param fill
* Aussfüllverhalten des Elements
* @param insets
* innenliegender Rahmenbereich
* @param ipadx
* aussenliegender Rahmenbereich in x-Richtung
* @param ipady
* aussenliegender Rahmenbereich in y-Richtung
*/
private void zeichneElement(Container co, Component com, int gridx,
int gridy, int gridwidth, int gridheight, double weightx,
double weighty, int anchor, int fill, Insets insets, int ipadx,
int ipady)
{
this.gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
this.gbl.setConstraints(com, this.gbc);
this.con.add(com);
}
}