package com.myapp.struts;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.sql.*;
import java.util.ArrayList;
import javax.sql.*;
import org.apache.struts.Globals;
/**
*
* @author Administrateur
* @version
*/
public class InitCombo extends Action {
private DataSource dataSource;
public ArrayList agentList = new ArrayList();
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
login formulaire = (login) form;
int taille = getagents().size();
String[] chaine = (String[]) agentList.toArray(new String[taille]);
formulaire.set("valeursnoms",chaine);
return mapping.findForward("afficheComboLogin");
}
// liste des valeurs du combo récupéré depuis la base de données
private ArrayList getagents(){
Connection conn = null;
Statement stmt = null;
PreparedStatement prpStmt = null;
ResultSet rs = null;
StringBuffer resultString ;
try{
/** Ici, 'alize' est associé à la clef de la DataSource dans struts-config.xml: */
dataSource = (DataSource)servlet.getServletContext().getAttribute("alize");
// dataSource = getDataSource(request);
conn = dataSource.getConnection();
String sqlQuery = "SELECT * FROM OPERATEURS";
prpStmt = conn.prepareStatement(sqlQuery);
rs = prpStmt.executeQuery();
/** Ici, nous avons mis le champ 2 (le nom) dans la agentList: */
while (rs.next()) {
agentList.add(rs.getObject(2));
//(new row());
}
rs.close();
} catch ( SQLException e ) {
System.err.println("SQL Exception occured while accessing the table" );
e.printStackTrace();
return null;
} catch ( Exception e ) {
e.printStackTrace();
return null;
}
return agentList;
}
}
Partager