Exécuter une classe java via l'attribut action de <h:commandButton>
Bonjour,
je travaille sur un projet en JSF sous Netbeans et mon problème est le suivant :
- j'ai développé une vue jsf : index.xhtml voici son code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Suppression du contenu de la Table MySQL</title>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Vider la table" action="#{Delete}"></h:commandButton>
</h:form>
</h:body>
</html> |
- et voici la classe Delete.java appelée par l'attribut action de <h:commandButton> :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.sql.*;
public class Delete{
public static void main(String[] args) {
System.out.println
("Example for Deleting All Rows from a database Table!");
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");
try{
Statement st = con.createStatement();
String sql = "DELETE FROM employee";
int delete = st.executeUpdate(sql);
if(delete == 0){
System.out.println("All rows are completelly deleted!");
}
}
catch(SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
} |
Mon problème est quand je clique sur le bouton : "Vider la table" dans la page index.xhtml rien ne se passe (la classe Delete.java) n'est pas appelée (exécutée)).
je demande si vous avez une solution pour mon problème ou toute autre solution qui permettra d’exécuter cette classe Java à partir du bouton de la vue jsf.
Merci d'avance