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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ged.data.acces;
import com.ged.data.entity.HistoConnexion;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
*
* @author AASPRONI
*/
public class ConnexionManager {
protected DbManager dm = new DbManager();
protected String createSQL = "CREATE TABLE IF NOT EXISTS connexion "
+ "(login TEXT UNIQUE, mdp TEXT, "
+ "dateDebut TEXT, active TEXT, "
+ "cheminFixe TEXT, url TEXT, privilege TEXT);";
public ConnexionManager() {
try {
dm.connect();
dm.update(createSQL);
dm.disconnect();
} catch (Exception e) {
}
}
public int create(HistoConnexion hc){
int res = 0;
try {
dm.connect();
PreparedStatement prep = dm.getPrepare("INSERT INTO connexion VALUES(?, ?, ?, ?, ?, ?, ?);");
prep.setString(1, hc.getLogin());
prep.setString(2, hc.getMdp());
prep.setString(3, hc.getDateDebut());
prep.setString(4, hc.getActive());
prep.setString(5, hc.getCheminFixe());
prep.setString(6, hc.getUrl());
prep.setString(7, hc.getPrivilege());
prep.addBatch();
res = dm.update(prep);
} catch (Exception e) {
res = -1;
}
finally{
dm.disconnect();
return res;
}
}
public int update(HistoConnexion hc){
int res = 0;
try {
dm.connect();
PreparedStatement prep = dm.getPrepare("UPDATE connexion SET "
+ "login = ?,"
+ "mdp = ?,"
+ "dateDebut = ?,"
+ "active = ?,"
+ "cheminFixe = ?,"
+ "url = ?,"
+ "privilege = ?"
+ " WHERE login = ?");
prep.setString(1, hc.getLogin());
prep.setString(2, hc.getMdp());
prep.setString(3, hc.getDateDebut());
prep.setString(4, hc.getActive());
prep.setString(5, hc.getCheminFixe());
prep.setString(6, hc.getUrl());
prep.setString(7, hc.getPrivilege());
prep.setString(8, hc.getLogin());
prep.addBatch();
res = dm.update(prep);
} catch (Exception e) {
res = -1;
}
finally{
dm.disconnect();
return res;
}
}
public int delete(HistoConnexion hc){
int res = 0;
try {
dm.connect();
PreparedStatement prep = dm.getPrepare("DELETE FROM connexion WHERE login = ?");
prep.setString(0, hc.getLogin());
prep.addBatch();
res = dm.update(prep);
} catch (Exception e) {
res = -1;
}
finally{
dm.disconnect();
return res;
}
}
public HistoConnexion obtenirParLogin(String login){
HistoConnexion hc = new HistoConnexion();
try {
dm.connect();
// PreparedStatement prep = dm.getPrepare("SELECT * FROM connexion WHERE login = ?");
// prep.setString(0, login);
// prep.addBatch();
//
// ResultSet rs = dm.getQuery(prep);
ResultSet rs = dm.getQuery("SELECT * FROM connexion WHERE login ='"+login+"'");
if(rs.next()){
hc.setLogin(rs.getString("login"));
hc.setMdp(rs.getString("mdp"));
hc.setDateDebut(rs.getString("dateDebut"));
hc.setActive(rs.getString("active"));
hc.setCheminFixe(rs.getString("cheminFixe"));
hc.setUrl(rs.getString("url"));
hc.setPrivilege(rs.getString("privilege"));
}
else{
hc = null;
}
} catch (Exception e) {
hc = null;
} finally {
dm.disconnect();
return hc;
}
}
public HistoConnexion obtenirConnexionActive(){
HistoConnexion hc = new HistoConnexion();
try {
dm.connect();
ResultSet rs = dm.getQuery("SELECT * FROM connexion WHERE active ='true'");
if(rs.next()){
hc.setLogin(rs.getString("login"));
hc.setMdp(rs.getString("mdp"));
hc.setDateDebut(rs.getString("dateDebut"));
hc.setActive(rs.getString("active"));
hc.setCheminFixe(rs.getString("cheminFixe"));
hc.setUrl(rs.getString("url"));
hc.setPrivilege(rs.getString("privilege"));
}
else{
hc = null;
}
} catch (Exception e) {
System.out.println("exception "+e.getMessage());
hc = null;
} finally {
dm.disconnect();
return hc;
}
}
public ResultSet getquery(String query){
ResultSet rs = null;
try {
dm.connect();
rs = dm.getQuery(query);
while(rs.next()){
System.out.println("Remote : "+rs.getString("login"));
System.out.println("Local : "+rs.getString("active"));
System.out.println(" ");
}
dm.disconnect();
} catch (Exception e) {
rs = null;
}
finally{
return rs;
}
}
} |
Partager