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

Source Link

Document

Constructs a SQLException object with a given cause.

Usage

From source file:ru.org.linux.user.UserDao.java

/**
 *    //  w  w  w . j a va 2  s  . c o  m
 * @param user 
 * @return ?   :-)
 */
public BanInfo getBanInfoClass(User user) {
    List<BanInfo> infoList = jdbcTemplate.query(queryBanInfoClass, new RowMapper<BanInfo>() {
        @Override
        public BanInfo mapRow(ResultSet resultSet, int i) throws SQLException {
            Timestamp date = resultSet.getTimestamp("bandate");
            String reason = resultSet.getString("reason");
            User moderator;
            try {
                moderator = getUser(resultSet.getInt("ban_by"));
            } catch (UserNotFoundException exception) {
                throw new SQLException(exception.getMessage());
            }
            return new BanInfo(date, reason, moderator);
        }
    }, user.getId());

    if (infoList.isEmpty()) {
        return null;
    } else {
        return infoList.get(0);
    }
}

From source file:com.sop4j.dbutils.QueryRunner.java

/**
 * Creates an {@link QueryExecutor} for the given SQL statement and connection.
 *
 * @param conn The connection to use for the query call.
 * @param closeConn True if the connection should be closed, false otherwise.
 * @param sql The SQL statement to execute.
 *
 * @return An {@link QueryExecutor} for this SQL statement.
 * @throws SQLException If there are database or parameter errors.
 *///from   ww  w. java2s.  c  o m
public QueryExecutor query(Connection conn, boolean closeConn, String sql) throws SQLException {
    if (conn == null) {
        throw new SQLException("Null connection");
    }

    if (sql == null) {
        if (closeConn) {
            close(conn);
        }
        throw new SQLException("Null SQL statement");
    }

    return new QueryExecutor(conn, sql, closeConn);
}

From source file:com.orientechnologies.orient.jdbc.H2.java

public static Collection<String> translate(String sql) throws SQLException {
    List<String> sections = new ArrayList<String>();
    if (StringUtils.isNotEmpty(sql)) {
        boolean quoted = false;
        int bracesOpen = 0;
        boolean create = false;
        boolean type = false;
        boolean typeLen = false;
        boolean not = false;
        int max = 0;
        String lastWord = null;//from w  w  w .j  a va2  s.c  om
        String className = null;
        String fieldName = null;
        boolean classNameAppended = false;
        StringBuffer section = new StringBuffer();

        for (final StringTokenizer splitter = new StringTokenizer(sql, " ();,'`\r\n\t", true); splitter
                .hasMoreTokens();) {
            String w = splitter.nextToken();
            if (w.length() == 0)
                continue;

            if (!quoted) {
                w = w.toUpperCase();
                if (stripWords.contains(w) || (SPACE.equals(w)))
                    section.append(SPACE_CHAR);
                else if (QUOTE.equals(w) || QUOTE_ALT.equals(w)) {
                    section.append(w);
                    if (QUOTE_ALT.equals(w) && !quoted)
                        w = QUOTE;
                    if (QUOTE.equals(w) && !QUOTE.equals(lastWord))
                        quoted = !quoted;
                } else if (BRACE_OPEN.equals(w)) {
                    bracesOpen++;
                    if (create) {
                        trim(section);
                        if (!type) {
                            sections.add(section.toString());
                            section = new StringBuffer("CREATE PROPERTY ");
                            section.append(className).append('.');
                        } else {
                            sections.add(section.toString());
                            section = new StringBuffer("ALTER PROPERTY ");
                            section.append(className).append('.');
                            section.append(fieldName).append(" MAX ");
                            typeLen = true;
                        }
                    } else
                        section.append(w);

                } else if (BRACE_CLOSED.equals(w)) {
                    bracesOpen--;
                    if (create) {
                        if (typeLen) {
                            trim(section);
                            section.append(SPACE_CHAR).append(max);
                            typeLen = false;
                            max = 0;
                            section.append(SPACE_CHAR);
                            continue;
                        } else if (type) {
                            type = false;
                            classNameAppended = false;
                        } else
                            create = false;
                    }
                    section.append(w);
                } else if (COMMA.equals(w)) {
                    if (create) {
                        if (type) {
                            if (!typeLen) {
                                trim(section);
                                sections.add(section.toString());
                                section = new StringBuffer("CREATE PROPERTY ");
                                section.append(className).append('.');
                                type = false;
                                classNameAppended = true;
                                fieldName = null;
                            } else
                                max++;
                        } else
                            section.append(w);
                    }
                } else if (create) {
                    if (type) {
                        boolean suppw = supportedWords.contains(w);
                        if (typeLen || suppw) {
                            if (suppw) {
                                if (NOT.equals(w)) {
                                    not = true;
                                    continue;
                                } else {
                                    trim(section);
                                    sections.add(section.toString());
                                    section = new StringBuffer("ALTER PROPERTY ");
                                    section.append(className).append('.');
                                    section.append(fieldName);
                                    if (not) {
                                        section.append(" NOT");
                                        not = false;
                                    }
                                    if (NULL.equals(w)) {
                                        if (section.lastIndexOf(NOT) == section.length() - 3)
                                            section.append(w).append("=TRUE");
                                        else
                                            section.append(SPACE_CHAR).append(NOT).append(w).append("=FALSE");
                                    } else
                                        section.append(w);
                                }
                            } else
                                max += Integer.parseInt(w);
                        } else {
                            final String mtype = typeMap.get(w);
                            if (StringUtils.isEmpty(mtype))
                                throw new SQLException(String
                                        .format("Sorry, type %s is not supported by Orient at the moment.", w));
                            section.append(mtype);
                        }

                    } else if (!classNameAppended) {
                        className = w;
                        section.append(className);
                        classNameAppended = true;
                    } else {
                        if (section.charAt(section.length() - 1) == SPACE_CHAR)
                            section.deleteCharAt(section.length() - 1);
                        section.append(w);
                        fieldName = w;
                        type = true;
                    }
                } else if (supportedWords.contains(w))
                    section.append(w);
                else if (SEMICOLON.equals(w)) {
                    trim(section);
                    sections.add(section.toString());
                    section = new StringBuffer();
                } else if (TABLE.equals(w)) {
                    section.append(CLASS);
                    create = true;
                } else
                    section.append(w);
            } else
                section.append(w);
        }
    }
    return sections;
}

From source file:com.cws.esolutions.core.dao.impl.ServiceDataDAOImpl.java

/**
 * @see com.cws.esolutions.core.dao.interfaces.IServiceDataDAO#listServices(int)
 *//* w  w  w. j ava 2s. c o  m*/
public synchronized List<String[]> listServices(final int startRow) throws SQLException {
    final String methodName = IServiceDataDAO.CNAME + "#listServices(final int startRow) throws SQLException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", startRow);
    }

    Connection sqlConn = null;
    ResultSet resultSet = null;
    CallableStatement stmt = null;
    List<String[]> responseData = null;

    try {
        sqlConn = dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        stmt = sqlConn.prepareCall("{CALL listServices(?)}");
        stmt.setInt(1, startRow);

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.execute()) {
            resultSet = stmt.getResultSet();

            if (DEBUG) {
                DEBUGGER.debug("resultSet: {}", resultSet);
            }

            if (resultSet.next()) {
                resultSet.beforeFirst();
                responseData = new ArrayList<String[]>();

                while (resultSet.next()) {
                    String[] data = new String[] { resultSet.getString(1), // GUID
                            resultSet.getString(2), // SERVICE_TYPE
                            resultSet.getString(3), // NAME
                    };

                    responseData.add(data);
                }

                if (DEBUG) {
                    DEBUGGER.debug("List<String>: {}", responseData);
                }
            }
        }
    } catch (SQLException sqx) {
        ERROR_RECORDER.error(sqx.getMessage(), sqx);

        throw new SQLException(sqx.getMessage(), sqx);
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }

        if (stmt != null) {
            stmt.close();
        }

        if ((sqlConn != null) && (!(sqlConn.isClosed()))) {
            sqlConn.close();
        }
    }

    return responseData;
}

From source file:FacultyAdvisement.StudentRepository.java

public static void delete(DataSource ds, Student student) throws SQLException {
    Connection conn = ds.getConnection();
    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }//from w  w w.j  ava 2 s. co m
    try {

        PreparedStatement ps;
        ps = conn.prepareStatement("Delete from STUDENT where EMAIL=?");
        ps.setString(1, student.getUsername());
        ps.executeUpdate();
        ps = conn.prepareStatement("Delete from USERTABLE where USERNAME=?");
        ps.setString(1, student.getUsername());
        ps.executeUpdate();
        ps = conn.prepareStatement("Delete from GROUPTABLE where USERNAME=?");
        ps.setString(1, student.getUsername());
        ps.executeUpdate();
    } finally {
        conn.close();
    }

    //students = (HashMap<String, StudentPOJO>) readAll(); // reload the updated info
}

From source file:com.sun.faces.mock.MockResultSet.java

public Object getObject(String columnName) throws SQLException {

    if ((row <= 0) || (row > beans.length)) {
        throw new SQLException("Invalid row number " + row);
    }/*w  ww.j ava2  s. co  m*/
    try {
        if (columnName.equals("writeOnlyProperty") && (beans[row - 1] instanceof TestBean)) {
            return (((TestBean) beans[row - 1]).getWriteOnlyPropertyValue());
        } else {
            return (PropertyUtils.getSimpleProperty(beans[row - 1], columnName));
        }
    } catch (Exception e) {
        throw new SQLException(e.getMessage());
    }

}

From source file:com.netspective.axiom.policy.AnsiDatabasePolicy.java

public Object executeAndGetSingleValue(ConnectionContext cc, String sql) throws SQLException {
    Object value = null;/* www .  j ava  2 s.  c o m*/
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = cc.getConnection().createStatement();
        try {
            rs = stmt.executeQuery(sql);
            if (rs.next())
                value = rs.getObject(1);
        } finally {
            if (rs != null)
                rs.close();
        }
    } catch (NamingException e) {
        throw new SQLException(e.toString() + " [" + sql + "]");
    } catch (SQLException e) {
        throw new SQLException(e.toString() + " [" + sql + "]");
    } finally {
        if (stmt != null)
            stmt.close();
    }
    return value;
}

From source file:com.softberries.klerk.dao.PeopleDao.java

@Override
public void delete(Long id) throws SQLException {
    Person toDel = find(id);//from   w w  w  .ja v a  2  s  . c om
    AddressDao adrDao = new AddressDao();
    try {
        init();
        for (Address adr : toDel.getAddresses()) {
            adrDao.delete(adr.getId(), conn);
        }
        st = conn.prepareStatement(SQL_DELETE_PERSON);
        st.setLong(1, id);
        // run the query
        int i = st.executeUpdate();
        System.out.println("i: " + i);
        if (i == -1) {
            System.out.println("db error : " + SQL_DELETE_PERSON);
        }
        conn.commit();
    } catch (Exception e) {
        // rollback the transaction but rethrow the exception to the caller
        conn.rollback();
        e.printStackTrace();
        throw new SQLException(e);
    } finally {
        close(conn, st, generatedKeys);
    }
}

From source file:herddb.jdbc.BasicHerdDBDataSource.java

@Override
public Connection getConnection(String username, String password) throws SQLException {
    ensureConnection();/*from w  w  w.j  a va2s.  com*/
    try {
        return pool.borrowObject();
    } catch (Exception err) {
        throw new SQLException(err);
    }
}