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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| package jsf2;
import java.sql.*;
import java.util.*;
import javax.faces.bean.ManagedBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
@ManagedBean
public class crud {
private String prenom;
private String nom;
private int age;
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void ajouter(){
PreparedStatement ps = null;
try
{
Connection con = getDBConnection();
String sql = "INSERT INTO etudiants(prenom,nom,age) VALUES(?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, getPrenom());
ps.setString(2, getNom());
ps.setInt(3, getAge());
ps.executeUpdate();
System.out.println("Data Added Successfully");
}
catch(Exception ex)
{
System.out.println("Your query is not working");
ex.printStackTrace();
}
}
public void get_liste(){
PreparedStatement ps = null;
public static ArrayList<Etudiant> etudiants = new ArrayList<Etudiant>();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
String sql = "SELECT * FROM etudiants";
ps = con.prepareStatement(sql);
ResultSet r=ps.executeQuery(sql);
while(r.next())
{
Etudiant e = new Etudiant();
}
}
catch(Exception ex)
{
System.out.println("Your query is not working");
ex.printStackTrace();
}
}
public Connection getDBConnection() {
Connection dbConnection = null;
String URL = "jdbc:mysql://localhost:3306/test";
String USER = "root";
String PASSWORD = "root";
String DRIVER = "com.mysql.jdbc.Driver";
try {
Class.forName(DRIVER);
dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Connection completed.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
System.out.println(cnfe.getMessage());
System.exit(-1);
}
return dbConnection;
}
} |
Partager