Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- MapeadorServicio aux = new MapeadorServicio();
- Auto aux1 = new Auto(0);
- Persistencia p = Persistencia.getInstancia();
- p.guardar(aux);
- }
- public void guardar(Mapeador map){
- if(map.getOid()==0) insertar(map);
- else modificar(map);
- }
- private void insertar(Mapeador m) {
- int oid = proximoOid();
- m.setOid(oid);
- ArrayList<String> sqls = m.getSqlInsert();
- if (!base.transaccion(sqls)){
- m.setOid(0);
- }
- }
- public boolean transaccion(ArrayList<String> sqls){
- try {
- conexion.setAutoCommit(false); //begin T
- for(String sql:sqls){
- if(actualizar(sql)==-1){
- conexion.rollback();
- return false;
- }
- }
- conexion.commit();
- return true;
- } catch (SQLException ex) {
- System.out.println("Error en T:" + ex.getMessage());
- return false;
- }finally{
- try {
- conexion.setAutoCommit(true); //end T
- } catch (SQLException ex) {
- }
- }
- }
- private Persistencia() {
- base = BaseDatos.getInstancia();
- try {
- base.conectar("jdbc:mysql://localhost/AutosYa.com", "root", "root");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
Add Comment
Please, Sign In to add comment