-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabonnementCRUD.java
115 lines (94 loc) · 4.1 KB
/
abonnementCRUD.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.abonnement.services;
import edu.abonnement.entities.abonnement;
import edu.abonnement.tools.MyConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author USER
*/
public class abonnementCRUD {
public void ajouterAbonnement(abonnement a){
String requete="INSERT INTO abonnement (id,duree,prix,type)" + "VALUES (?,?,?,?)";
try {
PreparedStatement pst = MyConnection.getInstance().getConnection().prepareStatement(requete);
pst.setInt(1, a.getId());
pst.setInt(2, a.getDuree());
pst.setInt(3, a.getPrix());
pst.setString(4, a.getType());
pst.executeUpdate();
System.out.println("abonnement ajouté");
} catch (SQLException ex) {
Logger.getLogger(abonnementCRUD.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void update (abonnement a) {
try {
String requete = "UPDATE abonnement SET id='"+a.getId()
+ "',duree='"+a.getDuree()+ "',prix='"+a.getPrix()
+"',type='"+a.getType()
+ "' WHERE Id=" + a.getId();
PreparedStatement pst = new MyConnection().cn.prepareStatement(requete);
int rowsUpdated = pst.executeUpdate(requete);
if (rowsUpdated > 0) {
}
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
public void delete(int Id) {
try {
String requete= "DELETE FROM `abonnement` WHERE `abonnement`.`Id` = ? ";
PreparedStatement pst = new MyConnection().cn.prepareStatement(requete); // howa el boustaji eli yhez requete ywasalha lel sever w men server yjib el rep nasn3ou a travers el cn +s
pst.setInt(1, Id);
pst.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(abonnementCRUD.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<abonnement> displayabonnement() {
List<abonnement> abonnementList = new ArrayList<>();
try {
String requete = "SELECT * FROM abonnement";
PreparedStatement pst = new MyConnection().cn.prepareStatement(requete);
ResultSet rs = pst.executeQuery(requete);
while(rs.next()){
abonnement p = new abonnement();
p.setId(rs.getInt(1));
p.setDuree(rs.getInt(2));
p.setPrix(rs.getInt(3));
p.setType(rs.getString(4));
abonnementList.add(p);
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return abonnementList;
}
public ArrayList<abonnement> FindabonnementByid(int Id) {
ArrayList<abonnement> listabonnement = new ArrayList<>();
try {
String requete= "select * from abonnement WHERE Id='" + Id + "' ";
PreparedStatement pst = new MyConnection().cn.prepareStatement(requete);
ResultSet res = pst.executeQuery("select * from abonnement WHERE Id='" + Id + "' ");
abonnement com = null;
while (res.next()) {
com = new abonnement (res.getInt(1),res.getInt(2),res.getInt(3),res.getString(4));
listabonnement .add(com);
}
} catch (SQLException ex) {
Logger.getLogger( abonnementCRUD.class.getName()).log(Level.SEVERE, null, ex);
}System.out.println("abonnement trouve");
return listabonnement ;
}
}