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:org.homiefund.config.DatasourceConfiguration.java

@Bean(name = "dataSource")
@Description("ComboPooledDataSource responsible for database connection.")
public DataSource c3p0dataSource() throws SQLException {
    ComboPooledDataSource ds = new ComboPooledDataSource();
    try {//from  ww  w. j av a2  s .  co m
        ds.setDriverClass(environment.getProperty("jdbc.driver_class"));
    } catch (PropertyVetoException e) {
        throw new SQLException(e);
    }
    ds.setJdbcUrl(environment.getProperty("jdbc.url"));
    ds.setUser(environment.getProperty("jdbc.username"));
    ds.setPassword(environment.getProperty("jdbc.password"));
    ds.setPreferredTestQuery(environment.getProperty("jdbc.test_query"));
    ds.setInitialPoolSize(environment.getProperty("jdbc.poolsize.init", Integer.class));
    ds.setMinPoolSize(environment.getProperty("jdbc.poolsize.min", Integer.class));
    ds.setMaxPoolSize(environment.getProperty("jdbc.poolsize.max", Integer.class));
    ds.setCheckoutTimeout(environment.getProperty("jdbc.timeout", Integer.class));
    ds.setMaxStatements(environment.getProperty("jdbc.max_statements", Integer.class));
    ds.setIdleConnectionTestPeriod(environment.getProperty("jdbc.idle_test_period", Integer.class));

    return ds;
}

From source file:com.healthmarketscience.jackcess.DataTypes.java

public static int toSQLType(byte dataType) throws SQLException {
    Integer i = (Integer) SQL_TYPES.get(new Byte(dataType));
    if (i != null) {
        return i.intValue();
    } else {/*from   ww  w  .  j a v  a 2  s .  com*/
        throw new SQLException("Unsupported data type: " + dataType);
    }
}

From source file:io.moquette.spi.impl.security.DBAuthenticatorTest.java

@Before
public void setup() throws ClassNotFoundException, SQLException, NoSuchAlgorithmException {
    Class.forName(ORG_H2_DRIVER);
    this.connection = DriverManager.getConnection(JDBC_H2_MEM_TEST);
    Statement statement = this.connection.createStatement();
    try {//  ww w  .  ja  va2 s.com
        statement.execute("DROP TABLE ACCOUNT");
    } catch (SQLException sqle) {
        LOG.info("Table not found, not dropping", sqle);
    }
    MessageDigest digest = MessageDigest.getInstance(SHA_256);
    String hash = new String(Hex.encodeHex(digest.digest("password".getBytes(StandardCharsets.UTF_8))));
    try {
        if (statement.execute("CREATE TABLE ACCOUNT ( LOGIN VARCHAR(64), PASSWORD VARCHAR(256))")) {
            throw new SQLException("can't create USER table");
        }
        if (statement.execute("INSERT INTO ACCOUNT ( LOGIN , PASSWORD ) VALUES ('dbuser', '" + hash + "')")) {
            throw new SQLException("can't insert in USER table");
        }
    } catch (SQLException sqle) {
        LOG.error("Table not created, not inserted", sqle);
        return;
    }
    LOG.info("Table User created");
    statement.close();
}

From source file:FacultyAdvisement.StudentRepository.java

public static Map readAll(DataSource ds) throws SQLException {
    if (ds == null) {
        throw new SQLException("ds is null; Can't get data source");
    }//from w ww  . java 2  s  .  c  om

    Connection conn = ds.getConnection();

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

    HashMap<String, Student> list = new HashMap<>();

    try {
        PreparedStatement ps = conn.prepareStatement("select * from STUDENT");

        // retrieve customer data from database
        ResultSet result = ps.executeQuery();

        while (result.next()) {
            Student s = new Student();
            s.setId(result.getString("STUID"));
            s.setUsername(result.getString("EMAIL"));
            s.setFirstName(result.getString("FIRSTNAME"));
            s.setLastName(result.getString("LASTNAME"));
            s.setMajorCode(result.getString("MAJORCODE"));
            s.setPhoneNumber(result.getString("PHONE"));
            if (result.getString("ADVISED").equals("true")) {
                s.setAdvised(true);
            } else {
                s.setAdvised(false);
            }
            list.put(s.getId(), s);
        }

    } finally {
        conn.close();
    }

    return list;
}

From source file:es.tid.fiware.rss.exceptionhandles.JsonExceptionMapperTest.java

@Test
public void toResponse() throws Exception {
    UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
    Mockito.when(mockUriInfo.getAbsolutePath()).thenReturn(new URI("http://www.test.com/go"));
    ReflectionTestUtils.setField(mapper, "uriInfo", mockUriInfo);

    RSSException e = new RSSException("RssException");
    Response response = mapper.toResponse(e);
    Assert.assertTrue(true);//from  w w w . ja  v a  2  s  .c o m

    GenericJDBCException ex = new GenericJDBCException("sql", new SQLException("reason"));
    response = mapper.toResponse(ex);
    Assert.assertTrue(true);

    JDBCConnectionException ex1 = new JDBCConnectionException("sql", new SQLException("reason"));
    response = mapper.toResponse(ex1);
    Assert.assertTrue(true);

    NotFoundException ex2 = new NotFoundException();
    response = mapper.toResponse(ex2);
    Assert.assertTrue(true);

    Exception ex3 = new Exception("RssException");
    response = mapper.toResponse(ex3);
    Assert.assertTrue(true);

    Exception ex4 = new Exception("RssException", ex);
    response = mapper.toResponse(ex4);
    Assert.assertTrue(true);

    Exception ex5 = new Exception("RssException", ex1);
    response = mapper.toResponse(ex5);
    Assert.assertTrue(true);

}

From source file:com.tacitknowledge.util.migration.jdbc.DataSourceMigrationContext.java

/**
 * Returns the database connection to use
 *
 * @return the database connection to use
 * @throws SQLException if an unexpected error occurs
 *///w  w  w .ja  v a  2  s  .c  o m
public Connection getConnection() throws SQLException {
    if ((connection == null) || connection.isClosed()) {
        DataSource ds = getDataSource();
        if (ds != null) {
            connection = ds.getConnection();
        } else {
            throw new SQLException("Datasource is null");
        }
    }
    return connection;
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodec.java

/**
 * {@inheritDoc}/*from  ww  w  .  j a va 2s  .  co  m*/
 * @see net.sf.hajdbc.codec.Codec#encode(java.lang.String)
 */
@Override
public String encode(String value) throws SQLException {
    try {
        Cipher cipher = Cipher.getInstance(this.key.getAlgorithm());

        cipher.init(Cipher.ENCRYPT_MODE, this.key);

        return new String(Base64.encodeBase64(cipher.doFinal(value.getBytes())));
    } catch (GeneralSecurityException e) {
        throw new SQLException(e);
    }
}

From source file:cz.muni.fi.editor.database.helpers.DataSourceConfiguration.java

@Bean(name = "dataSource")
public DataSource c3p0DataSource() throws SQLException {
    ComboPooledDataSource ds = new ComboPooledDataSource();
    try {/* www.j  a  v a  2  s . co  m*/
        ds.setDriverClass(env.getProperty("database.jdbc.driver_class"));
    } catch (PropertyVetoException e) {
        throw new SQLException(e);
    }
    ds.setJdbcUrl(env.getProperty("database.jdbc.url"));
    ds.setUser(env.getProperty("database.jdbc.username"));
    ds.setPassword(env.getProperty("database.jdbc.password"));
    ds.setPreferredTestQuery(env.getProperty("database.jdbc.test_query"));
    ds.setInitialPoolSize(env.getProperty("database.jdbc.poolsize.init", Integer.class));
    ds.setMinPoolSize(env.getProperty("database.jdbc.poolsize.min", Integer.class));
    ds.setMaxPoolSize(env.getProperty("database.jdbc.poolsize.max", Integer.class));
    ds.setCheckoutTimeout(env.getProperty("database.jdbc.timeout", Integer.class));
    ds.setMaxStatements(env.getProperty("database.jdbc.max_statements", Integer.class));
    ds.setIdleConnectionTestPeriod(env.getProperty("database.jdbc.idle_test_period", Integer.class));

    return ds;
}

From source file:com.qubole.quark.catalog.db.encryption.AESEncrypt.java

public String convertToEntityAttribute(String phrase) throws SQLException {
    try {/*from ww w .ja v  a  2  s  . co  m*/
        Cipher decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(this.key, "UTF-8"));

        return new String(decryptCipher.doFinal(Hex.decodeHex(phrase.toCharArray())));
    } catch (Exception e) {
        throw new SQLException(e);
    }
}

From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java

public boolean deleteContact(int id) {

    int rows = 0;

    try {//  www. ja v  a2s.c  o m
        StringBuffer sbDelete = new StringBuffer();

        sbDelete.append("DELETE FROM ");
        sbDelete.append(ContactsConstants.CONTACTS_TABLE_NAME);
        sbDelete.append(" WHERE ");
        sbDelete.append(ContactsConstants.CONTACTS_COL_ID + " = " + id);

        DataSource d = DerbyDAOFactory.getDataSource();
        QueryRunner run = new QueryRunner(d);

        rows = run.update(sbDelete.toString());

        if (rows != 1) {
            throw new SQLException("executeUpdate return value: " + rows);
        }

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

    return true;

}