Example usage for java.sql SQLException toString

List of usage examples for java.sql SQLException toString

Introduction

In this page you can find the example usage for java.sql SQLException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:application.bbdd.pool.java

public static void liberaConexion(Connection conexion) {
    try {// w w w . j a  v a  2  s  .c  om
        if (null != conexion)
            // En realidad no cierra, solo libera la conexion.
            conexion.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.toString());
    }
}

From source file:com.sql.SECExceptions.java

/**
 * Inserts an exception to the database/*from  ww w  .  ja va  2  s.c  o m*/
 *
 * @param item SECExceptionsModel
 * @return boolean
 */
public static boolean insertException(SECExceptionsModel item) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "INSERT INTO SECExceptions (" + "className, " + "methodName, " + "exceptionType, "
                + "exceptionDescrption, " + "timeOccurred " + ") VALUES (" + "?, " + "?, " + "?, " + "?, "
                + "GETDATE())";
        ps = conn.prepareStatement(sql);
        ps.setString(1, item.getClassName());
        ps.setString(2, item.getMethodName());
        ps.setString(3, item.getExceptionType());
        ps.setString(4, item.getExceptionDescription());
        ps.executeUpdate();
    } catch (SQLException ex) {
        System.out.println(ex.toString());
        return true;
    } finally {
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(conn);
    }
    return false;
}

From source file:application.bbdd.pool.java

public static void realizaConsulta2() {
    Connection conexion = null;/*from w w w.  j  ava  2s  .  c o m*/
    Statement sentencia = null;
    ResultSet rs = null;

    try {
        conexion = getConexion();
        sentencia = conexion.createStatement();
        rs = sentencia.executeQuery("select count(*) from db");
        rs.next();
        JOptionPane.showMessageDialog(null, "El numero de bd es: " + rs.getInt(1));
        logStatistics();

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.toString());
    } finally {
        try {
            rs.close();
            sentencia.close();
            liberaConexion(conexion);
        } catch (Exception fe) {
            JOptionPane.showMessageDialog(null, fe.toString());
        }
    }
}

From source file:application.bbdd.pool.java

public static void realizaConsulta1() {
    Connection conexion = null;// w  w w .ja v a 2 s. c  o m
    Statement sentencia = null;
    ResultSet rs = null;

    try {
        // BasicDataSource nos reserva una conexion y nos la devuelve
        conexion = getConexion();
        sentencia = conexion.createStatement();
        rs = sentencia.executeQuery("select count(*) from user");
        rs.next();
        JOptionPane.showMessageDialog(null, "El numero de usuarios es: " + rs.getInt(1));
        logStatistics();

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.toString());
    } finally {
        try {
            rs.close();
            sentencia.close();
            liberaConexion(conexion);
        } catch (Exception fe) {
            JOptionPane.showMessageDialog(null, fe.toString());
        }
    }
}

From source file:Classes.DBConnection.java

/**
 * Used to get a connection from database init
 * @return a connection to database/*w w  w  . ja  va  2 s  . c  o  m*/
 */
public static Connection getConnection() {
    Connection connection = null;

    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.toString());
    }

    return connection;
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute an INSERT or UPDATE statement, that doesn't return any data.
 *
 * @param dbUrl/*from ww  w .j a  va  2s.c o  m*/
 * @param user
 * @param password
 * @param sql
 */
public static void Execute(String dbUrl, String user, String password, String sql) {
    Connection c = null;
    Statement s = null;
    try {
        c = DriverManager.getConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        //_logger.info( "JDBC T: {} @1 jdbc = {}", System.currentTimeMillis(), sql );
        s = c.createStatement();
        //_logger.info( "JDBC T: {} @2 ", System.currentTimeMillis() );
        s.execute(sql);
        //_logger.info( "JDBC T: {} @3 ", System.currentTimeMillis() );

        //STEP 6: Clean-up environment
        //            s.close();
        //            c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        } finally {
            try {
                if (c != null)
                    c.close();
            } catch (SQLException se) {
                logger.error(se.toString(), se);
            }
        }
    }
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute a SQL query that returns data.
 *
 * @param dbUrl/* w  w  w. j  av  a2s .  co m*/
 * @param user
 * @param password
 * @param sql
 * @param cb
 */
public static void ExecuteQuery(String dbUrl, String user, String password, String sql, ResultSetCallback cb) {
    Connection c = null;
    Statement s = null;
    try {

        //            c = DriverManager.getConnection(dbUrl, user, password);
        c = GetConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        s = c.createStatement();
        ResultSet rs = s.executeQuery(sql);

        if (cb != null) {
            cb.onResultSet(rs);
        }

        //STEP 6: Clean-up environment
        rs.close();
        s.close();
        c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        }

        try {
            if (c != null)
                c.close();
        } catch (SQLException se) {
            logger.error(se.toString(), se);
        }

    }
}

From source file:com.boldust.general.LocalDAO.java

public synchronized static void closeConnection() {
    try {//from   w ww . ja  v  a2s  .  c  om
        conn.close();
        conn = null;
        System.out.println("Close Connect DataBase Success!");
    } catch (SQLException e) {
        System.err.println("Close Connect DataBase Fail! Error = " + e.toString());
    }
}

From source file:com.splicemachine.tutorials.model.RULPredictiveModel.java

/**
 * Stored Procedure for Predictions/*from   w  w w .j  a v  a  2  s  .  c o  m*/
 */
public static void predictRUL(String sensorTableName, String resultsTableName, String savedModelPath,
        int loopinterval) {

    try {

        //Initialize variables
        if (sensorTableName == null || sensorTableName.length() == 0)
            sensorTableName = "IOT.SENSOR_AGG_1_VIEW";
        if (resultsTableName == null || resultsTableName.length() == 0)
            resultsTableName = "IOT.PREDICTION_EXT";
        if (savedModelPath == null || savedModelPath.length() == 0)
            savedModelPath = "/tmp";
        if (!savedModelPath.endsWith("/"))
            savedModelPath = savedModelPath + "/";
        savedModelPath += "model/";

        String jdbcUrl = "jdbc:splice://localhost:1527/splicedb;user=splice;password=admin;useSpark=true";
        Connection conn = DriverManager.getConnection(jdbcUrl);

        SparkSession sparkSession = SpliceSpark.getSession();

        //Specify the data for predictions
        Map<String, String> options = new HashMap<String, String>();
        options.put("driver", "com.splicemachine.db.jdbc.ClientDriver");
        options.put("url", jdbcUrl);
        options.put("dbtable", sensorTableName);

        //Load Model to use for predictins
        CrossValidatorModel cvModel = CrossValidatorModel.load(savedModelPath);

        //Keep checking for new data and make predictions
        while (loopinterval > 0) {
            //Sensor data requiring predictions
            Dataset<Row> sensords = sparkSession.read().format("jdbc").options(options).load();

            //prepare data
            sensords = sensords.na().fill(0);

            //make predictions
            Dataset<Row> predictions = cvModel.transform(sensords)
                    .select("ENGINE_TYPE", "UNIT", "TIME", "prediction")
                    .withColumnRenamed("prediction", "PREDICTION");

            //Save predictions
            String fileName = "temp_pred_" + RandomStringUtils.randomAlphabetic(6).toLowerCase();

            predictions.write().mode(SaveMode.Append).csv("/tmp/data_pred/predictions");

            //Mark records for which predictions are made
            PreparedStatement pStmtDel = conn.prepareStatement(
                    "delete  from IOT.TO_PROCESS_SENSOR s where exists (select 1 from IOT.PREDICTIONS_EXT p where p.engine_type = s.engine_type and p.unit= s.unit and p.time=s.time )");
            pStmtDel.execute();
            pStmtDel.close();
        }

    } catch (SQLException sqle) {
        System.out.println("Error  :::::" + sqle.toString());
        LOG.error("Exception in getColumnStatistics", sqle);
        sqle.printStackTrace();
    }

}

From source file:Emporium.Controle.ContrDestinatarioImporta.java

public static int inserir(int idCliente, int idDepartamento, String nome, String cpf_cnpj, String empresa,
        String cep, String endereco, String numero, String complemento, String bairro, String cidade, String uf,
        String email, String celular, String pais, String nomeBD, String tags) {
    Connection conn = Conexao.conectar(nomeBD);
    String sql = "INSERT INTO cliente_destinatario (idCliente, nome, cpf_cnpj, empresa, cep, endereco, numero, complemento, bairro, cidade, uf, email, celular, pais, tags, idDepartamento) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    //System.out.println("inserir Destinatario -----------------\n"+sql+"\n---------------");

    try {//w w w  .  j  a  va2 s .c  o  m
        PreparedStatement valores = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
        valores.setInt(1, idCliente);
        valores.setString(2, FormataString.removeSpecialChars(nome));
        valores.setString(3, cpf_cnpj);
        valores.setString(4, empresa);
        valores.setString(5, cep);
        valores.setString(6, FormataString.removeSpecialChars(endereco));
        valores.setString(7, numero);
        valores.setString(8, complemento);
        valores.setString(9, bairro);
        valores.setString(10, cidade);
        valores.setString(11, uf);
        valores.setString(12, email);
        valores.setString(13, celular);
        valores.setString(14, pais);
        valores.setString(15, tags);
        valores.setInt(16, idDepartamento);
        valores.executeUpdate();
        int autoIncrementKey = 0;
        ResultSet rs = valores.getGeneratedKeys();
        if (rs.next()) {
            autoIncrementKey = rs.getInt(1);
        }
        valores.close();
        return autoIncrementKey;
    } catch (SQLException e) {
        //System.out.println("ERRO > "+e);
        ContrErroLog.inserir("HOITO - ContrPreVendaDest.inserir", "SQLException", sql, e.toString());
        return 0;
    } finally {
        Conexao.desconectar(conn);
    }
}