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:BQJDBC.QueryResultTest.BQForwardOnlyResultSetFunctionTest.java

@Test
public void TestResultIndexOutofBound() {
    try {/*  w  w w.ja v a2  s .c  o m*/
        this.logger.debug(BQForwardOnlyResultSetFunctionTest.Result.getBoolean(99));
    } catch (SQLException e) {
        Assert.assertTrue(true);
        this.logger.error("SQLexception" + e.toString());
    }
}

From source file:co.nubetech.hiho.mapreduce.lib.db.apache.DataDrivenDBInputFormat.java

/** {@inheritDoc} */
public List<InputSplit> getSplits(JobContext job) throws IOException {

    int targetNumTasks = job.getConfiguration().getInt(MRJobConfig.NUM_MAPS, 1);
    if (1 == targetNumTasks) {
        // There's no need to run a bounding vals query; just return a split
        // that separates nothing. This can be considerably more optimal for a
        // large table with no index.
        List<InputSplit> singletonSplit = new ArrayList<InputSplit>();
        singletonSplit.add(new DataDrivenDBInputSplit("1=1", "1=1"));
        return singletonSplit;
    }/*from   w w  w .j av  a 2  s.  c om*/

    ResultSet results = null;
    Statement statement = null;
    Connection connection = getConnection();
    try {
        statement = connection.createStatement();

        results = statement.executeQuery(getBoundingValsQuery());
        results.next();

        // Based on the type of the results, use a different mechanism
        // for interpolating split points (i.e., numeric splits, text splits,
        // dates, etc.)
        int sqlDataType = results.getMetaData().getColumnType(1);
        DBSplitter splitter = getSplitter(sqlDataType);
        if (null == splitter) {
            throw new IOException("Unknown SQL data type: " + sqlDataType);
        }

        return splitter.split(job.getConfiguration(), results, getDBConf().getInputOrderBy());
    } catch (SQLException e) {
        throw new IOException(e.getMessage());
    } finally {
        // More-or-less ignore SQL exceptions here, but log in case we need it.
        try {
            if (null != results) {
                results.close();
            }
        } catch (SQLException se) {
            LOG.debug("SQLException closing resultset: " + se.toString());
        }

        try {
            if (null != statement) {
                statement.close();
            }
        } catch (SQLException se) {
            LOG.debug("SQLException closing statement: " + se.toString());
        }

        try {
            connection.commit();
            closeConnection();
        } catch (SQLException se) {
            LOG.debug("SQLException committing split transaction: " + se.toString());
        }
    }
}

From source file:org.apache.hadoop.metrics2.sink.SqlServerSink.java

public void insertMetricValue(long metricRecordID, String metricName, String metricValue) {
    CallableStatement cstmt = null;
    if (metricRecordID < 0 || metricName == null || metricValue == null)
        return;// www.jav  a  2  s .  co  m
    try {
        if (ensureConnection()) {
            cstmt = conn.prepareCall("{call dbo.uspInsertMetricValue(?, ?, ?)}");
            cstmt.setLong(1, metricRecordID);
            cstmt.setNString(2, metricName);
            cstmt.setNString(3, metricValue);
            cstmt.execute();
        }
    } catch (Exception e) {
        if (DEBUG)
            logger.info("Error during insertMetricValue call sproc: " + e.toString());
        flush();
    } finally {
        if (cstmt != null) {
            try {
                cstmt.close();
            } catch (SQLException se) {
                if (DEBUG)
                    logger.info("Error during insertMetricValue close cstmt: " + se.toString());
            }
            /*
             * We don't close the connection here because we are likely to be
             * writing
             * more metric values next and it is more efficient to share the
             * connection.
             */
        }
    }
}

From source file:org.apache.sqoop.manager.sqlserver.SQLServerMultiMapsManualTest.java

public void setUp() {
    super.setUp();
    MSSQLTestUtils utils = new MSSQLTestUtils();
    try {//from ww w.java2s  .com
        utils.createTableFromSQL(MSSQLTestUtils.CREATE_TALBE_LINEITEM);
        utils.populateLineItem();
    } catch (SQLException e) {
        LOG.error("Setup fail with SQLException: " + StringUtils.stringifyException(e));
        fail("Setup fail with SQLException: " + e.toString());
    }

}

From source file:org.apache.hadoop.mapreduce.lib.db.DataDrivenDBInputFormat.java

/** {@inheritDoc} */
public List<InputSplit> getSplits(JobContext job) throws IOException {

    int targetNumTasks = job.getConfiguration().getInt("mapred.map.tasks", 1);
    if (1 == targetNumTasks) {
        // There's no need to run a bounding vals query; just return a split
        // that separates nothing. This can be considerably more optimal for a
        // large table with no index.
        List<InputSplit> singletonSplit = new ArrayList<InputSplit>();
        singletonSplit.add(new DataDrivenDBInputSplit("1=1", "1=1"));
        return singletonSplit;
    }/*from   www .j av  a 2 s . c  om*/

    ResultSet results = null;
    Statement statement = null;
    Connection connection = getConnection();
    try {
        statement = connection.createStatement();

        results = statement.executeQuery(getBoundingValsQuery());
        results.next();

        // Based on the type of the results, use a different mechanism
        // for interpolating split points (i.e., numeric splits, text splits,
        // dates, etc.)
        int sqlDataType = results.getMetaData().getColumnType(1);
        DBSplitter splitter = getSplitter(sqlDataType);
        if (null == splitter) {
            throw new IOException("Unknown SQL data type: " + sqlDataType);
        }

        return splitter.split(job.getConfiguration(), results, getDBConf().getInputOrderBy());
    } catch (SQLException e) {
        throw new IOException(e.getMessage());
    } finally {
        // More-or-less ignore SQL exceptions here, but log in case we need it.
        try {
            if (null != results) {
                results.close();
            }
        } catch (SQLException se) {
            LOG.debug("SQLException closing resultset: " + se.toString());
        }

        try {
            if (null != statement) {
                statement.close();
            }
        } catch (SQLException se) {
            LOG.debug("SQLException closing statement: " + se.toString());
        }

        try {
            connection.commit();
            closeConnection();
        } catch (SQLException se) {
            LOG.debug("SQLException committing split transaction: " + se.toString());
        }
    }
}

From source file:fi.helsinki.lib.simplerest.ItemsResource.java

@Get("html|xhtml|xml")
public Representation toXml() {
    Collection collection = null;
    DomRepresentation representation = null;
    Document d = null;/*  w  w w . j  a va  2 s . com*/
    try {
        collection = Collection.find(context, this.collectionId);
        if (collection == null) {
            return errorNotFound(context, "Could not find the collection.");
        }

        representation = new DomRepresentation(MediaType.TEXT_HTML);
        d = representation.getDocument();
    } catch (SQLException e) {
        return errorInternal(context, e.toString());
    } catch (IOException e) {
        return errorInternal(context, e.toString());
    }

    Element html = d.createElement("html");
    d.appendChild(html);

    Element head = d.createElement("head");
    html.appendChild(head);

    Element title = d.createElement("title");
    head.appendChild(title);
    title.appendChild(d.createTextNode("Items for collection " + collection.getName()));

    Element body = d.createElement("body");
    html.appendChild(body);

    Element ulItems = d.createElement("ul");
    setId(ulItems, "items");
    body.appendChild(ulItems);

    String base = baseUrl();
    try {
        ItemIterator ii = collection.getItems();
        while (ii.hasNext()) {
            Item item = ii.next();
            Element li = d.createElement("li");
            Element a = d.createElement("a");
            String name = item.getName();
            if (name == null) {
                // FIXME: Should we really give names for items with no
                // FIXME: name? (And if so does "Untitled" make sense?)
                // FIXME: Anyway, this would break with null values.
                name = "Untitled";
            }
            a.appendChild(d.createTextNode(name));
            String href = base + ItemResource.relativeUrl(item.getID());
            setAttribute(a, "href", href);
            li.appendChild(a);
            ulItems.appendChild(li);
        }
    } catch (SQLException e) {
        String errMsg = "SQLException while trying to items of the collection. " + e.getMessage();
        return errorInternal(context, errMsg);
    }

    Element form = d.createElement("form");
    form.setAttribute("enctype", "multipart/form-data");
    form.setAttribute("method", "post");
    makeInputRow(d, form, "title", "Title");
    makeInputRow(d, form, "lang", "Language");

    Element submitButton = d.createElement("input");
    submitButton.setAttribute("type", "submit");
    submitButton.setAttribute("value", "Create a new item");
    form.appendChild(submitButton);

    body.appendChild(form);

    try {
        if (context != null) {
            context.complete();
        }
    } catch (SQLException e) {
        log.log(Priority.INFO, e);
    }

    return representation;
}

From source file:org.apache.hadoop.sqoop.testutil.ImportJobTestCase.java

/**
 * verify that the single-column single-row result can be read back from the db.
 *
 *//*from w w w.  j  ava  2  s  .com*/
protected void verifyReadback(int colNum, String expectedVal) {
    try {
        ResultSet results = getManager().readTable(getTableName(), getColNames());
        assertNotNull("Null results from readTable()!", results);
        assertTrue("Expected at least one row returned", results.next());
        String resultVal = results.getString(colNum);
        if (null != expectedVal) {
            assertNotNull("Expected non-null result value", resultVal);
        }

        assertEquals("Error reading inserted value back from db", expectedVal, resultVal);
        assertFalse("Expected at most one row returned", results.next());
        results.close();
    } catch (SQLException sqlE) {
        fail("Got SQLException: " + sqlE.toString());
    }
}

From source file:org.apache.sqoop.manager.sqlserver.SQLServerMultiMapsManualTest.java

public void tearDown() {
    super.tearDown();
    MSSQLTestUtils utils = new MSSQLTestUtils();
    try {//  ww  w.j  a v  a2 s . c  o  m
        utils.dropTableIfExists("TPCH1M_LINEITEM");
    } catch (SQLException e) {
        LOG.error("TeatDown fail with SQLException: " + StringUtils.stringifyException(e));
        fail("TearDown fail with SQLException: " + e.toString());
    }
}

From source file:BQJDBC.QueryResultTest.Timeouttest.java

@Test
public void QueryResultTest03() {
    final String sql = "SELECT COUNT(DISTINCT web100_log_entry.connection_spec.remote_ip) AS num_clients FROM [guid754187384106:m_lab.2010_01] "
            + "WHERE IS_EXPLICITLY_DEFINED(web100_log_entry.connection_spec.remote_ip) AND IS_EXPLICITLY_DEFINED(web100_log_entry.log_time) "
            + "AND web100_log_entry.log_time > 1262304000 AND web100_log_entry.log_time < 1262476800";
    final String description = "A sample query from google, but we don't have Access for the query table #ERROR #accessDenied #403";

    this.logger.info("Test number: 03");
    this.logger.info("Running query:" + sql);
    this.logger.debug(description);
    try {/*ww w .j  a v  a  2s .  c  om*/
        Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.debug("SQLexception" + e.toString());
        // fail("SQLException" + e.toString());
        Assert.assertTrue(e.getCause().toString()
                .contains("Access Denied: Table measurement-lab:m_lab.2010_01: QUERY_TABLE"));
    }
}

From source file:org.fabrican.extension.variable.provider.servlet.ManagementServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {/*from w w w  . j av a  2 s  . c o m*/
        Pair<String, String> params = gePathParams(req);
        String cmd = params.left();
        String name = params.right();
        RuleSetDAO dao = RuleSetDAO.getInstance();
        resp.setHeader("Cache-Control", "no-cache");
        switch (cmd) {
        case "config":
            InputStreamReader reader = new InputStreamReader(req.getInputStream());
            JSONObject jo = new JSONObject(new JSONTokener(reader));
            String driver = null, url = null, user = null, pass = null;
            if (jo.has("driver")) {
                driver = jo.getString("driver");
            }
            if (jo.has("url")) {
                url = jo.getString("url");
            }
            if (jo.has("user")) {
                user = jo.getString("user");
            }
            if (jo.has("pass")) {
                pass = jo.getString("pass");
            }
            if (driver == null || url == null) {
                resp.setStatus(400);
                output(resp, "missing paramter \"driver\" or \"url\"");
            } else {
                try {
                    if (dao.resetConnection(driver, url, user, pass)) {
                        resp.setStatus(200);
                    } else {
                        resp.setStatus(400);
                        output(resp, "config failed");
                    }
                } catch (Exception ex) {
                    resp.setStatus(500);
                    output(resp, ex.toString());
                }
            }
            return;
        case "providers":
            name = name.trim();
            if (name.equals("")) { // add
                RuleSet r = RuleSetJSONHelper.parse(req.getInputStream());
                if (dao.addRuleSet(r)) {
                    resp.setStatus(201);
                } else {
                    resp.setStatus(400);
                    output(resp, "add failed.");
                }
            } else {
                RuleSet r = RuleSetJSONHelper.parse(req.getInputStream());
                if (dao.updateRuleSet(r)) {
                    resp.setStatus(200);
                } else {
                    resp.setStatus(409);
                    output(resp, "update failed.");
                }
            }
            return;
        case "command":
            name = name.trim();
            if (name.equals("copy")) {
                reader = new InputStreamReader(req.getInputStream());
                jo = new JSONObject(new JSONTokener(reader));
                String from = null;
                String to = null;
                if (jo.has("copyFrom")) {
                    from = jo.getString("copyFrom");
                }
                if (jo.has("copyTo")) {
                    to = jo.getString("copyTo");
                }
                if (from == null || to == null || from.equals(to)) {
                    resp.setStatus(400);
                    output(resp, "parameter error");
                    return;
                }
                RuleSet rFrom = dao.getRuleSetByName(from);
                if (rFrom == null) {
                    resp.setStatus(400);
                    output(resp, "rule not found");
                    return;
                } else {
                    rFrom.setName(to);
                    if (dao.addRuleSet(rFrom)) {
                        resp.setStatus(201);
                    } else {
                        resp.setStatus(400);
                        output(resp, "rule not added, " + to);
                    }
                }
                return;
            } else if (name.equals("destroyCache")) {
                dao.destroyCache();
                resp.setStatus(201);
                return;
            }
        }
    } catch (JSONException ex) {
        resp.setStatus(400);
        output(resp, ex.toString());
    } catch (SQLException e) {
        resp.setStatus(500);
        output(resp, e.toString());
    } catch (InterruptedException e) {
        resp.setStatus(500);
        output(resp, e.toString());
    }
    resp.setStatus(400);
    output(resp, "unrecognized request");
}