This repository has been archived on 2020-11-09. You can view files and clone it, but cannot push or open issues or pull requests.
Autovermietung/src/autovermietung/helper/Database.java

233 lines
7.5 KiB
Java

package autovermietung.helper;
import autovermietung.models.Kunde;
import autovermietung.models.Modell;
import autovermietung.models.Pkw;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
public class Database {
private static String strUsername, strPassword, ipStr, dbStr;
public static ArrayList<Kunde> liesKunden() {
ArrayList<Kunde> Kunden = new ArrayList<>();
try {
Connection conn = getConnection();
PreparedStatement kunden = conn.prepareStatement("SELECT * FROM kunde");
ResultSet result = kunden.executeQuery();
while(result.next()) {
Kunden.add(new Kunde(result.getInt(1), result.getString(2), result.getString(3), result.getString(4), result.getString(5), result.getInt(6)));
}
conn.close();
} catch(Exception e) {
System.out.println(e);
}
return Kunden;
}
public static ArrayList<Modell> liesModell() {
ArrayList<Modell> Modelle = new ArrayList<>();
try {
Connection conn = getConnection();
PreparedStatement modelle = conn.prepareStatement("SELECT * FROM modell");
ResultSet result = modelle.executeQuery();
while(result.next()) {
Modelle.add(new Modell(result.getInt(1), result.getString(2), result.getInt(3), result.getInt(4), result.getFloat(5), result.getFloat(6)));
}
conn.close();
} catch(Exception e) {
System.out.println(e);
}
return Modelle;
}
public static ArrayList<Pkw> liesPkw() {
ArrayList<Pkw> Pkws = new ArrayList<>();
try {
Connection conn = getConnection();
PreparedStatement pkws = conn.prepareStatement("SELECT * FROM pkw");
ResultSet result = pkws.executeQuery();
while(result.next()) {
Pkws.add(new Pkw(result.getString(1), result.getDate(2), result.getInt(3), result.getString(4), result.getDate(5), result.getInt(6), result.getInt(7)));
}
conn.close();
} catch(Exception e) {
System.out.println(e);
}
return Pkws;
}
public static void updatePkw(Pkw auto) {
try {
Connection conn = getConnection();
String kennzeichen = auto.getKennzeichen();
Date erstzulassung = auto.getErstzulassung();
int tachostand = auto.getTachostand();
String farbe = auto.getFarbe();
Date ausleihdatum = auto.getAusleihdatum();
int mnr = auto.getMnr();
int knr = auto.getKnr();
PreparedStatement updatePkw = conn.prepareStatement("REPLACE INTO pkw VALUES(\"" + kennzeichen + "\", \"" + erstzulassung + "\", " + tachostand + ", \"" + farbe + "\", \"" + ausleihdatum + "\", " + mnr + ", " + knr + ");");
updatePkw.executeQuery();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void deletePkw(Pkw auto) {
try {
Connection conn = getConnection();
String kennzeichen = auto.getKennzeichen();
String deletePkwSQL = "DELETE FROM pkw WHERE kennzeichen = ?;";
PreparedStatement deletePkw = conn.prepareStatement(deletePkwSQL);
deletePkw.setString(1, kennzeichen);
deletePkw.executeUpdate();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void updateKunde(Kunde kunde) {
try {
Connection conn = getConnection();
int knr = kunde.getKnr();
String vorname = kunde.getVorname();
String nachname = kunde.getNachname();
String anschrift = kunde.getAnschrift();
String telefon = kunde.getTelefon();
int personr = kunde.getPersonr();
PreparedStatement updateKunde = conn.prepareStatement("REPLACE INTO kunde VALUES(" + knr + ", " + vorname + ", " + nachname + ", " + anschrift + ", " + telefon + ", " + personr + ";");
updateKunde.executeQuery();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void deleteKunde(Kunde kunde) {
try {
Connection conn = getConnection();
int knr = kunde.getKnr();
String deleteKundeSQL = "DELETE FROM kunde WHERE knr = ?;";
PreparedStatement deleteKunde = conn.prepareStatement(deleteKundeSQL);
deleteKunde.setInt(1, knr);
deleteKunde.executeUpdate();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void updateModell(Modell modell) {
try {
Connection conn = getConnection();
int mnr = modell.getMnr();
String modellname = modell.getModellname();
int hubraum = modell.getHubraum();
int leistung = modell.getLeistung();
float kmpreis = modell.getKmpreis();
float pauschale = modell.getPauschale();
PreparedStatement updateModell = conn.prepareStatement("REPLACE INTO modell VALUES(" + mnr + ", " + modellname + ", " + hubraum + ", " + leistung + ", " + kmpreis + ", " + pauschale + ";");
updateModell.executeQuery();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void deleteModell(Modell modell) {
try {
Connection conn = getConnection();
int mnr = modell.getMnr();
String deleteModellSQL = "DELETE FROM modell WHERE mnr = ?;";
PreparedStatement deleteModell = conn.prepareStatement(deleteModellSQL);
deleteModell.setInt(1, mnr);
deleteModell.executeUpdate();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
}
private static Connection getConnection() throws Exception {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("database.properties");
// load input
prop.load(input);
// get all values
ipStr = prop.getProperty("ip");
dbStr = prop.getProperty("db");
strUsername = prop.getProperty("username");
strPassword = prop.getProperty("password");
} catch(IOException ex) {
ex.printStackTrace();
} finally {
if(input != null) {
try {
input.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://" + ipStr + "/" + dbStr + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String username = strUsername;
String password = strPassword;
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
} catch(Exception e) {
System.out.println(e);
}
return null;
}
}