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
|
public class AchatDB {
/**
* Méthode qui vérifie si l'id existe dans la table contact et sinon insère une anomalie
* @param obj
* @return boolean
*/
public boolean findContact(Achat obj) {
boolean found = false;
Connection connect = ConnectionDB.getConnection();
try {
Statement stt = null;
stt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet result = stt.executeQuery("SELECT con_num_cli FROM t_contact WHERE con_num_cli = '" + obj.getNum_client() + "'");
result.last();
if (result.getRow() >= 1) {
result.close();
found = true; //Renvoie true si la requête renvoie au moins 1 ligne (donc présent dans la db)
} else {
PreparedStatement pstmt = connect.prepareStatement("INSERT INTO t_anomalie (ano_num_cli, ano_date_achat, ano_num_serie, ano_code_produit, ano_date_ano, ano_code_revendeur, ano_code_ano) VALUES (? , ?::Date , ? , ? , ?::Date , ? , ?)");
pstmt.setString(1, obj.getNum_client());
pstmt.setString(2, obj.getDate_achat());
pstmt.setString(3, obj.getNum_serie());
pstmt.setString(4, obj.getCode_produit());
pstmt.setString(5, new Date().toString());
pstmt.setString(6, obj.getCode_revendeur());
pstmt.setString(7, "03");
int result2 = pstmt.executeUpdate();
if (result2 == 1) //Si l'insert est ok
{
found = false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
connect = null;
return found;
}
} |
Partager