Example usage for java.sql SQLException printStackTrace

List of usage examples for java.sql SQLException printStackTrace

Introduction

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

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:net.sf.hajdbc.codec.base64.Base64CodecFactory.java

public static void main(String... args) {
    if (args.length != 2) {
        System.err.println(String.format("Usage:%s\tjava %s <cluster-id> <password-to-encrypt>",
                Strings.NEW_LINE, Base64CodecFactory.class.getName()));
        System.exit(1);//from   ww  w .j a va2s  . c o m
        return;
    }

    String clusterId = args[0];
    String value = args[1];

    try {
        Codec codec = new Base64CodecFactory().createCodec(clusterId);

        System.out.println(codec.encode(value));
    } catch (SQLException e) {
        e.printStackTrace(System.err);
    }
}

From source file:net.sf.hajdbc.codec.hex.HexCodecFactory.java

public static void main(String... args) {
    if (args.length != 2) {
        System.err.println(String.format("Usage:%s\tjava %s <cluster-id> <password-to-encrypt>",
                Strings.NEW_LINE, HexCodecFactory.class.getName()));
        System.exit(1);//from   w w  w  .j  a v a2 s  .co  m
        return;
    }

    String clusterId = args[0];
    String value = args[1];

    try {
        Codec codec = new HexCodecFactory().createCodec(clusterId);

        System.out.println(codec.encode(value));
    } catch (SQLException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

/**
 * Print the stack trace for a SQLException to a 
 * specified PrintWriter. //from  w  ww .  j  a va  2s. com
 *
 * @param e SQLException to print stack trace of
 * @param pw PrintWriter to print to
 */
public static void printStackTrace(SQLException e, PrintWriter pw) {

    SQLException next = e;
    while (next != null) {
        next.printStackTrace(pw);
        next = next.getNextException();
        if (next != null) {
            pw.println("Next SQLException:");
        }
    }
}

From source file:it.itis.pertini.falessi.tunes.services.AbstractService.java

protected static ErrorMessage toErrorMessage(SQLException e) {
    ErrorMessage errorMessage = new ErrorMessage();
    errorMessage.setErrorCode(e.getErrorCode());
    errorMessage.setSqlState(e.getSQLState());

    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    errorMessage.setStackTrace(stringWriter.toString());

    return errorMessage;
}

From source file:morphy.utils.john.DatabaseConnection.java

public String[] getArray(java.sql.ResultSet r, int columnIndex) {
    try {//from ww w . j  a va  2  s.  c om
        if (columnIndex == 0)
            columnIndex = 1;

        java.util.List<String> arr = new java.util.ArrayList<String>();
        while (r.next()) {
            arr.add(r.getString(columnIndex));
        }
        return arr.toArray(new String[arr.size()]);
    } catch (SQLException e) {
        e.printStackTrace(System.err);
    }
    return null;
}

From source file:morphy.utils.john.DatabaseConnection.java

/**
 * Shorthand for getConnection().getStatement().execute(query).
 * You can NOT retrieve ResultSets using this method, so they can only be INSERTs, etc.
 *//*from  w ww .  ja  v  a2s  .  co  m*/
public boolean executeQuery(String query) {
    try {
        if (LOG.isInfoEnabled()) {
            LOG.info("Executed query: " + query);
        }
        getConnection().createStatement().execute(query);
        return true;
    } catch (SQLException se) {
        if (LOG.isErrorEnabled()) {
            LOG.error(se);
            se.printStackTrace(System.err);
        }
        return false;
    }
}

From source file:RawSQLServlet.java

/** Do the SQL query */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String query = request.getParameter("sql");

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    if (query == null) {
        out.println("<b>Error: malformed query, contact administrator</b>");
        return;//from w ww .j a v  a 2  s  .  co m
    }

    // NB MUST also check for admin privs before proceding!

    try { // SQL
        out.println("<p>Your query: <b>" + query + "</b></p>");
        stmt.execute(query);
        ResultSet rs = stmt.getResultSet();
        if (rs == null) {
            // print updatecount
            out.println("<p>Result: updateCount = <b>" + stmt.getUpdateCount() + "</p>");
        } else {
            // process resultset

            out.println("<br>Your response:");

            ResultSetMetaData md = rs.getMetaData();
            int count = md.getColumnCount();
            out.println("<table border=1>");
            out.print("<tr>");
            for (int i = 1; i <= count; i++) {
                out.print("<th>");
                out.print(md.getColumnName(i));
            }
            out.println("</tr>");
            while (rs.next()) {
                out.print("<tr>");
                for (int i = 1; i <= count; i++) {
                    out.print("<td>");
                    out.print(rs.getString(i));
                }
                out.println("</tr>");
            }
        }
        out.println("</table>");
        // rs.close();
    } catch (SQLException ex) {
        out.print("<B>" + getClass() + ": SQL Error:</B>\n" + ex);
        out.print("<pre>");
        ex.printStackTrace(out);
        out.print("</pre>");
    }
}

From source file:cz.lbenda.dataman.db.DatamanDataSource.java

private Connection createConnection(String username, String password) throws SQLException {
    DatamanConnection connection = connections.get(dbConfig);
    if (connection != null && !connection.isClosed()) {
        scheduleUnconnect(connection);//www. j av a 2s . c om
        return connection;
    }
    Properties connectionProps = new Properties();
    if (!StringUtils.isEmpty(username)) {
        connectionProps.put("user", username);
    }
    if (!StringUtils.isEmpty(password)) {
        connectionProps.put("password", password);
    }
    Driver driver = getDriver(dbConfig);
    try {
        connection = new DatamanConnection(
                driver.connect(dbConfig.getJdbcConfiguration().getUrl(), connectionProps));
        connection.setConnectionTimeout(dbConfig.getConnectionTimeout());
        connections.put(dbConfig, connection);
        scheduleUnconnect(connection);
        return connection;
    } catch (SQLException e) {
        getLogWriter().print("Filed to create connection");
        e.printStackTrace(getLogWriter());
        onException(e);
        throw e;
    }
}

From source file:morphy.command.ClearmessagesCommand.java

public void process(String arguments, UserSession userSession) {
    /*  clearmessages */*from  w ww .ja v  a 2s .  c o  m*/
      will delete all of your messages.
       clearmessages 2
      will delete your message #2.
       clearmessages DAV
      will delete all of your messages from DAV.
       clearmessages 4-7
      will delete your messages 4 through 7.
     */
    /*   clearmessage 38-40
       Messages 38-40 cleared.
    */
    /*   clearmessages 37-40
       You have no messages 37-40.
     */
    /*   You have 36 messages (1 unread).
       Use "messages u" to view unread messages and "clearmessages *" to clear all.
    */

    arguments = arguments.trim();
    if (arguments.equals("")) {
        userSession.send(getContext().getUsage());
        return;
    } else {
        if (!UserService.getInstance().isRegistered(userSession.getUser().getUserName())) {
            userSession.send("Only registered players can use the clearmessages command.");
            return;
        }

        int numMessages = 0;
        String query = "SELECT COUNT(*) FROM `messages` WHERE `to_user_id` = '"
                + userSession.getUser().getDBID() + "';";
        ResultSet rs = DatabaseConnectionService.getInstance().getDBConnection().executeQueryWithRS(query);
        try {
            if (rs.next()) {
                numMessages = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }

        if (numMessages == 0) {
            userSession.send("You have no messages.");
            return;
        }

        if (arguments.equals("*")) {
            // delete all messages

            query = "DELETE FROM `messages` WHERE `to_user_id` = '" + userSession.getUser().getDBID() + "'";
            boolean executed = DatabaseConnectionService.getInstance().getDBConnection().executeQuery(query);
            if (executed) {
                userSession.send("Messages cleared.");
                return;
            }
        }

        if (StringUtils.isNumeric(arguments)) {
            // delete this message

            arguments += "-" + arguments;
            //return;
        }

        if (StringUtils.isAlpha(arguments)) {
            // delete all messages from this user

            int id = UserService.getInstance().getDBID(arguments);
            if (id == 0) { /* something failed */
                userSession.send("There is no player matching the name " + arguments + ".");
                return;
            } else {
                /*   clearmessages outrunyou
                   You have no messages from OUTRUNYOU.
                */

                String username = UserService.getInstance().correctCapsUsername(arguments);

                query = "SELECT `id` FROM `messages` WHERE `from_user_id` = '" + id + "' ORDER BY `id` ASC";
                rs = DatabaseConnectionService.getInstance().getDBConnection().executeQueryWithRS(query);
                try {
                    List<Integer> ids = new ArrayList<Integer>();
                    while (rs.next()) {
                        ids.add(new Integer(rs.getInt(1)));
                    }
                    if (ids.size() == 0) {
                        userSession.send("You have no messages from " + username + ".");
                        return;
                    } else {
                        query = "DELETE FROM `messages` WHERE "
                                + MessagesCommand.formatIdListForQuery("id", ids);
                        boolean executed = DatabaseConnectionService.getInstance().getDBConnection()
                                .executeQuery(query);
                        if (executed) {
                            userSession.send("Messages from " + username + " cleared.");
                            return;
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace(System.err);
                }
            }
            return;
        }

        if (arguments.matches("[0-9]+\\-[0-9]+")) {
            // delete this range of messages
            List<Integer> list = MessagesCommand.expandRange(arguments);
            java.util.Collections.sort(list);
            query = "SELECT m.`id`,u1.`username`,`message`,`timestamp`,`read` "
                    + "FROM `messages` m INNER JOIN `users` u1 ON (u1.`id` = m.`from_user_id`) "
                    + "WHERE m.to_user_id = '" + userSession.getUser().getDBID() + "'" + "ORDER BY m.`id` ASC";
            rs = DatabaseConnectionService.getInstance().getDBConnection().executeQueryWithRS(query);
            try {
                // the "ids" variable contains the actual message ids as stored in the database
                // and NOT the psuedo message number as the user thinks.
                List<Integer> ids = new ArrayList<Integer>();
                List<Integer> rownums = new ArrayList<Integer>();
                int rownum = 0;
                while (rs.next()) {
                    rownum++;
                    if (list.contains(rownum)) {
                        ids.add(rs.getInt(1));
                        rownums.add(rownum);
                    }
                }
                if (ids.size() > 0) {
                    query = "DELETE FROM `messages` WHERE " + MessagesCommand.formatIdListForQuery("id", ids);
                    boolean executed = DatabaseConnectionService.getInstance().getDBConnection()
                            .executeQuery(query);
                    if (executed) {
                        userSession.send((rownums.size() == 1 ? "Message" : "Messages") + " "
                                + rownums.toString() + " cleared.");
                        return;
                    }
                } else {
                    userSession.send("You have no message" + (rownums.size() > 1 ? "s" : "") + " "
                            + rownums.toString() + ".");
                    return;
                }
            } catch (SQLException e) {
                e.printStackTrace(System.err);
            }

            return;
        }

        // if we've reached this point, nothing has been deleted, so invalid arguments.
        userSession.send(getContext().getUsage());
        return;
    }
}

From source file:com.alibaba.wasp.jdbcx.JdbcConnectionPool.java

private void closeConnection(PooledConnection pc) {
    try {/* ww w.ja v  a2s .c  om*/
        pc.close();
    } catch (SQLException e) {
        if (logWriter != null) {
            e.printStackTrace(logWriter);
        }
    }
}