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 javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import beans.Faq;
import beans.GameInfo;
import beans.ListOfGames;
import beans.QuestionReponse;
import beans.SiteInfo;
import Interface.SiteDAOInterface;
public class JDBCSiteDAO implements SiteDAOInterface{
public DataSource getDataSource() throws NamingException{
Context namingContext = new InitialContext();
DataSource dataSource = (DataSource)namingContext.lookup ("java:comp/env/jdbc/testDatasource");
return dataSource;
}
@Override
public void loadSiteInfo() {
Connection con=null;
try{
con= getDataSource().getConnection();
Statement stmt = con.createStatement();
ResultSet rs =stmt.executeQuery("select titre, description from SITE");
if(rs.next()){
SiteInfo.setTitre(rs.getString(1));
SiteInfo.setDescription(rs.getString(2));
}
}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 void loadListOfGames() {
Connection con=null;
ArrayList<GameInfo> listOfGame = new ArrayList<GameInfo>();
try{
con= getDataSource().getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select nom,description,controls,numberofplay,numberofrates,rates,num_jeux from JEUX");
while(rs.next()){
GameInfo gi = new GameInfo();
gi.setNom(rs.getString(1));
gi.setDescription(rs.getString(2));
gi.setControls(rs.getString(3));
gi.setNbOfPlays(rs.getInt(4));
gi.setNbOfRates(rs.getInt(5));
gi.setRate(rs.getInt(6));
gi.setNum(rs.getInt(7));
listOfGame.add(gi);
}
ListOfGames.setListOfGames(listOfGame);
}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 void loadFAQ() {
Connection con=null;
ArrayList<QuestionReponse> faq = new ArrayList<QuestionReponse>();
try{
con= getDataSource().getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select text from QUESTION_REPONSE");
while(rs.next()){
QuestionReponse qr = new QuestionReponse();
qr.setQr(rs.getString(1));
faq.add(qr);
}
Faq.setFaq(faq);
}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();
}
}
}
}