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:br.ufmt.ic.pawii.util.Estados.java

public static String getEstados(String estado, String nome, String email) {
    try {/*from  ww w  .j a  v  a 2s  . com*/

        String strCidades;

        Statement stm;

        Connection con = ConnBD.getConnection();

        if (con == null) {
            throw new SQLException("Erro conectando");
        }

        stm = con.createStatement();

        String sql = "SELECT codigo,municipio FROM municipios" + " WHERE uf='" + estado
                + "' ORDER BY municipio ASC";

        ResultSet rs = stm.executeQuery(sql);

        JSONArray cidades = new JSONArray();

        while (rs.next()) {

            String codigo = rs.getString(1);
            String cidade = rs.getString(2);

            JSONObject jsonCidade = new JSONObject();
            jsonCidade.put("codigo", codigo);
            jsonCidade.put("nome", cidade);
            cidades.put(jsonCidade);
        }

        JSONObject jsonRetorno = new JSONObject();
        jsonRetorno.put("cidades", cidades);
        jsonRetorno.put("seuNome", nome);
        jsonRetorno.put("seuEmail", email);

        strCidades = jsonRetorno.toString();

        return strCidades;

    } catch (SQLException ex) {
        System.out.println("Error: " + ex.getMessage());
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    return null;
}

From source file:StoredProcUtil.java

public static void addRaceEvent(String name, String location, String date) {

    if ((!check(name)) || (!check(location)) || (!check(date)))
        throw new IllegalArgumentException("Invalid param values passed to addRaceEvent()");

    Connection conn = null;//from  w  w w  .j a v a 2  s  .com

    try {

        conn = pool.getConnection();

        if (conn == null)
            throw new SQLException("Invalid Connection in addRaceEvent method");

        CallableStatement cs = null;

        //Create an instance of the CallableStatement
        cs = conn.prepareCall("{call addEvent (?,?,?)}");

        cs.setString(1, name);
        cs.setString(2, location);
        cs.setString(3, date);

        //Call the inherited PreparedStatement.executeUpdate() method
        cs.executeUpdate();

        // return the connection to the pool
        conn.close();

    } catch (SQLException sqle) {
    }

}

From source file:Main.java

/**
 * <p>/*from  w  w w  .j  a v a  2s.c  o  m*/
 * Execute a DDL statement
 * </p>
 */
public static void executeDDL(String ddl) throws SQLException {
    Connection conn = getLocalConnection();

    // now register the function
    print(ddl);
    try {
        PreparedStatement createStatement = conn.prepareStatement(ddl);

        createStatement.execute();
        createStatement.close();
    } catch (SQLException t) {
        SQLException s = new SQLException("Could not execute DDL:\n" + ddl);

        s.setNextException(t);

        throw s;
    }
}

From source file:FacultyAdvisement.StudentRepository.java

public static void create(DataSource ds, Student student) throws SQLException {
    String studentSQL = "INSERT INTO STUDENT(STUID, EMAIL, FIRSTNAME, LASTNAME, MAJORCODE, PHONE, ADVISED) "
            + "VALUES (?, ?, ?, ?, ?, ?, \'false\')";
    String userSQL = "INSERT INTO USERTABLE(PASSWORD, USERNAME, VERIFIED) VALUES (?, ?, ?)"; //haseeb was here

    String groupSQL = "INSERT INTO GROUPTABLE(GROUPNAME, USERNAME) VALUES (\'customergroup\', ?)";

    if (ds == null) {
        throw new SQLException("ds is null; Can't get data source");
    }/* w ww .  jav a2  s. c om*/

    Connection conn = ds.getConnection();

    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }

    try {

        //Here we execute three SQL statements
        //Student Information
        PreparedStatement sqlStatement = conn.prepareStatement(studentSQL);

        sqlStatement.setString(1, student.getId());
        sqlStatement.setString(2, student.getUsername());
        sqlStatement.setString(3, student.getFirstName());
        sqlStatement.setString(4, student.getLastName());
        sqlStatement.setString(5, student.getMajorCode());
        sqlStatement.setString(6, student.getPhoneNumber());

        sqlStatement.executeUpdate();

        //user credentials
        sqlStatement = conn.prepareStatement(userSQL);

        //Encrypt the pssword into SHA-256
        sqlStatement.setString(1, SHA256Encrypt.encrypt(student.getPassword()));

        sqlStatement.setString(2, student.getUsername());
        sqlStatement.setString(3, "false");
        sqlStatement.execute();

        //Group Table
        sqlStatement = conn.prepareStatement(groupSQL);
        sqlStatement.setString(1, student.getUsername());
        sqlStatement.execute();
    } finally {
        conn.close();
    }

}

From source file:com.sf.ddao.factory.param.ForwardParameter.java

@Override
public void appendParam(Context context, StringBuilder sb) throws SQLException {
    Object param = extractParam(context);
    if (param == null) {
        throw new SQLException("ParameterHandler '" + this.name + "' is not defined");
    }//from  w w  w.j a  v a2 s .co m
    StatementParamter statementParamter = (StatementParamter) param;
    statementParamter.appendParam(context, sb);
}

From source file:net.ageto.gyrex.persistence.jdbc.pool.internal.PoolDataSourceFactory.java

@Override
public ConnectionPoolDataSource createConnectionPoolDataSource(final Properties props) throws SQLException {
    throw new SQLException("not allowed");
}

From source file:it.larusba.neo4j.jdbc.http.driver.Neo4jStatement.java

/**
 * Convert the list of query to a JSON compatible with Neo4j endpoint.
 *
 * @param queries List of cypher queries.
 * @return The JSON string that correspond to the body of the API call
 *//*from  w w  w  .  j  ava  2s.c o  m*/
public static String toJson(List<Neo4jStatement> queries, ObjectMapper mapper) throws SQLException {
    StringBuffer sb = new StringBuffer();
    try {
        sb.append("{\"statements\":");
        sb.append(mapper.writeValueAsString(queries));
        sb.append("}");

    } catch (JsonProcessingException e) {
        throw new SQLException("Can't convert Cypher statement(s) into JSON");
    }
    return sb.toString();
}

From source file:com.wso2telco.ids.datapublisher.util.DBUtil.java

private static Connection getConnectDBConnection() throws SQLException, AuthenticatorException {
    initializeDatasources();//from  w  ww  .  j a va  2s  . c o  m

    if (mConnectDatasource != null) {
        return mConnectDatasource.getConnection();
    }
    throw new SQLException("Sessions Datasource not initialized properly");
}

From source file:org.neo4j.jdbc.http.driver.Neo4jStatement.java

/**
 * Convert the list of query to a JSON compatible with Neo4j endpoint.
 *
 * @param queries List of cypher queries.
 * @param mapper mapper//  ww  w .  j av  a 2 s  . c om
 * @return The JSON string that correspond to the body of the API call
 * @throws SQLException sqlexception
 */
public static String toJson(List<Neo4jStatement> queries, ObjectMapper mapper) throws SQLException {
    StringBuilder sb = new StringBuilder();
    try {
        sb.append("{\"statements\":");
        sb.append(mapper.writeValueAsString(queries));
        sb.append("}");

    } catch (JsonProcessingException e) {
        throw new SQLException("Can't convert Cypher statement(s) into JSON");
    }
    return sb.toString();
}

From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java

public static byte getByteFromString(String stringVal) throws SQLException {

    if (StringUtils.isBlank(stringVal)) {
        return (byte) 0;
    }/*from   w w w .  ja  v a 2  s . co m*/
    stringVal = stringVal.trim();

    try {
        int decimalIndex = stringVal.indexOf(".");

        if (decimalIndex != -1) {
            double valueAsDouble = Double.parseDouble(stringVal);
            return (byte) valueAsDouble;
        }

        long valueAsLong = Long.parseLong(stringVal);

        return (byte) valueAsLong;
    } catch (NumberFormatException NFE) {
        throw new SQLException("Parse byte value error:" + stringVal);
    }
}