Example usage for org.hibernate JDBCException getErrorCode

List of usage examples for org.hibernate JDBCException getErrorCode

Introduction

In this page you can find the example usage for org.hibernate JDBCException getErrorCode.

Prototype

public int getErrorCode() 

Source Link

Document

Get the vendor specific error code from the underlying SQLException .

Usage

From source file:aseguradora.MedicoVentana.java

public void cargarMedico() {
    try {//from   w  w w.  ja v a 2  s .  c  o m
        String[] columnNames = new String[3];
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        Session session = sesion.openSession();

        Medico med = new Medico();
        Query cons = session.createQuery("from pojo.Medico");
        List<Medico> lista = cons.list();
        Iterator<Medico> iter = lista.iterator();

        while (iter.hasNext()) {
            med = (Medico) iter.next();
            Hibernate.initialize(med.getHospital());
            Vector row = new Vector();
            row.add(med.getCodM());
            row.add(med.getNM());
            row.add(med.getHospital().getCodH());
            model.addRow(row);
        }

        tablaMedico.setModel(model);
        session.close();

    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == TABLA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane,
                    "Tabla no encontrada. Pongase en contacto con el administrador.");
        }
    }
}

From source file:aseguradora.MedicoVentana.java

private void insertarMedico() {
    try {//from  w w w .  j  av a  2 s. c  o m

        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Medico medico = new Medico();
        Hospital hospital = new Hospital(Short.parseShort(etCodHospital.getText().toString()));
        medico.setCodM(Short.parseShort(etCodMedico.getText().toString()));
        medico.setNM(etNomMedico.getText().toString());
        medico.setHospital(hospital);
        session.save(medico);
        tx.commit();
        session.close();

        cargarMedico();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == CLAVE_PRIMARIA_DUPLICADA) {
            JOptionPane.showMessageDialog(rootPane, "Existe un mdico con ese identificador (ID)");
        }
        if (e.getErrorCode() == CLAVE_AJENA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane, "No existe un hospital con ese identificador (ID)");
        }
        if (e.getErrorCode() == VALOR_DEMASIADO_LARGO) {
            JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo");
        }
    }
}

From source file:aseguradora.VistaVentana.java

public void cargarVista() {
    try {/*from  w  ww  . j  av  a  2  s .  c  o m*/
        String[] columnNames = { "Cod. Poliza", "Datos Poliza", "Num", "Nombre Asegurado", "FN" };
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        Session session = sesion.openSession();

        PolizasAsegurados pa = new PolizasAsegurados();
        Query cons = session.createQuery("from pojo.PolizasAsegurados");
        List<PolizasAsegurados> lista = cons.list();
        Iterator<PolizasAsegurados> iter = lista.iterator();

        while (iter.hasNext()) {
            pa = (PolizasAsegurados) iter.next();
            Hibernate.initialize(pa.getId());
            Vector row = new Vector();
            row.add(pa.getId().getCodP());
            row.add(pa.getId().getDatosP());
            row.add(pa.getId().getNum());
            row.add(pa.getId().getNa());
            row.add(formatDate(pa.getId().getFn().toString()));
            model.addRow(row);
        }

        tablaVista.setModel(model);
        session.close();

    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == TABLA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane,
                    "Tabla no encontrada. Pongase en contacto con el administrador.");
        }
    }
}

From source file:aseguradora.VistaVentana.java

private void insertarAsegurado() {
    try {// w w w . ja  v a2  s. c  o m
        Date fechaAsegurado = (Date) jsFechaAsegurado.getValue();
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        PolizasAsegurados pa = new PolizasAsegurados(
                new PolizasAseguradosId(Integer.parseInt(etCodPoliza.getText()), etDatosPoliza.getText(),
                        Integer.parseInt(etNumAsegurado.getText())));
        System.out.println("CODPOLIZA: " + Integer.parseInt(etCodPoliza.getText()));
        pa.getId().setCodP(Integer.parseInt(etCodPoliza.getText()));
        pa.getId().setDatosP(etDatosPoliza.getText());
        pa.getId().setNum(Integer.parseInt(etNumAsegurado.getText()));
        pa.getId().setNa(etNomAsegurado.getText());
        pa.getId().setFn(fechaAsegurado);
        session.save(pa);
        tx.commit();
        session.close();

        cargarVista();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == CLAVE_PRIMARIA_DUPLICADA) {
            JOptionPane.showMessageDialog(rootPane, "Existe un asegurado con ese identificador (ID)");
        }
        if (e.getErrorCode() == CLAVE_AJENA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane, "No existe una pliza con ese identificador (ID)");
        }
        if (e.getErrorCode() == VALOR_DEMASIADO_LARGO) {
            JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo");
        }
    }
}

From source file:aseguradora.VistaVentana.java

private void actualizarVista() {
    try {// w  w  w. j a v a 2 s.  co m
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();

        String hql = "update PolizasAsegurados pa set " + "pa.id.na = :na,pa.id.fn = :fn "
                + "where pa.id.codP = :codp " + "and pa.id.num = :num";
        Date fechaAsegurado = (Date) jsFechaAsegurado.getValue();
        int query = session.createQuery(hql).setString("na", etNomAsegurado.getText())
                .setDate("fn", fechaAsegurado).setInteger("codp", Integer.parseInt(etCodPoliza.getText()))
                .setInteger("num", Integer.parseInt(etNumAsegurado.getText())).executeUpdate();

        tx.commit();
        session.close();
        cargarVista();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == CLAVE_PRIMARIA_DUPLICADA) {
            JOptionPane.showMessageDialog(rootPane, "Existe una pliza con ese identificador (ID)");
        }
        if (e.getErrorCode() == CLAVE_AJENA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane, "No existe una pliza con ese identificador (ID)");
        }
        if (e.getErrorCode() == VALOR_DEMASIADO_LARGO) {
            JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo");
        }
        ;
        if (e.getErrorCode() == ERROR_EDITAR_CLAVE_AJENA) {
            JOptionPane.showMessageDialog(rootPane, "No puedes editar el id de este asegurado");
        }
        ;
    }
}

From source file:aseguradora.VistaVentana.java

private void eliminarAsegurado() {
    try {//  ww w  .  j a  v a  2  s.c  om
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        PolizasAsegurados pa;

        Query q = session
                .createQuery("from PolizasAsegurados as pa " + "where pa.id.codP = ? " + "and pa.id.num = ?");

        q.setInteger(0, Integer.parseInt(etCodPoliza.getText()));
        q.setInteger(1, Integer.parseInt(etNumAsegurado.getText()));

        pa = (PolizasAsegurados) q.uniqueResult();

        session.delete(pa);
        tx.commit();
        session.close();
        cargarVista();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == ERROR_EDITAR_CLAVE_AJENA) {
            JOptionPane.showMessageDialog(rootPane,
                    "No puedes borrar este asegurado, restriccin clave primeria/ajena");
        }
    }
}

From source file:com.gu.management.database.checking.ConnectionCheckResultClassifier.java

License:Apache License

private boolean containsSevereErrorCodeOrMessage(JDBCException jdbcFailureCause) {
    if (severeErrorCodes.contains(jdbcFailureCause.getErrorCode())) {
        return true;
    }//  www.  java  2 s  .  com
    if (containsSevereErrorMessage(jdbcFailureCause)) {
        return true;
    }
    return false;
}

From source file:edu.psu.iam.cpr.core.service.helper.ServiceHelper.java

License:Apache License

/**
 * This routine is used to handle exceptions of the JDBC type.
 * @param log4jLogger contains a log4j Logger instance.
 * @param serviceCoreReturn contains the service core return instance.
 * @param db contains a database connection.
 * @param e contains the JDBCEXception exception information.
 * @return contains the formatted String.
 *///ww w. j av a  2 s  . com
public String handleJDBCException(Logger log4jLogger, ServiceCoreReturn serviceCoreReturn, Database db,
        JDBCException e) {

    String sqlState = e.getSQLState();
    sqlState = (sqlState == null) ? "N/A" : sqlState;
    int errorCode = e.getErrorCode();
    String exceptionMessage = e.getMessage();
    exceptionMessage = (exceptionMessage == null) ? "EMPTY" : exceptionMessage;

    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("Database Exception: ");
    sb.append("Message = ");
    sb.append(exceptionMessage);
    sb.append(", Error Code = ");
    sb.append(errorCode);
    sb.append(", SQL State = ");
    sb.append(sqlState);

    String formattedException = sb.toString();
    log4jLogger.info(formattedException);
    db.rollbackSession();
    return formattedException;
}

From source file:edu.psu.iam.cpr.service.helper.ServiceHelper.java

License:Apache License

/**
 * This routine is used to handle exceptions of the JDBC type.
 * @param log4jLogger contains a log4j Logger instance.
 * @param serviceCoreReturn contains the service core return instance.
 * @param db contains a database connection.
 * @param e contains the JDBCEXception exception information.
 * @return contains the formatted String.
 *///from   www  .j av a  2s.  c  om
public String handleJDBCException(Logger log4jLogger, ServiceCoreReturn serviceCoreReturn, Database db,
        JDBCException e) {

    String sqlState = e.getSQLState();
    sqlState = (sqlState == null) ? "N/A" : sqlState;
    int errorCode = e.getErrorCode();
    String exceptionMessage = e.getMessage();
    exceptionMessage = (exceptionMessage == null) ? "EMPTY" : exceptionMessage;

    StringBuffer sb = new StringBuffer(BUFFER_SIZE);
    sb.append("Database Exception: ");
    sb.append("Message = ");
    sb.append(exceptionMessage);
    sb.append(", Error Code = ");
    sb.append(errorCode);
    sb.append(", SQL State = ");
    sb.append(sqlState);

    String formattedException = sb.toString();
    try {
        serviceCoreReturn.getServiceLogTable().endLog(db, formattedException);
    } catch (Exception e1) {
    }
    log4jLogger.info(formattedException);
    db.rollbackSession();
    return formattedException;
}

From source file:org.springframework.orm.hibernate3.HibernateJdbcException.java

License:Apache License

public HibernateJdbcException(JDBCException ex) {
    super("JDBC exception on Hibernate data access: SQLException for SQL [" + ex.getSQL() + "]; SQL state ["
            + ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex);
}