Guest User

Untitled

a guest
Dec 27th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. MapeadorServicio aux = new MapeadorServicio();
  4. Auto aux1 = new Auto(0);
  5. Persistencia p = Persistencia.getInstancia();
  6. p.guardar(aux);
  7.  
  8. }
  9.  
  10. public void guardar(Mapeador map){
  11. if(map.getOid()==0) insertar(map);
  12. else modificar(map);
  13. }
  14.  
  15. private void insertar(Mapeador m) {
  16. int oid = proximoOid();
  17. m.setOid(oid);
  18. ArrayList<String> sqls = m.getSqlInsert();
  19. if (!base.transaccion(sqls)){
  20. m.setOid(0);
  21. }
  22.  
  23. }
  24.  
  25. public boolean transaccion(ArrayList<String> sqls){
  26. try {
  27. conexion.setAutoCommit(false); //begin T
  28. for(String sql:sqls){
  29. if(actualizar(sql)==-1){
  30. conexion.rollback();
  31. return false;
  32. }
  33. }
  34. conexion.commit();
  35. return true;
  36.  
  37. } catch (SQLException ex) {
  38. System.out.println("Error en T:" + ex.getMessage());
  39. return false;
  40. }finally{
  41. try {
  42. conexion.setAutoCommit(true); //end T
  43. } catch (SQLException ex) {
  44. }
  45. }
  46.  
  47. }
  48.  
  49. private Persistencia() {
  50. base = BaseDatos.getInstancia();
  51. try {
  52. base.conectar("jdbc:mysql://localhost/AutosYa.com", "root", "root");
  53. } catch (ClassNotFoundException ex) {
  54. Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
  55. } catch (InstantiationException ex) {
  56. Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
  57. } catch (IllegalAccessException ex) {
  58. Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
  59. }
  60. }
Add Comment
Please, Sign In to add comment