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 126 127 128
|
public class MaterielFactory
{
static Connection con = null;
static PreparedStatement ps = null;
static Materiel m = new Materiel();
public static void Insert()
{
String type = MaterielFrame.styp.getSelectedItem().toString();
String marque = MaterielFrame.saisirMarque.getText().trim();
String modele = MaterielFrame.saisirModele.getText().trim();
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/emprunts", "root", "sa@admin");
ps = con.prepareStatement("INSERT INTO materiel (Type, Marque, Modele) VALUES (?,?,?)");
ps.setString(1, type);
ps.setString(2,marque);
ps.setString(3,modele);
ps.execute();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public Materiel Select(int id)
{
Materiel retValue = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/emprunts", "root", "sa@admin");
ps = con.prepareStatement("SELECT * FROM materiel WHERE ID=?");
ps.setInt(1, id);
ResultSet res = ps.executeQuery();
if(res.next())
{
retValue = _readMat(res);
}
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return retValue;
}
public static void Delete()
{
int iidd = Integer.parseInt(MaterielFrame.saisirNum.getText().trim());
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/emprunts", "root", "sa@admin");
ps = con.prepareStatement("DELETE FROM materiel WHERE ID=?");
ps.setInt(1,iidd);
ps.execute();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void Update()
{
String type = MaterielFrame.styp.getSelectedItem().toString();
String marque = MaterielFrame.saisirMarque.getText().trim();
String modele = MaterielFrame.saisirModele.getText().trim();
int id = Integer.parseInt(MaterielFrame.saisirNum.getText().trim());
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/emprunts", "root", "sa@admin");
ps = con.prepareStatement("UPDATE materiel SET Type=?, Marque=?, Modele=? WHERE ID=?");
ps.setString(1,type);
ps.setString(2,marque);
ps.setString(3, modele);
ps.setInt(4, id);
ps.execute();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static ArrayList<Materiel> List() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/emprunts", "root", "sa@admin");
ps = con.prepareStatement("SELECT * FROM materiel");
ResultSet rs = ps.executeQuery();
ArrayList<Materiel> liste = new ArrayList<Materiel>();
while (rs.next())
{
m = _readMat(rs);
liste.add(m);
}
con.close();
return liste;
}
public static Materiel _readMat(ResultSet rs) throws SQLException
{
int id = rs.getInt("ID");
String type = rs.getString("Type");
String marque = rs.getString("Marque");
String modele = rs.getString("Modele");
Materiel materiel = new Materiel();
materiel.setID(id);
materiel.setType(type);
materiel.setMarque(marque);
materiel.setModele(modele);
return materiel;
}
} |
Partager