0% found this document useful (0 votes)
39 views4 pages

Public Class Manipbd (: Public Void Affiche (String Log) (

This Java code defines a class called manipbd that performs CRUD (create, read, update, delete) operations on a database using JPA (Java Persistence API). The class contains methods to add, display, modify, remove, and query users from the database. Queries include selecting all users, selecting by name, and performing a join between the user and operation tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

Public Class Manipbd (: Public Void Affiche (String Log) (

This Java code defines a class called manipbd that performs CRUD (create, read, update, delete) operations on a database using JPA (Java Persistence API). The class contains methods to add, display, modify, remove, and query users from the database. Queries include selecting all users, selecting by name, and performing a join between the user and operation tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package introjpa;

JPA u.setPrenom(prenom);
u.setCin(cin);
import java.util.List; u.setTel(tel);
import javax.persistence.EntityManager; u.setMail(mail);
import javax.persistence.EntityManagerFactory; u.setLog(log);
import javax.persistence.EntityTransaction; u.setPass(pass);
import javax.persistence.Persistence;
import javax.persistence.Query; em.persist(u);

public class manipbd { trans.commit();


em.close();
public void affiche(String log){ emf.close();
System.out.println(" \t (ajout) ====> Objet ajouté !!\n");
EntityManagerFactory emf = }catch(Exception ie){
Persistence.createEntityManagerFactory("introjpaPU"); System.out.println(" \t (ajout) ====> Objet déjà
EntityManager em = emf.createEntityManager(); existant !!\n");
}
User u = em.find(User.class, log); }
if(u!=null){
em.refresh(u); public void supp(String log){
System.out.println("\n \t"+u.getNom()+"
"+u.getPrenom()+" "+u.getCin()+" "+u.getTel()+" "+u.getMail() try{
+" "+u.getLog()+" "+u.getPass()+"\n"); EntityManagerFactory emf =
//System.out.println(u); Persistence.createEntityManagerFactory("introjpaPU");
}else EntityManager em = emf.createEntityManager();
System.out.println(" \t (affiche) ====> Objet EntityTransaction trans=em.getTransaction();
inexistant !!\n"); trans.begin();
em.close();
emf.close(); User u = em.find(User.class, log);
}
em.remove(u);
public void modif (String log,String newnom,String
newprenom,String newcin,String newtel,String newmail,String trans.commit();
newpass){ em.close();
emf.close();
System.out.println(" \t (supp) ====> Objet
try{ supprimé !!\n");
EntityManagerFactory emf = }catch(Exception ie){
Persistence.createEntityManagerFactory("introjpaPU"); System.out.println(" \t (supp) ====> Objet
EntityManager em = emf.createEntityManager(); inexistant !!\n");
EntityTransaction trans=em.getTransaction(); }
trans.begin(); }
User u = em.find(User.class, log); public void reqselect(){
u.setNom(newnom);
u.setPrenom(newprenom); try{
u.setCin(newcin); EntityManagerFactory emf =
u.setTel(newtel); Persistence.createEntityManagerFactory("introjpaPU");
u.setMail(newmail); EntityManager em = emf.createEntityManager();
u.setPass(newpass);
Query req = em.createQuery("Select u from User u");
em.flush();
//Query req = em.createQuery("Select u from User u
trans.commit(); where u.nom='methni'");
em.close();
emf.close(); //Query req = em.createQuery("Select u from User u
System.out.println(" \t (modif) ====> Objet where u.nom='methni' and u.prenom='zied'");
modifié !!\n");
}catch(Exception ie){ //Query req = em.createQuery("Select u from User u
System.out.println(" \t (modif) ====> Objet where u.nom=:m and u.prenom='zied'");
inexistant !!\n");
} //req.setParameter("m", "methni");
}
//Query req = em.createQuery("Select u from User u
public void ajout(String nom,String prenom,String where u.nom like '%b'");
cin,String tel,String mail,String log,String pass){
//Query req = em.createQuery("Select u from User u
try{ where u.nom like '%b%'");
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("introjpaPU"); List<User> l = req.getResultList();
EntityManager em = emf.createEntityManager();
EntityTransaction trans=em.getTransaction(); em.close();
trans.begin(); emf.close();
User u = new User(); if (!l.isEmpty()){
u.setNom(nom); for (int i = 0; i < l.size(); i++) {
System.out.println("\n \t "+l.get(i).getNom()+" "+o2.getTel()+" "+o2.getMail()+" "+o2.getLog()+"
"+l.get(i).getPrenom()+" "+l.get(i).getCin()+" "+l.get(i).getTel() "+o2.getPass()+"\n");
+" "+l.get(i).getMail()+" "+l.get(i).getLog()+" }
"+l.get(i).getPass()+"\n");
} System.out.println(" \t ( Select * ) ====> Liste
affichée !!\n");
System.out.println(" \t ( Select * ) ====> Liste }else
affichée !!\n"); System.out.println(" \t ( Select * ) ====> Aucune
}else resultat !!\n");
System.out.println(" \t ( Select * ) ====> Aucune }catch(Exception ie){
resultat !!\n"); ie.printStackTrace();
}catch(Exception ie){ System.out.println(" \t ( Select * ) ====>
System.out.println(" \t ( Select * ) ====> Erreur !!\n"+ie.getLocalizedMessage());
Erreur !!\n"+ie.getMessage()); }
}
}
}
public void jointure(){
public void selectnomprenom(){
try{
try{ EntityManagerFactory emf =
EntityManagerFactory emf = Persistence.createEntityManagerFactory("introjpaPU");
Persistence.createEntityManagerFactory("introjpaPU"); EntityManager em = emf.createEntityManager();
EntityManager em = emf.createEntityManager();
Query req = em.createQuery("Select
Query req = em.createQuery("Select u.nom,u.prenom u.nom,u.prenom,o.lib from User u, Operation o where
from User u where u.nom like '%b%'"); u.log=o.log");

List l = req.getResultList(); List l = req.getResultList();

em.close(); em.close();
emf.close(); emf.close();

if (!l.isEmpty()){ if (!l.isEmpty()){
for (int i = 0; i < l.size(); i++) { for (int i = 0; i < l.size(); i++) {
String o1=(String)((Object[])l.get(i))[0]; String o1=(String)((Object[])l.get(i))[0];
String o2=(String)((Object[])l.get(i))[1]; String o2=(String)((Object[])l.get(i))[1];
System.out.println("\n \t "+o1+"\t "+o2); String o3=(String)((Object[])l.get(i))[2];
} System.out.println("\n \t "+o1+" "+o2+" "+o3);
}
System.out.println(" \t ( Select * ) ====> Liste
affichée !!\n"); System.out.println("\n \t ( Select * ) ====> Liste
}else affichée !!\n");
System.out.println(" \t ( Select * ) ====> Aucune }else
resultat !!\n"); System.out.println("\n \t ( Select * ) ====> Aucune
}catch(Exception ie){ resultat !!\n");
ie.printStackTrace(); }catch(Exception ie){
System.out.println(" \t ( Select * ) ====> ie.printStackTrace();
Erreur !!\n"+ie.getLocalizedMessage()); System.out.println("\n \t ( Select * ) ====>
} Erreur !!\n"+ie.getLocalizedMessage());
}
} }

public void selectuu(){ public static void main(String args[]){

try{ /*new
EntityManagerFactory emf = manipbd().ajout("slimeni","yahia","08965435","555555","yahia
Persistence.createEntityManagerFactory("introjpaPU"); @gmail.com","yaya","yaya");
EntityManager em = emf.createEntityManager(); new manipbd().affiche("yaya");
new manipbd().modif("yaya", "salamonti", "salamonti",
Query req = em.createQuery("Select u,u from User u "0000", "9999", "[email protected]", "salamonti");
where u.nom like '%b%'"); new manipbd().affiche("yaya");
new manipbd().supp("yaya");*/
List l = req.getResultList(); //new manipbd().reqselect();
//new manipbd().selectnomprenom();
em.close(); //new manipbd().selectuu();
emf.close(); //new manipbd().jointure();
}
if (!l.isEmpty()){ }
for (int i = 0; i < l.size(); i++) {
User o1=(User)((Object[])l.get(i))[0];
User o2=(User)((Object[])l.get(i))[1];
System.out.println("\n \t "+o1.getNom()+"
"+o1.getPrenom()+" "+o1.getCin()+" "+o1.getTel()+"
"+o1.getMail()+" "+o1.getLog()+" "+o1.getPass()+" \t
"+o2.getNom()+" "+o2.getPrenom()+" "+o2.getCin()+"
/*
USER this.mail = mail;
* To change this template, choose Tools | Templates this.pass = pass;
* and open the template in the editor. }
*/
package introjpa; public String getNom() {
return nom;
import java.io.Serializable; }
import javax.persistence.Basic;
import javax.persistence.Column; public void setNom(String nom) {
import javax.persistence.Entity; this.nom = nom;
import javax.persistence.Id; }
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; public String getPrenom() {
import javax.persistence.Table; return prenom;
}
/**
* public void setPrenom(String prenom) {
* @author Administrateur this.prenom = prenom;
*/ }
@Entity
@Table(name = "user", catalog = "sujet", schema = "") public String getCin() {
@NamedQueries({@NamedQuery(name = "User.findAll", return cin;
query = "SELECT u FROM User u"), @NamedQuery(name = }
"User.findByNom", query = "SELECT u FROM User u
WHERE u.nom = :nom"), @NamedQuery(name = public void setCin(String cin) {
"User.findByPrenom", query = "SELECT u FROM User u this.cin = cin;
WHERE u.prenom = :prenom"), @NamedQuery(name = }
"User.findByCin", query = "SELECT u FROM User u WHERE
u.cin = :cin"), @NamedQuery(name = "User.findByTel", query public String getTel() {
= "SELECT u FROM User u WHERE u.tel = :tel"), return tel;
@NamedQuery(name = "User.findByMail", query = "SELECT }
u FROM User u WHERE u.mail = :mail"),
@NamedQuery(name = "User.findByLog", query = "SELECT u public void setTel(String tel) {
FROM User u WHERE u.log = :log"), @NamedQuery(name = this.tel = tel;
"User.findByPass", query = "SELECT u FROM User u WHERE }
u.pass = :pass")})
public class User implements Serializable { public String getMail() {
return mail;
private static final long serialVersionUID = 1L; }
@Basic(optional = false)
@Column(name = "nom", nullable = false, length = 50) public void setMail(String mail) {
private String nom; this.mail = mail;
@Basic(optional = false) }
@Column(name = "prenom", nullable = false, length = 50)
private String prenom; public String getLog() {
@Basic(optional = false) return log;
@Column(name = "cin", nullable = false, length = 50) }
private String cin;
@Basic(optional = false) public void setLog(String log) {
@Column(name = "tel", nullable = false, length = 50) this.log = log;
private String tel; }
@Basic(optional = false)
@Column(name = "mail", nullable = false, length = 50) public String getPass() {
private String mail; return pass;
@Id }
@Basic(optional = false)
@Column(name = "log", nullable = false, length = 50) public void setPass(String pass) {
private String log; this.pass = pass;
@Basic(optional = false) }
@Column(name = "pass", nullable = false, length = 50)
private String pass; @Override
public int hashCode() {
public User() { int hash = 0;
} hash += (log != null ? log.hashCode() : 0);
return hash;
public User(String log) { }
this.log = log;
} @Override
public boolean equals(Object object) {
public User(String log, String nom, String prenom, String cin, // TODO: Warning - this method won't work in the case the
String tel, String mail, String pass) { id fields are not set
this.log = log; if (!(object instanceof User)) {
this.nom = nom; return false;
this.prenom = prenom; }
this.cin = cin; User other = (User) object;
this.tel = tel;
if ((this.log == null && other.log != null) || (this.log != null
&& !this.log.equals(other.log))) {
return false;
}
return true;
}

@Override
public String toString() {
return "introjpa.User[log=" + log + "]";
}
}

You might also like