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:org.callimachusproject.sql.SqlTupleResult.java

@Override
public List<String> getBindingNames() throws QueryEvaluationException {
    try {//www.j av a2  s .co  m
        int n = md.getColumnCount();
        List<String> names = new ArrayList<>(n);
        for (int col = 1; col <= n; col++) {
            names.add(md.getColumnName(col));
        }
        return names;
    } catch (SQLException e) {
        throw new QueryEvaluationException(e.toString(), e);
    }
}

From source file:org.apache.hadoop.sqoop.util.ResultSetPrinter.java

/**
 * Format the contents of the ResultSet into something that could be printed
 * neatly; the results are appended to the supplied StringBuilder.
 *//*w w  w.  j a v  a  2s . co  m*/
public final void printResultSet(OutputStream os, ResultSet results) throws IOException {
    try {
        StringBuilder sbNames = new StringBuilder();
        int cols = results.getMetaData().getColumnCount();

        int[] colWidths = new int[cols];
        ResultSetMetaData metadata = results.getMetaData();
        for (int i = 1; i < cols + 1; i++) {
            String colName = metadata.getColumnName(i);
            colWidths[i - 1] = Math.min(metadata.getColumnDisplaySize(i), MAX_COL_WIDTH);
            if (colName == null || colName.equals("")) {
                colName = metadata.getColumnLabel(i) + "*";
            }
            printPadded(sbNames, colName, colWidths[i - 1]);
            sbNames.append(COL_SEPARATOR);
        }
        sbNames.append('\n');

        StringBuilder sbPad = new StringBuilder();
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < COL_SEPARATOR.length() + colWidths[i]; j++) {
                sbPad.append('-');
            }
        }
        sbPad.append('\n');

        sendToStream(sbPad, os);
        sendToStream(sbNames, os);
        sendToStream(sbPad, os);

        while (results.next()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < cols + 1; i++) {
                printPadded(sb, results.getString(i), colWidths[i - 1]);
                sb.append(COL_SEPARATOR);
            }
            sb.append('\n');
            sendToStream(sb, os);
        }

        sendToStream(sbPad, os);
    } catch (SQLException sqlException) {
        LOG.error("Error reading from database: " + sqlException.toString());
    }
}

From source file:org.callimachusproject.sql.SqlTupleResult.java

@Override
public synchronized BindingSet next() throws QueryEvaluationException {
    try {//from w  w  w . ja  v a  2 s  .  co  m
        if (next != null)
            return next;
    } finally {
        next = null;
    }
    try {
        if (!rs.next())
            return null;
        int n = md.getColumnCount();
        MapBindingSet map = new MapBindingSet(n);
        for (int col = 1; col <= n; col++) {
            Value value = value(col);
            if (value != null) {
                map.addBinding(md.getColumnName(col), value);
            }
        }
        return map;
    } catch (SQLException e) {
        throw new QueryEvaluationException(e.toString(), e);
    }
}

From source file:ListInput.java

public void appendResultSet(ResultSet results, int index, boolean toTitleCase) {
    textfield.setText("");
    DefaultListModel model = new DefaultListModel();
    try {/*w  ww .  j  a v  a2s.c  o  m*/
        while (results.next()) {
            String str = results.getString(index);
            if (toTitleCase) {
                str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
            }

            model.addElement(str);
        }
    } catch (SQLException ex) {
        System.err.println("appendResultSet: " + ex.toString());
    }
    list.setModel(model);
    if (model.getSize() > 0)
        list.setSelectedIndex(0);
}

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

public void setAutoCommit(boolean flag) {
    try {//from  ww  w .j a v  a 2 s  . c o  m
        conn.setAutoCommit(flag);
    } catch (SQLException sqle) {
        System.err.println(sqle.toString());
    }
}

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

public synchronized void rollback() {

    try {// w w  w.  j a v a  2 s  . com
        conn.rollback();
    } catch (SQLException sqle) {
        System.err.println(sqle.toString());
    }
}

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

public synchronized void commit() throws LocalDAOException {

    try {//ww w  . j ava 2s. c  o  m
        conn.commit();
    } catch (SQLException sqle) {
        System.err.println(sqle.toString());
        throw new LocalDAOException("DAOException commit:", sqle);
    }

}

From source file:com.gdo.project.util.SqlUtils.java

private static void closeStatement(StclContext stclContext, Statement stmt) {
    try {/*  w  w  w.j a v a2  s  . c  o m*/
        if (stmt != null) {
            stmt.close();
        }
    } catch (SQLException e) {
        logWarn(stclContext, e.toString());
    }
}

From source file:edu.ku.brc.specify.datamodel.DataModelObjBase.java

/**
 * Deletes the data object./*  w  w  w.  j  a va  2s  .  c  o  m*/
 * @param doShowError whether to show the an error dialog
 * @return true is ok, false if not
 */
public static boolean delete(final int id, final int tableId, final boolean doShowError) {
    errMsg = null;

    DBTableInfo ti = DBTableIdMgr.getInstance().getInfoById(tableId);
    Connection connection = DBConnection.getInstance().createConnection();
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        int numRecs = stmt
                .executeUpdate("DELETE FROM " + ti.getName() + " WHERE " + ti.getIdColumnName() + " = " + id);
        if (numRecs != 1) {
            // TODO need error message
            return false;
        }

    } catch (SQLException ex) {
        edu.ku.brc.af.core.UsageTracker.incrSQLUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataModelObjBase.class, ex);
        ex.printStackTrace();
        errMsg = ex.toString();

        try {
            connection.rollback();

        } catch (SQLException ex2) {
            edu.ku.brc.af.core.UsageTracker.incrSQLUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataModelObjBase.class, ex2);
            ex.printStackTrace();
        }
        return false;

    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            edu.ku.brc.af.core.UsageTracker.incrSQLUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataModelObjBase.class, ex);
            ex.printStackTrace();
        }
    }
    return true;
}

From source file:org.castor.cpa.persistence.sql.keygen.SequenceAfterKeyGenerator.java

/**
 * @param conn An open connection within the given transaction.
 * @param tableName The table name.//  ww w  .java 2s. c  om
 * @param primKeyName The primary key name.
 * @return A new key.
 * @throws PersistenceException An error occured talking to persistent storage.
 */
public Object generateKey(final Connection conn, final String tableName, final String primKeyName)
        throws PersistenceException {
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
        // prepares the statement
        String sql = _factory.getSequenceAfterSelectString(getSeqName(tableName, primKeyName), tableName);
        stmt = conn.prepareStatement(sql);

        // execute the prepared statement
        rs = stmt.executeQuery();

        // process result set using appropriate handler and return its value
        return _typeHandler.getValue(rs);
    } catch (SQLException e) {
        String msg = Messages.format("persist.keyGenSQL", this.getClass().getName(), e.toString());
        throw new PersistenceException(msg);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            LOG.warn("Problem closing JDBC statement", e);
        }
    }
}