138 lines
4.7 KiB
Java
138 lines
4.7 KiB
Java
package at.reisinger;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
* Stellt die Verbindung zur "Datenbank" her
|
|
* Die "Datenbank" sind hier nur beispieldaten.
|
|
*/
|
|
public class DBConnection {
|
|
private ArrayList<TradingDeal> tradingDeals;
|
|
private ArrayList<Package> packages;
|
|
private Connection c;
|
|
private Statement stmt;
|
|
|
|
private Connection con(){
|
|
this.c = null;
|
|
try {
|
|
Class.forName("org.postgresql.Driver");
|
|
this.c = DriverManager
|
|
.getConnection("jdbc:postgresql://postgres.dergeorg.at:5432/mtcg",
|
|
"user", "user");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
System.err.println(e.getClass().getName()+": "+e.getMessage());
|
|
System.exit(0);
|
|
}
|
|
System.out.println("Opened database");
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* Erstellt alle Beispieldaten und simuliert somit den
|
|
* Verbindungsaufbau zu einer DB
|
|
*/
|
|
public DBConnection(){
|
|
this.c = con();
|
|
System.out.println("Create Tables start");
|
|
try {
|
|
createTables();
|
|
} catch (SQLException throwables) {
|
|
throwables.printStackTrace();
|
|
}
|
|
/*
|
|
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
|
|
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
|
|
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
|
|
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
|
|
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
|
|
ArrayList<Card> deckArrList = new ArrayList<Card>();
|
|
ArrayList<Card> stackArrList = new ArrayList<Card>();
|
|
deckArrList.add(c1);
|
|
deckArrList.add(c2);
|
|
stackArrList.add(c3);
|
|
stackArrList.add(c4);
|
|
stackArrList.add(c5);
|
|
Cards deck = new Cards(deckArrList);
|
|
Cards stack = new Cards(stackArrList);
|
|
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
|
|
this.tradingDeals = new ArrayList<TradingDeal>();
|
|
this.packages = new ArrayList<Package>();
|
|
this.tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
|
|
this.packages.add(new Package(stack, "Test at.reisinger.Package", 100));
|
|
*/
|
|
}
|
|
|
|
private void createTables() throws SQLException {
|
|
this.c = con();
|
|
try{
|
|
stmt = this.c.createStatement();
|
|
String sql = "CREATE TABLE IF NOT EXISTS USERS " +
|
|
"(username TEXT PRIMARY KEY NOT NULL," +
|
|
" nachname TEXT NOT NULL, " +
|
|
" email TEXT NOT NULL, " +
|
|
" password TEXT NOT NULL)";
|
|
stmt.executeUpdate(sql);
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch ( Exception e ) {
|
|
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
|
|
System.exit(0);
|
|
}
|
|
System.out.println("User Table created");
|
|
}
|
|
|
|
public boolean addUser(String username, String password, String nachname, String email){
|
|
this.c = con();
|
|
try{
|
|
stmt = this.c.createStatement();
|
|
String sql = "INSERT INTO users (username, nachname, email, password) values (\'" + username +"\',\'" + nachname + "\',\'" + email + "\',\'" + password + "\')";
|
|
System.out.println(sql);
|
|
stmt.executeUpdate(sql);
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch ( Exception e ) {
|
|
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
|
|
return false;
|
|
}
|
|
System.out.println("User added");
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Holt alle verfügbaren TradingDeals
|
|
* @return Alle Trading Deals
|
|
*/
|
|
public ArrayList<TradingDeal> getAllTradingDeals(){
|
|
return this.tradingDeals;
|
|
}
|
|
|
|
/**
|
|
* Holt alle verfügbaren Packages
|
|
* @return Alle Packages
|
|
*/
|
|
public ArrayList<Package> getAllPackages(){
|
|
return this.packages;
|
|
}
|
|
|
|
/**
|
|
* Fügt einen neuen Tradingdeal zum at.reisinger.Store hinzu
|
|
* @param tradingDeal Der neue Deal
|
|
*/
|
|
public void addTradingDeal(TradingDeal tradingDeal){
|
|
this.tradingDeals.add(tradingDeal);
|
|
}
|
|
|
|
// /**
|
|
// * Fügt ein neues at.reisinger.Package zu dem at.reisinger.Store hinzu
|
|
// * @param packageDeal Das neue at.reisinger.Package
|
|
// */
|
|
// public void addPackage(at.reisinger.Package packageDeal){
|
|
// this.packages.add(packageDeal);
|
|
// }
|
|
}
|