package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import Interface.GameDAOInterface;
import beans.GameInfo;
public class JDBCGameDAO implements GameDAOInterface {
public DataSource getDataSource() throws NamingException{
Context namingContext = new InitialContext();
DataSource dataSource = (DataSource)namingContext.lookup ("java:comp/env/jdbc/testDatasource");
return dataSource;
}
@Override
public ArrayList<String> getGameUrl() {
ArrayList<String> games=new ArrayList<String>();
Connection con=null;
try{
con= getDataSource().getConnection();
Statement stmt = con.createStatement();
ResultSet rs =stmt.executeQuery("select nom from JEUX");
while(rs.next()){
games.add(rs.getString(1));
}
}catch(Exception e){
System.out.println("probleme de connection");
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return games;
}
@Override
public GameInfo getGameInfo(String nom) {
Connection con=null;
GameInfo gi = new GameInfo();
try{
con= getDataSource().getConnection();
PreparedStatement pstmt = con.prepareStatement("select description,controls,numberofplay,numberofrates,rates,num_jeux,lien from JEUX where nom=?");
pstmt.setString(1, nom);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
gi.setNom(nom);
gi.setDescription(rs.getString(1));
gi.setControls(rs.getString(2));
gi.setNbOfPlays(rs.getInt(3));
gi.setNbOfRates(rs.getInt(4));
gi.setRate(rs.getInt(5));
gi.setNum(rs.getInt(6));
gi.setLien(rs.getString(7));
}
}catch(Exception e){
System.out.println("probleme de connection");
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return gi;
}
@Override
public void increaseNbOfPlays(String nom) {
Connection con=null;
try{
con= getDataSource().getConnection();
PreparedStatement pstmt=con.prepareStatement("update JEUX set numberofplay=numberofplay+1 where nom=?");
pstmt.setString(1, nom);
pstmt.executeUpdate();
}catch(Exception e){
System.out.println("probleme de connection");
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public GameInfo getGameOfTheMonth() {
Connection con=null;
GameInfo gi = new GameInfo();
try{
con= getDataSource().getConnection();
Statement stmt=con.createStatement();
ResultSet rs = stmt.executeQuery("select description,controls,numberofplay,numberofrates,rates,nom from JEUX where jeux_du_mois=1");
if(rs.next()){
gi.setDescription(rs.getString(1));
gi.setControls(rs.getString(2));
gi.setNbOfPlays(rs.getInt(3));
gi.setNbOfRates(rs.getInt(4));
gi.setRate(rs.getInt(5));
gi.setNom(rs.getString(6));
}
}catch(Exception e){
System.out.println("probleme de connection");
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return gi;
}
@Override
public float voteForGame(String login, int num, int rate) {
Connection con=null;
float retour=-1;
try{
con= getDataSource().getConnection();
con.setAutoCommit(false);
PreparedStatement pstmt=con.prepareStatement("insert into NOTER (login,num_jeux,note) values(?,?,?)");
pstmt.setString(1, login);
pstmt.setInt(2, num);
pstmt.setInt(3, rate);
pstmt.executeUpdate();
pstmt=con.prepareStatement("update JEUX set numberofrates=numberofrates+1 where num_jeux=?");
pstmt.setInt(1, num);
pstmt.executeUpdate();
pstmt=con.prepareStatement("update JEUX set rates=(rates*(numberofrates-1)+?)/numberofrates where num_jeux=?");
pstmt.setInt(1, rate);
pstmt.setInt(2, num);
pstmt.executeUpdate();
pstmt=con.prepareStatement("Select rates from JEUX where num_jeux=?");
pstmt.setInt(1, num);
ResultSet res=pstmt.executeQuery();
if(res.next()){
retour=res.getFloat(1);
}
con.commit();
con.setAutoCommit(true);
}catch(Exception e){
try {
con.rollback();
PreparedStatement pstmt=con.prepareStatement("Select rates from JEUX where num_jeux=?");
pstmt.setInt(1, num);
ResultSet res=pstmt.executeQuery();
if(res.next()){
retour=res.getFloat(1);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("probleme de connection");
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return retour;
}
}