Example usage for java.sql SQLException SQLException

List of usage examples for java.sql SQLException SQLException

Introduction

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

Prototype

public SQLException(String reason, Throwable cause) 

Source Link

Document

Constructs a SQLException object with a given reason and cause.

Usage

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;// w  w w .  ja  v  a 2s. c  o  m
    Statement stmt = null;
    ResultSet rs = null;
    try {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver).newInstance();
        System.out.println("Connecting to database...");
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
        stmt = conn.createStatement();
        try {
            rs = stmt.executeQuery("Select * from no_table_exisits");
        } catch (SQLException seRs) {
            String exMsg = "Message from MySQL Database";
            String exSqlState = "Exception";
            SQLException mySqlEx = new SQLException(exMsg, exSqlState);
            seRs.setNextException(mySqlEx);
            throw seRs;
        }
    } catch (SQLException se) {
        int count = 1;
        while (se != null) {
            System.out.println("SQLException " + count);
            System.out.println("Code: " + se.getErrorCode());
            System.out.println("SqlState: " + se.getSQLState());
            System.out.println("Error Message: " + se.getMessage());
            se = se.getNextException();
            count++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.sf.ddao.DynamicQuery.java

public int bindParam(PreparedStatement preparedStatement, int idx, Context context) throws SQLException {
    for (Object param : params) {
        try {/*  w w  w.  ja v a  2  s . c  o  m*/
            ParameterHelper.bind(preparedStatement, idx++, param, context);
        } catch (SQLException e) {
            throw new SQLException("Paramter #" + --idx + ":" + param, e);
        }
    }
    return params.size();
}

From source file:org.deshang.content.indexing.util.jdbc.AbstractRowMapper.java

protected String getBlobContent(Blob blob) throws SQLException {

    LOGGER.debug("Enter getBlobContent(Blob)");
    String blobContent = null;//  w  ww.j  a  v  a 2 s  .  co  m
    try {
        InputStream in = blob.getBinaryStream();
        BufferedInputStream bis = new BufferedInputStream(in);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int data = -1;
        while ((data = bis.read()) != -1) {
            baos.write(data);
        }
        blobContent = baos.toString("UTF-8");
    } catch (IOException e) {
        throw new SQLException("Can't read blob conent", e);
    }

    LOGGER.debug("Exit getBlobContent(Blob)");
    return blobContent;
}

From source file:net.orpiske.ssps.common.db.MultiRsHandler.java

@Override
protected T handleRow(ResultSet rs) throws SQLException {
    T dto;/*from  w  w w . j  ava  2 s .c  o m*/

    try {
        dto = clazz.newInstance();
    } catch (InstantiationException e1) {
        throw new SQLException("Unable to instantiate DTO class: " + e1.getMessage(), e1);
    } catch (IllegalAccessException e1) {
        throw new SQLException("Illegal to instantiate DTO class: " + e1.getMessage(), e1);
    }

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            PropertyUtils.setSimpleProperty(dto, javaProperty, value);
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}

From source file:org.mayocat.cms.home.store.jdbi.mapper.HomePageMapper.java

@Override
public HomePage map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
    HomePage homePage = new HomePage();
    homePage.setId((UUID) resultSet.getObject("id"));

    if (!Strings.isNullOrEmpty(resultSet.getString("localization_data"))) {
        ObjectMapper mapper = new ObjectMapper();
        try {//from  www . java  2s  .c  o m
            Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
            Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class);
            for (Map map : data) {
                localizedVersions.put(LocaleUtils.toLocale((String) map.get("locale")),
                        (Map) map.get("entity"));
            }
            homePage.setLocalizedVersions(localizedVersions);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    return homePage;
}

From source file:net.orpiske.ssps.common.db.CountRsHandler.java

@Override
public Integer handle(ResultSet rs) throws SQLException {
    Integer dto = null;/*from   w ww  . j av a 2s. co  m*/

    // No records to handle :O
    if (!rs.next()) {
        return null;
    }

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);

        try {
            if (value instanceof Integer) {
                dto = (Integer) value;
            }

        } catch (Exception e) {
            throw new SQLException("Unable to set/retrieve count value", e);
        }
    }

    return dto;
}

From source file:cn.sel.dvw.SimpleObjectRowMapper.java

@Override
public T mapRow(ResultSet resultSet, int rowNum) throws SQLException {
    T rowObj;/*from  ww  w .  j a  v  a 2 s . c  o m*/
    try {
        rowObj = clazz.getConstructor().newInstance();
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
            | InvocationTargetException e) {
        e.printStackTrace();
        throw new SQLException("Failed to instantiate the result object!", e);
    }
    Field[] fields = clazz.getFields();
    for (Field field : fields) {
        MProperty annotation = field.getAnnotation(MProperty.class);
        String fieldName = field.getName();
        String columnName = null;
        boolean useSetter = false;
        if (annotation != null) {
            columnName = annotation.db_col_name();
            useSetter = annotation.useSetter();
        }
        if (columnName == null || columnName.isEmpty()) {
            columnName = fieldName;
        }
        Object value = resultSet.getObject(columnName);
        if (value != null) {
            try {
                if (useSetter) {
                    PropertyUtils.setSimpleProperty(rowObj, fieldName, value);
                } else {
                    field.setAccessible(true);
                    field.set(rowObj, value);
                }
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
                throw new SQLException(String.format("Failed to set value for property '%s'!", fieldName));
            }
        }
    }
    return rowObj;
}

From source file:org.mayocat.addons.mapper.AddonGroupMapper.java

@Override
public AddonGroup map(int index, ResultSet result, StatementContext context) throws SQLException {
    AddonGroup addonGroup = new AddonGroup();
    addonGroup.setEntityId((UUID) result.getObject("entity_id"));
    addonGroup.setGroup(result.getString("addon_group"));
    addonGroup.setSource(AddonSource.fromJson(result.getString("source")));

    ObjectMapper mapper = new ObjectMapper();

    try {/*w  w  w .  j a v  a 2s.co  m*/
        Map<String, Object> value = mapper.readValue(result.getString("value"),
                new TypeReference<Map<String, Object>>() {
                });
        addonGroup.setValue(value);
    } catch (IOException e) {
        // Failed as a map ? it must be a sequenced addon
        try {
            List<Map<String, Object>> value = mapper.readValue(result.getString("value"),
                    new TypeReference<List<Map<String, Object>>>() {
                    });
            addonGroup.setValue(value);
        } catch (IOException e1) {
            throw new SQLException("Failed to de-serialize value", e);
        }
    }

    try {
        Map<String, Map<String, Object>> model = mapper.readValue(result.getString("model"),
                new TypeReference<Map<String, Object>>() {
                });
        addonGroup.setModel(model);
    } catch (IOException e) {
        throw new SQLException("Failed to de-serialize model", e);
    }

    return addonGroup;
}

From source file:org.mayocat.attachment.store.jdbi.mapper.AttachmentMapper.java

@Override
public Attachment map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException {
    Attachment attachment = new Attachment();
    attachment.setId((UUID) resultSet.getObject("id"));
    attachment.setTitle(resultSet.getString("title"));
    attachment.setDescription(resultSet.getString("description"));
    attachment.setSlug(resultSet.getString("slug"));
    attachment.setExtension(resultSet.getString("extension"));
    attachment.setParentId((UUID) resultSet.getObject("parent_id"));

    ObjectMapper mapper = new ObjectMapper();
    if (!Strings.isNullOrEmpty(resultSet.getString("metadata"))) {
        try {/*w  w  w  .  j a  v  a 2  s .  c  om*/

            Map<String, Map<String, Object>> metadata = mapper.readValue(resultSet.getString("metadata"),
                    new TypeReference<Map<String, Map<String, Object>>>() {
                    });
            attachment.setMetadata(metadata);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    if (MapperUtils.hasColumn("localization_data", resultSet)
            && !Strings.isNullOrEmpty(resultSet.getString("localization_data"))) {
        try {
            Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
            Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class);
            for (Map map : data) {
                localizedVersions.put(LocaleUtils.toLocale((String) map.get("locale")),
                        (Map) map.get("entity"));
            }
            attachment.setLocalizedVersions(localizedVersions);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    return attachment;

}

From source file:org.mayocat.shop.catalog.store.jdbi.mapper.CollectionMapper.java

@Override
public Collection map(int index, ResultSet result, StatementContext statementContext) throws SQLException {
    Collection collection = new Collection((UUID) result.getObject("id"));
    collection.setParentId((UUID) result.getObject("parent_id"));
    collection.setSlug(result.getString("slug"));
    collection.setTitle(result.getString("title"));
    collection.setDescription(result.getString("description"));
    collection.setFeaturedImageId((UUID) result.getObject("featured_image_id"));

    if (MapperUtils.hasColumn("localization_data", result)
            && !Strings.isNullOrEmpty(result.getString("localization_data"))) {
        ObjectMapper mapper = new ObjectMapper();
        try {//w w  w .  ja  va  2s . c  o  m
            Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
            Map[] data = mapper.readValue(result.getString("localization_data"), Map[].class);
            for (Map map : data) {
                localizedVersions.put(LocaleUtils.toLocale((String) map.get("locale")),
                        (Map) map.get("entity"));
            }
            collection.setLocalizedVersions(localizedVersions);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    return collection;
}