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.Timeouttest.java

@Test
public void QueryResultTest09() {
    final String sql = "SELECT corpus, corpus_date FROM publicdata:samples.shakespeare GROUP BY corpus, corpus_date ORDER BY corpus_date DESC LIMIT 3;";
    final String description = "Shakespeare's 3 latest";
    String[][] expectation = new String[][] { { "kinghenryviii", "tempest", "winterstale" },
            { "1612", "1611", "1610" } };

    this.logger.info("Test number: 09");
    this.logger.info("Running query:" + sql);

    java.sql.ResultSet Result = null;
    try {/*ww w.j a v a 2s . com*/
        Result = Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail("SQLException" + e.toString());
    }
    Assert.assertNotNull(Result);

    this.logger.debug(description);

    HelperFunctions.printer(expectation);
    try {
        Assert.assertTrue("Comparing failed in the String[][] array",
                this.comparer(expectation, BQSupportMethods.GetQueryResult(Result)));
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail(e.toString());
    }
}

From source file:BQJDBC.QueryResultTest.Timeouttest.java

@Test
public void QueryResultTest04() {
    final String sql = "SELECT corpus FROM publicdata:samples.shakespeare WHERE LOWER(word)=\"lord\" GROUP BY corpus ORDER BY corpus DESC LIMIT 5;";
    final String description = "A query which gets 5 of Shakespeare were the word lord is present";
    String[][] expectation = new String[][] {
            { "winterstale", "various", "twogentlemenofverona", "twelfthnight", "troilusandcressida" } };

    this.logger.info("Test number: 04");
    this.logger.info("Running query:" + sql);

    java.sql.ResultSet Result = null;
    try {/*from  w ww .j av a2  s . c  o  m*/
        Result = Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail("SQLException" + e.toString());
    }
    Assert.assertNotNull(Result);

    this.logger.debug(description);

    HelperFunctions.printer(expectation);

    try {
        Assert.assertTrue("Comparing failed in the String[][] array",
                this.comparer(expectation, BQSupportMethods.GetQueryResult(Result)));
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail(e.toString());
    }
}

From source file:BQJDBC.QueryResultTest.Timeouttest.java

@Test
public void QueryResultTest06() {
    final String sql = "SELECT corpus_date,SUM(word_count) FROM publicdata:samples.shakespeare GROUP BY corpus_date ORDER BY corpus_date DESC LIMIT 5;";
    final String description = "A query which gets how many words Shapespeare wrote in a year (5 years displayed descending)";
    String[][] expectation = new String[][] { { "1612", "1611", "1610", "1609", "1608" },
            { "26265", "17593", "26181", "57073", "19846" } };

    this.logger.info("Test number: 06");
    this.logger.info("Running query:" + sql);

    java.sql.ResultSet Result = null;
    try {/*  w w  w.j a  v a2  s  .c o  m*/
        Result = Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail("SQLException" + e.toString());
    }
    Assert.assertNotNull(Result);

    this.logger.debug(description);

    HelperFunctions.printer(expectation);

    try {
        Assert.assertTrue("Comparing failed in the String[][] array",
                this.comparer(expectation, BQSupportMethods.GetQueryResult(Result)));
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail(e.toString());
    }
}

From source file:org.apache.hadoop.sqoop.manager.SqlManager.java

/**
 * Poor man's SQL query interface; used for debugging.
 * @param s//from  w w  w.  jav  a 2s  .  c  om
 */
public void execAndPrint(String s) {
    System.out.println("Executing statement: " + s);
    ResultSet results = execute(s);
    if (null == results) {
        LOG.info("Got null results back!");
        return;
    }

    try {
        int cols = results.getMetaData().getColumnCount();
        System.out.println("Got " + cols + " columns back");
        if (cols > 0) {
            System.out.println("Schema: " + results.getMetaData().getSchemaName(1));
            System.out.println("Table: " + results.getMetaData().getTableName(1));
        }
    } catch (SQLException sqlE) {
        LOG.error("SQLException reading result metadata: " + sqlE.toString());
    }

    try {
        new ResultSetPrinter().printResultSet(System.out, results);
    } catch (IOException ioe) {
        LOG.error("IOException writing results to stdout: " + ioe.toString());
        return;
    }
}

From source file:BQJDBC.QueryResultTest.Timeouttest.java

@Test
public void QueryResultTest07() {
    final String sql = "SELECT corpus, SUM(word_count) as w_c FROM publicdata:samples.shakespeare GROUP BY corpus HAVING w_c > 20000 ORDER BY w_c ASC LIMIT 5;";
    final String description = "A query which gets Shakespeare were there are more words then 20000 (only 5 is displayed ascending)";
    String[][] expectation = new String[][] {
            { "juliuscaesar", "twelfthnight", "titusandronicus", "kingjohn", "tamingoftheshrew" },
            { "21052", "21633", "21911", "21983", "22358" } };

    this.logger.info("Test number: 07");
    this.logger.info("Running query:" + sql);

    java.sql.ResultSet Result = null;
    try {/*from w  w  w. j  a v a  2s .  c  o  m*/
        Result = Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail("SQLException" + e.toString());
    }
    Assert.assertNotNull(Result);

    this.logger.debug(description);

    HelperFunctions.printer(expectation);

    try {
        Assert.assertTrue("Comparing failed in the String[][] array",
                this.comparer(expectation, BQSupportMethods.GetQueryResult(Result)));
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail(e.toString());
    }
}

From source file:BQJDBC.QueryResultTest.Timeouttest.java

@Test
public void QueryResultTest08() {
    final String sql = "SELECT corpus, MAX(word_count) as m, word FROM publicdata:samples.shakespeare GROUP BY corpus,word ORDER BY m DESC LIMIT 5;";
    final String description = "A query which gets those Shakespeare with the most common word ordered by count descending (only 5 is displayed)";
    String[][] expectation = new String[][] {
            { "hamlet", "coriolanus", "kinghenryv", "2kinghenryiv", "kingrichardiii" },
            { "995", "942", "937", "894", "848" }, { "the", "the", "the", "the", "the" } };

    this.logger.info("Test number: 08");
    this.logger.info("Running query:" + sql);

    java.sql.ResultSet Result = null;
    try {/*from   ww w.j a v  a  2 s .co m*/
        Result = Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail("SQLException" + e.toString());
    }
    Assert.assertNotNull(Result);

    this.logger.debug(description);

    HelperFunctions.printer(expectation);

    try {
        Assert.assertTrue("Comparing failed in the String[][] array",
                this.comparer(expectation, BQSupportMethods.GetQueryResult(Result)));
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail(e.toString());
    }
}

From source file:org.apache.hadoop.sqoop.manager.SqlManager.java

@Override
public Map<String, Integer> getColumnTypes(String tableName) {
    String stmt = "SELECT t.* FROM " + tableName + " AS t WHERE 1 = 1";

    ResultSet results = execute(stmt);
    if (null == results) {
        return null;
    }/*from   w  w w  . j  a v  a2s  . com*/

    try {
        Map<String, Integer> colTypes = new HashMap<String, Integer>();

        int cols = results.getMetaData().getColumnCount();
        ResultSetMetaData metadata = results.getMetaData();
        for (int i = 1; i < cols + 1; i++) {
            int typeId = metadata.getColumnType(i);
            String colName = metadata.getColumnName(i);
            if (colName == null || colName.equals("")) {
                colName = metadata.getColumnLabel(i);
            }

            colTypes.put(colName, Integer.valueOf(typeId));
        }

        return colTypes;
    } catch (SQLException sqlException) {
        LOG.error("Error reading from database: " + sqlException.toString());
        return null;
    }
}

From source file:org.apache.hadoop.sqoop.manager.SqlManager.java

/**
 * executes an arbitrary SQL statement//from w w  w  .  j ava 2 s  . c o  m
 * @param stmt The SQL statement to execute
 * @return A ResultSet encapsulating the results or null on error
 */
protected ResultSet execute(String stmt, Object... args) {
    if (null == stmt) {
        LOG.error("Null statement sent to SqlManager.execute()");
        return null;
    }

    PreparedStatement statement = null;
    try {
        statement = this.getConnection().prepareStatement(stmt, ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY);
        if (null != args) {
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
        }

        LOG.info("Executing SQL statement: " + stmt);
        return statement.executeQuery();
    } catch (SQLException sqlException) {
        LOG.error("Error returned by SQL database: " + sqlException.toString());
        return null;
    }

    // TODO(aaron): Is calling ResultSet.close() sufficient?
    // Or must statement.close() be called too?
}

From source file:org.apache.hadoop.sqoop.manager.SqlManager.java

@Override
public String[] listTables() {
    ResultSet results = null;//from www. j  av a2s . com
    String[] tableTypes = { "TABLE" };
    try {
        DatabaseMetaData metaData = this.getConnection().getMetaData();
        results = metaData.getTables(null, null, null, tableTypes);
    } catch (SQLException sqlException) {
        LOG.error("Error reading database metadata: " + sqlException.toString());
        return null;
    }

    if (null == results) {
        return null;
    }

    try {
        ArrayList<String> tables = new ArrayList<String>();
        while (results.next()) {
            String tableName = results.getString("TABLE_NAME");
            tables.add(tableName);
        }

        return tables.toArray(new String[0]);
    } catch (SQLException sqlException) {
        LOG.error("Error reading from database: " + sqlException.toString());
        return null;
    }
}

From source file:BQJDBC.QueryResultTest.Timeouttest.java

@Test
public void QueryResultTest01() {
    final String sql = "SELECT TOP(word, 10), COUNT(*) FROM publicdata:samples.shakespeare";
    final String description = "The top 10 word from shakespeare #TOP #COUNT";
    String[][] expectation = new String[][] {
            { "you", "yet", "would", "world", "without", "with", "your", "young", "words", "word" },
            { "42", "42", "42", "42", "42", "42", "41", "41", "41", "41" } };
    /** somehow the result changed with time
    { "you", "yet", "would", "world", "without", "with", "will",
            "why", "whose", "whom" },/* w w w  . ja v  a2 s.c  o  m*/
    { "42", "42", "42", "42", "42", "42", "42", "42", "42", "42" } };
    */

    this.logger.info("Test number: 01");
    this.logger.info("Running query:" + sql);

    java.sql.ResultSet Result = null;
    try {
        Result = Timeouttest.con.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail("SQLException" + e.toString());
    }
    Assert.assertNotNull(Result);

    this.logger.debug(description);

    HelperFunctions.printer(expectation);

    try {
        Assert.assertTrue("Comparing failed in the String[][] array",
                this.comparer(expectation, BQSupportMethods.GetQueryResult(Result)));
    } catch (SQLException e) {
        this.logger.error("SQLexception" + e.toString());
        Assert.fail(e.toString());
    }
}