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() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:ca.sqlpower.persistance.CatNap.java

public static void delete(Connection con, String tableName, String where)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    Statement stmt = null;/*from   www . jav a 2 s  .  co m*/
    StringBuffer sql = new StringBuffer();
    try {
        sql.append("DELETE FROM " + tableName);
        sql.append(" WHERE " + where);
        sql.append("\n)");

        stmt = con.createStatement();
        stmt.executeUpdate(sql.toString());
    } catch (SQLException ex) {
        System.err.println("Catnap: Insert failed. Statement was:\n" + sql);
        throw ex;
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException ex) {
            System.err.println(
                    "Catnap: Couldn't close the statement.  Damn.  But at least you won a stack trace:");
            ex.printStackTrace();
        }
    }
}

From source file:edumsg.core.PostgresConnection.java

public static void disconnect(ResultSet rs, PreparedStatement statment, Connection conn, Statement query) {
    if (rs != null) {
        try {/*ww  w  .ja v  a2 s. c o  m*/
            rs.close();
        } catch (SQLException e) {
        }
    }
    if (statment != null) {
        try {
            statment.close();
        } catch (SQLException e) {
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
        }
    }

    if (query != null) {
        try {
            query.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:ca.sqlpower.persistance.CatNap.java

public static void load(Connection con, String tableName, Object loadTo, String where)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    BeanUtils.describe(loadTo);/*w  w  w. j a  va 2 s  .  c o  m*/
    Statement stmt = null;
    StringBuffer sql = new StringBuffer();
    try {
        sql.append("SELECT * FROM " + tableName + " WHERE " + where);
        sql.append("\n");

        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql.toString());
        while (rs.next()) {
            ResultSetMetaData metaData = rs.getMetaData();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                String beanPropertyName = underscoreToCamelCaps(metaData.getColumnName(i).toLowerCase());

                BeanUtils.setProperty(loadTo, beanPropertyName, rs.getObject(i));

            }
        }
    } catch (SQLException ex) {
        System.err.println("Catnap: Insert failed. Statement was:\n" + sql);
        throw ex;
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException ex) {
            System.err.println(
                    "Catnap: Couldn't close the statement.  Damn.  But at least you won a stack trace:");
            ex.printStackTrace();
        }
    }
}

From source file:ca.sqlpower.persistance.CatNap.java

public static void update(Connection con, Object bean, String tableName, String where)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    Statement stmt = null;//from ww  w .  java 2s.  c o m
    StringBuffer sql = new StringBuffer();
    try {
        Map<String, Object> beanProps = BeanUtils.describe(bean);
        sql.append("UPDATE " + tableName + " SET ");
        boolean first = true;
        for (Map.Entry<String, Object> ent : beanProps.entrySet()) {
            if (first) {
                first = false;
            } else {
                sql.append(", ");
            }
            sql.append(camelToUnderscore(ent.getKey()));
            sql.append("=");
            if (ent.getValue() instanceof Boolean) {
                if ((Boolean) ent.getValue()) {
                    sql.append(SQL.quote('Y'));
                } else {
                    sql.append(SQL.quote('N'));
                }
            } else if (ent.getValue() instanceof Number) {
                sql.append(ent.getValue());

            } else if (ent.getValue() instanceof String) {
                sql.append(SQL.quote(ent.getValue().toString()));
            }
            // ignore all other types
        }
        sql.append(" WHERE " + where);
        sql.append("\n)");

        stmt = con.createStatement();
        stmt.executeUpdate(sql.toString());
    } catch (SQLException ex) {
        System.err.println("Catnap: Insert failed. Statement was:\n" + sql);
        throw ex;
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException ex) {
            System.err.println(
                    "Catnap: Couldn't close the statement.  Damn.  But at least you won a stack trace:");
            ex.printStackTrace();
        }
    }
}

From source file:azkaban.project.JdbcProjectLoaderTest.java

private static void clearDB() {
    if (!testDBExists) {
        return;//from  w w  w. ja  va2 s.  c  o m
    }

    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    QueryRunner runner = new QueryRunner();
    try {
        runner.update(connection, "DELETE FROM projects");

    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_events");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_permissions");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_files");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_flows");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_properties");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);
}

From source file:azkaban.project.JdbcProjectLoaderTest.java

@BeforeClass
public static void setupDB() {
    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);/*w  w  w . j  a v a  2 s .  c  o m*/
    testDBExists = true;

    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    CountHandler countHandler = new CountHandler();
    QueryRunner runner = new QueryRunner();
    try {
        runner.query(connection, "SELECT COUNT(1) FROM projects", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_events", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_permissions", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_files", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_flows", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_properties", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);

    clearDB();
}

From source file:ca.sqlpower.persistance.CatNap.java

public static void insert(Connection con, Object bean, String tableName)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    Statement stmt = null;/*from  w  w  w . j  a v a  2s  .  c  o  m*/
    StringBuffer sql = new StringBuffer();
    try {
        Map<String, Object> beanProps = BeanUtils.describe(bean);
        sql.append("INSERT INTO " + tableName + " (");
        boolean first = true;
        for (Map.Entry<String, Object> ent : beanProps.entrySet()) {
            if (first) {
                first = false;
            } else {
                sql.append(", ");
            }
            sql.append(camelToUnderscore(ent.getKey()));
        }

        sql.append("\n) VALUES (");
        first = true;
        for (Map.Entry<String, Object> ent : beanProps.entrySet()) {
            if (first) {
                first = false;
            } else {
                sql.append(", ");
            }
            if (ent.getValue() instanceof Boolean) {
                if ((Boolean) ent.getValue()) {
                    sql.append(SQL.quote('Y'));
                } else {
                    sql.append(SQL.quote('N'));
                }
            } else if (ent.getValue() instanceof Number) {
                sql.append(ent.getValue());

            } else if (ent.getValue() instanceof String) {
                sql.append(SQL.quote(ent.getValue().toString()));
            }
            // ignore all other types
        }
        sql.append("\n)");

        stmt = con.createStatement();
        stmt.executeUpdate(sql.toString());
    } catch (SQLException ex) {
        System.err.println("Catnap: Insert failed. Statement was:\n" + sql);
        throw ex;
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException ex) {
            System.err.println(
                    "Catnap: Couldn't close the statement.  Damn.  But at least you won a stack trace:");
            ex.printStackTrace();
        }
    }
}

From source file:at.alladin.rmbt.controlServer.TestResultDetailResource.java

/**
 * /*from ww w .jav a2s.  c o  m*/
 * @param test
 * @param settings
 * @param conn
 * @return
 * @throws JSONException
 */
public static JSONObject getGeoLocation(Settings sett, Test test, ResourceBundle settings, Connection conn,
        ResourceBundle labels) throws JSONException {
    JSONObject json = new JSONObject();
    // geo-location
    final Field latField = test.getField("geo_lat"); //csv 6
    final Field longField = test.getField("geo_long"); //csv 7
    final Field accuracyField = test.getField("geo_accuracy");
    final Field providerField = test.getField("geo_provider"); //csv 8
    if (!(latField.isNull() || longField.isNull() || accuracyField.isNull())) {
        final double accuracy = accuracyField.doubleValue();
        if (accuracy < Double.parseDouble(sett.getSetting("rmbt_geo_accuracy_detail_limit"))) {
            final StringBuilder geoString = new StringBuilder(
                    Helperfunctions.geoToString(latField.doubleValue(), longField.doubleValue()));

            geoString.append(" (");
            if (!providerField.isNull()) {
                String provider = providerField.toString().toUpperCase(Locale.US);

                switch (provider) {
                case "NETWORK":
                    provider = labels.getString("key_geo_source_network");
                    break;
                case "GPS":
                    provider = labels.getString("key_geo_source_gps");
                    break;
                }

                geoString.append(provider);
                geoString.append(", ");
            }
            geoString.append(String.format(Locale.US, "+/- %.0f m", accuracy));
            geoString.append(")");
            json.put("location", geoString.toString());

            //also try getting the distance during the test
            final Date clientDate = ((TimestampField) test.getField("client_time")).getDate();
            final long clientTime = clientDate.getTime();
            try {
                OpenTestResource.LocationGraph locGraph = new OpenTestResource.LocationGraph(test.getUid(),
                        clientTime, conn);
                if ((locGraph.getTotalDistance() > 0) && locGraph.getTotalDistance() <= Double
                        .parseDouble(sett.getSetting("rmbt_geo_distance_detail_limit"))) {
                    json.put("motion", Math.round(locGraph.getTotalDistance()) + " m");
                }

            } catch (SQLException ex) {
                //cannot happen since the test uid has to exist in here
                ex.printStackTrace();
            }
        }

        // country derived from location
        final Field countryLocationField = test.getField("country_location");
        if (!countryLocationField.isNull()) {
            json.put("country_location", countryLocationField.toString());
        }
    }

    return json;
}

From source file:model.SQLiteModel.java

public static void closeConnection() {
    try {/*  ww w  .jav a  2 s. c o m*/
        c.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Closed database successfully");
}

From source file:edu.ku.brc.specify.dbsupport.cleanuptools.LocalityCleanup.java

/**
 * //from   w  ww.ja  va  2 s  .  c  om
 */
public static void fixGCRCatNums() {
    String connectStr = "jdbc:mysql://localhost/";

    String dbName = "gcrfish_6";

    DBConnection dbc = new DBConnection("root", "root", connectStr + dbName, "com.mysql.jdbc.Driver",
            "org.hibernate.dialect.MySQLDialect", dbName);
    Connection conn = dbc.createConnection();
    BasicSQLUtils.setDBConnection(conn);

    try {
        // Fix Catalog Numbers
        String sql = "SELECT COUNT(*) FROM collectionobject WHERE CatalogNumber LIKE '%.%'";
        System.out.println("CatNum to be fixed: " + BasicSQLUtils.getCountAsInt(sql));

        int fixedCnt = 0;
        PreparedStatement pTxStmt = conn.prepareStatement(
                "UPDATE collectionobject SET CatalogNumber=?,AltCatalogNumber=? WHERE CollectionObjectID = ?");
        sql = "SELECT CatalogNumber, CollectionObjectID FROM collectionobject WHERE CatalogNumber LIKE '%.%'";
        for (Object[] cols : BasicSQLUtils.query(sql)) {
            String oldCatNum = cols[0].toString();
            String newCatNum = "0" + StringUtils.replace(oldCatNum, ".", "");

            pTxStmt.setString(1, newCatNum);
            pTxStmt.setString(2, oldCatNum);
            pTxStmt.setInt(3, (Integer) cols[1]);

            if (pTxStmt.executeUpdate() != 1) {
                System.out.println("Error updating ColObjID: " + cols[1]);
            } else {
                System.out.println("Fixed ColObjID: " + cols[1]);
                fixedCnt++;
            }
        }
        pTxStmt.close();

        System.out.println("Fixed ColObj CatNum: " + fixedCnt);
        conn.close();

    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}