Ok, bare lige for at være helt sikker, inden jeg bestilller webhotel. Nedenstående klasse skulle altså være ok, når den kommer ud på serveren. Måske med lidt tilpasning af brugernavan og password?
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
public class DAO {
private static DAO uniqueInstance;
private static Connection con;
private DAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:
mysql://localhost/fodbold",
"bruger", "pass");
} catch (Exception e) {
System.err.println("ERROR DAO1: " + e.getMessage());
e.printStackTrace();
}
}
public static DAO getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new DAO();
}
return uniqueInstance;
}
public void addPlayer(Player p) {
boolean success = false;
try {
Statement stmt = con.createStatement();
int i = -1; //Viser om spilleren er admin
if (p.isAdmin()) {
i = 1;
} else {
i = 0;
}
String todo = "INSERT INTO spiller VALUES('" + p.getUsername() + "', '" + p.getPassword() + "', " + i + ", 0, 0, 0, '" + p.getFirstName() + "', '" + p.getLastName() + "');";
success = stmt.execute(todo);
} catch (SQLException e) {
e.printStackTrace();
}
if(!success){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Spiller ikke oprettet" ));
}
}
public void deletePlayer(Player p){
try{
Statement stmt = con.createStatement();
String todo = "delete from spiller where brugernavn = '"+ p.getUsername() + "';";
String todo2 = "delete from deltageretraening where spillerId = '"+ p.getUsername() + "';"; //Fjerner spilleren fra tilmeldte træninger
String todo3 = "delete from deltagerekamp where spillerId = '"+ p.getUsername() + "';"; //Fjerner spilleren fra tilmeldte kampe
stmt.execute(todo);
stmt.execute(todo2);
stmt.execute(todo3);
} catch(Exception e){
}
}
public void incYellowStatus(Player p){
try{
Statement stmt = con.createStatement();
int count = p.getYellowCardCount();
System.out.println(count);
String todo = "update spiller set guleKort = '" + count +"' where brugernavn ='" + p.getUsername() +"';";
boolean success = stmt.execute(todo);
} catch (Exception e){
}
}
public void decYellowStatus(Player p){
int count = 0;
try{
Statement stmt = con.createStatement();
count = p.getYellowCardCount();
String todo = "update spiller set guleKort = '" + count +"' where brugernavn ='" + p.getUsername() +"';";
boolean success = stmt.execute(todo);
} catch (Exception e){
}
}
public void incRedStatus(Player p){
try{
Statement stmt = con.createStatement();
int count = p.getRedCardCount();
String todo = "update spiller set roedeKort = '" + count +"' where brugernavn ='" + p.getUsername() +"';";
boolean success = stmt.execute(todo);
} catch (Exception e){
}
}
public void decRedStatus(Player p){
int count = 0;
try{
Statement stmt = con.createStatement();
count = p.getRedCardCount();
String todo = "update spiller set roedeKort = '" + count +"' where brugernavn ='" + p.getUsername() +"';";
boolean success = stmt.execute(todo);
} catch (Exception e){
}
}
public void incGoalStatus(Player p){
try{
Statement stmt = con.createStatement();
int count = p.getGoalsScored();
String todo = "update spiller set maal = '" + count +"' where brugernavn ='" + p.getUsername() +"';";
boolean success = stmt.execute(todo);
} catch (Exception e){
}
}
public void decGoalStatus(Player p){
int count = 0;
try{
Statement stmt = con.createStatement();
count = p.getGoalsScored();
String todo = "update spiller set maal = '" + count +"' where brugernavn ='" + p.getUsername() +"';";
boolean success = stmt.execute(todo);
} catch (Exception e){
}
}
public List getAllPlayers(){
List temp = new ArrayList<Player>();
try{
Statement stmt = con.createStatement();
String todo = "select * from spiller";
ResultSet rs = stmt.executeQuery(todo);
while(rs.next()){
String user = rs.getString(1);
String pass = rs.getString(2);
boolean admin = rs.getInt(3)==1;
int yellow = rs.getInt(4);
int red = rs.getInt(5);
int goals = rs.getInt(6);
String firstName = rs.getString(7);
String lastName = rs.getString(8);
Player p = new Player(user, pass, firstName, lastName, admin, yellow, red, goals);
temp.add(p);
}
} catch(Exception e){
System.out.println("ERROR DAO2: " + e.getMessage());
e.printStackTrace();
}
return temp;
}
public void updatePlayer(Player p){
int admin = -1;
if(p.isAdmin()){
admin = 1;
} else{
admin = 0;
}
try{
Statement stmt = con.createStatement();
String todo = "update spiller set password = '" + p.getPassword() + "', admin='" + admin + "', fornavn='" + p.getFirstName() + "', efternavn='"+p.getLastName()+"', guleKort='"+p.getYellowCardCount() +"', roedeKort ='" + p.getRedCardCount() + "', maal='"+p.getGoalsScored() +"' where brugernavn='" + p.getUsername() +"';";
System.out.println(todo);
stmt.execute(todo);
boolean success = stmt.execute(todo);
} catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public void addNewMatch(Match m){
int i = -1;
if(m.isHome()){
i = 1; //Hjemme kamp
} else{
i = 0; //Ude kamp
}
try {
Statement stmt = con.createStatement();
String dato = m.getDay().getDate() + "-" + (m.getDay().getMonth()+1) + "-" + (m.getDay().getYear()+1900);
String todo = "INSERT INTO kamp VALUES(" + m.getID() + ", '" + m.getOpponent() + "', " + i + ", '" + dato +"', " + m.getTime() + ", 0, 0, 0);";
boolean success = stmt.execute(todo);
} catch (SQLException e) {
System.out.println("ERROR DAO3: " + e.getMessage());
e.printStackTrace();
}
}
public void deleteMatch(Match m){
try{
Statement stmt = con.createStatement();
Statement stmt2 = con.createStatement();
String todo = "delete from kamp where id ='" + m.getID() +"';";
String todo2 = "delete from deltagerekamp where kampId ='" + m.getID() +"';";
stmt.execute(todo);
stmt2.execute(todo2);
}catch(Exception e){
e.printStackTrace();
}
}
public void finishMatch(Match m){
try {
Statement stmt = con.createStatement();
String todo ="update kamp set maalFor = '"+ m.getGoalsFor() + "', maalImod = '"+ m.getGoalsAgainst()+"', spillet='1' where id ='" +m.getID()+"' ";
stmt.execute(todo);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List getAllMatches(){
List temp = new ArrayList<Match>();
try {
Statement stmt = con.createStatement();
String todo="select * from kamp";
ResultSet rs = stmt.executeQuery(todo);
while(rs.next()){
String id = rs.getString(1);
String opponent = rs.getString(2);
boolean home = rs.getInt(3)==1;
Date day = getDate(rs.getString(4));
int time = rs.getInt(5);
int goalsFor = rs.getInt(6);
int goalsAgainst = rs.getInt(7);
boolean played = rs.getInt(8) ==1;
Match m = new Match(id, opponent, home, day, time, goalsFor, goalsAgainst);
m.setPlayed(played);
temp.add(m);
}
return temp;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public List getMatchParticipants(String matchId){ //Finder alle deltagere til en bestemt kamp
List temp = new ArrayList();
List<Player> allPlayers = this.getAllPlayers();
List<Player> retur = new ArrayList<Player>();
try {
Statement stmt = con.createStatement();
String todo = "select * from deltagerekamp where kampId = '" + matchId + "';";
ResultSet rs = stmt.executeQuery(todo);
while(rs.next()){
String tId = rs.getString(1);
String sId = rs.getString(2);
temp.add(sId);
System.out.println(sId);
}
for(int i = 0; i < temp.size(); i++){
for(int j = 0; j < allPlayers.size(); j++){
if(temp.get(i).equals(allPlayers.get(j).getUsername())){
retur.add(allPlayers.get(j));
}
}
}
return retur;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("ERROR");
return null;
}
}
public void signUpMatch(Player p, String matchId){
try{
Statement stmt = con.createStatement();
String todo ="insert into deltagerekamp values('" + matchId + "', '" + p.getUsername() + "');";
stmt.execute(todo);
} catch(Exception e){
}
}
public void signOffMatch(Player p, String matchId){
try{
Statement stmt = con.createStatement();
String todo = "delete from deltagerekamp where kampId = '" + matchId + "' and spillerUser = '"+ p.getUsername() + "';";
stmt.execute(todo);
} catch(Exception e){
}
}
public void addNewTraining(Training t){
try{
Statement stmt = con.createStatement();
String dato = t.getDay().getDate() + "-" + (t.getDay().getMonth()+1) + "-" + (t.getDay().getYear()+1900);
String todo = "insert into traening values('"+ t.getID() + "', '" + dato + "', '" + t.getTime() + "');";
boolean success = stmt.execute(todo);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void deleteTraining(Training t){
try{
Statement stmt1 = con.createStatement();
Statement stmt2 = con.createStatement();
String todo1 = "delete from deltageretraening where traeningId = '" + t.getID()+ "';";
String todo2 ="delete from traening where id = '" + t.getID() + "';";
stmt1.execute(todo1);
stmt2.execute(todo2);
} catch (Exception e){
System.out.println(e.getMessage());
}
}
public List<Training> getAllTrainings(){
try{
List<Training> temp = new ArrayList<Training>();
Statement stmt = con.createStatement();
String todo = "select * from traening;";
ResultSet rs = stmt.executeQuery(todo);
while(rs.next()){
String id = rs.getString(1);
Date day = getDate(rs.getString(2));
int time = rs.getInt(3);
Training t = new Training(id, day, time);
temp.add(t);
}
return temp;
}catch (Exception e){
return null;
}
}
public void signUpTraining(Player p, String trainingId){
try{
Statement stmt = con.createStatement();
String todo ="insert into deltageretraening values('" + trainingId + "', '" + p.getUsername() + "');";
stmt.execute(todo);
} catch(Exception e){
}
}
public void signOffTraining(Player p, String trainingId){
try{
Statement stmt = con.createStatement();
String todo = "delete from deltageretraening where traeningId = '" + trainingId + "' and spillerId = '"+ p.getUsername() + "';";
stmt.execute(todo);
} catch(Exception e){
}
}
public List getTrainingParticipants(String trainingId){ //Finder alle deltagere til en bestemt træning
List temp = new ArrayList();
List<Player> allPlayers = this.getAllPlayers();
List<Player> retur = new ArrayList<Player>();
try {
Statement stmt = con.createStatement();
String todo = "select * from deltageretraening where traeningId = '" + trainingId + "';";
ResultSet rs = stmt.executeQuery(todo);
while(rs.next()){
String tId = rs.getString(1);
String sId = rs.getString(2);
temp.add(sId);
System.out.println(sId);
}
for(int i = 0; i < temp.size(); i++){
for(int j = 0; j < allPlayers.size(); j++){
if(temp.get(i).equals(allPlayers.get(j).getUsername())){
retur.add(allPlayers.get(j));
}
}
}
return retur;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("ERROR");
return null;
}
}
public Date getDate(String s){
int dayTemp = -1, monthTemp = -1, yearTemp = -1, first = -1;
String ny = "";
String temp = "";
System.out.println();
for(int j = s.length()-1; j > -1; j--){
if((""+s.charAt(j)).equals("-")){
if(yearTemp == -1){
yearTemp =Integer.parseInt(temp);
} else if(monthTemp == -1){
monthTemp = Integer.parseInt(temp);
} else{
}
temp = "";
} else{
temp = s.charAt(j) + temp;
}
}
dayTemp = Integer.parseInt(temp);
return new Date((yearTemp-1900), (monthTemp-1), dayTemp);
}
}
Tak for hjælpen, smid et svar...