Example usage for javax.sql DataSource getConnection

List of usage examples for javax.sql DataSource getConnection

Introduction

In this page you can find the example usage for javax.sql DataSource getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Attempts to establish a connection with the data source that this DataSource object represents.

Usage

From source file:org.biblionum.commentaire.modele.CommentaireOuvragesModele.java

public static boolean updateOuvragetype(DataSource ds, int keyId, String contenu_commentaire,
        String date_commentaire, int utilisateurid, int ouvrageid) throws SQLException {

    Connection con = ds.getConnection();
    String sql = "SELECT * FROM ouvragetype WHERE id = ?";
    PreparedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    statement.setInt(1, keyId);/*from w  w w  .j a v a2s.c o  m*/
    ResultSet entry = statement.executeQuery();

    entry.last();
    int rows = entry.getRow();
    entry.beforeFirst();
    if (rows == 0) {
        entry.close();
        statement.close();
        con.close();
        return false;
    }
    entry.next();

    if (contenu_commentaire != null) {
        entry.updateString("contenu_commentaire", contenu_commentaire);
    }
    if (date_commentaire != null) {
        entry.updateString("date_commentaire", date_commentaire);
    }
    entry.updateInt("utilisateurid", utilisateurid);
    entry.updateInt("ouvrageid", ouvrageid);

    entry.updateRow();
    entry.close();
    statement.close();
    con.close();
    return true;
}

From source file:com.glaf.core.jdbc.DBConnectionFactory.java

public static String getDatabaseType(DataSource dataSource) {
    String dbType = null;/* w  ww.j av  a2s  .  c o  m*/
    Connection con = null;
    try {
        con = dataSource.getConnection();
        dbType = getDatabaseType(con);
    } catch (Exception ex) {
        logger.error(ex);
        throw new RuntimeException(ex);
    } finally {
        JdbcUtils.close(con);
    }
    return dbType;
}

From source file:de.griffel.confluence.plugins.plantuml.DatasourceHelper.java

/**
 * Returns Database meta data./*from w w w .ja va 2s.c  om*/
 *
 * @param datasource data source
 * @param attributes attributes to get
 * @return Map with requested attributes
 */
public static Map<String, String> getDatabaseMetadata(DataSource datasource, List<String> attributes) {
    final Map<String, String> databaseMetadata = new HashMap<String, String>();
    Connection con = null;
    try {
        con = datasource.getConnection();
        final DatabaseMetaData dbmd = con.getMetaData();
        for (String attribute : attributes) {
            if (CATALOGS.equals(attribute)) {
                databaseMetadata.put(CATALOGS, StringUtils.join(getCatalogs(dbmd), ","));
            } else if (TABLE_TYPES.equals(attribute)) {
                databaseMetadata.put(TABLE_TYPES, StringUtils.join(getTableTypes(dbmd), ","));
            } else {
                fillValue(dbmd, attribute, databaseMetadata);
            }
        }
    } catch (SQLException e) {
        log.debug("SQLException accessing metadata", e);
        databaseMetadata.put(ERROR, e.getMessage());
    } finally {
        try {
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            log.debug("SQLException closing connection", ex);
        }
    }
    return databaseMetadata;
}

From source file:fm.last.commons.test.LastAssertions.java

/**
 * Asserts that the database that the passed DataSource has been configured to use is a unit test database.
 * //from  ww w .  j  a v a 2  s . c  om
 * @param dataSource Configured DataSource.
 * @throws SQLException If an error occurs connecting to the database to retrieve the database name.
 */
public static void assertTestDatabase(DataSource dataSource) throws SQLException {
    Connection connection = null;
    String dbName = null;
    try {
        connection = dataSource.getConnection();
        dbName = connection.getCatalog();
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    if (!dbName.endsWith(TEST_DB_SUFFIX)) {
        throw new IllegalArgumentException("Database provided was " + dbName
                + " but tests must run against a database who's name ends with " + TEST_DB_SUFFIX);
    }
}

From source file:org.biblionum.commentaire.modele.CommentaireOuvragesModele.java

public static CommentaireOuvrages getCommentaireByid(DataSource ds, int id) {

    CommentaireOuvrages co = null;//from www.j a v  a  2  s.  co  m
    Connection con = null;
    PreparedStatement stm;
    ResultSet rs;
    try {
        con = ds.getConnection();
        stm = con.prepareStatement(
                "SELECT commentaireouvrages.id AS comid, commentaireouvrages.contenu_commentaire, commentaireouvrages.date_commentaire, utilisateur.id AS uid,utilisateur.pseudo, utilisateur.nom, utilisateur.prenom, utilisateur.type_user  FROM commentaireouvrages INNER JOIN utilisateur ON  commentaireouvrages.Utilisateurid=utilisateur.id WHERE commentaireouvrages.id=?");
        stm.setInt(1, id);//type user prof 1

        rs = stm.executeQuery();
        Utilisateur u = new Utilisateur();

        while (rs.next()) {
            co = new CommentaireOuvrages();
            u = new Utilisateur();
            u.setId(rs.getInt("uid"));
            u.setPseudo(rs.getString("pseudo"));
            u.setNom(rs.getString("nom"));
            u.setPrenom(rs.getString("prenom"));
            co.setId(rs.getInt("comid"));
            co.setContenu_commentaire(rs.getString("contenu_commentaire"));
            co.setDate_commentaire(rs.getString("date_commentaire"));
            co.setUtilisateur(u);

        }

        con.close();
        rs.close();

    } catch (SQLException ex) {
        Logger.getLogger(UtilisateurModele.class.getName()).log(Level.SEVERE, null, ex);
    }

    return co;
}

From source file:mangotiger.sql.SQL.java

public static List<Object> list(final DataSource dataSource, final String sql, final Object[] parameters)
        throws SQLException {
    Connection connection = null;
    try {/*from www  .  j a v a2s. co m*/
        connection = dataSource.getConnection();
        return list(connection, sql, parameters);
    } catch (SQLException e) {
        log().error(e);
        throw e;
    } finally {
        close(connection);
    }
}

From source file:mangotiger.sql.SQL.java

public static Map<String, List<Object>> map(final DataSource dataSource, final String sql,
        final Object[] parameters) throws SQLException {
    Connection connection = null;
    try {//  w  w w  . ja  v  a 2  s  .  co m
        connection = dataSource.getConnection();
        return map(connection, sql, parameters);
    } catch (SQLException e) {
        log().error(e);
        throw e;
    } finally {
        close(connection);
    }
}

From source file:mangotiger.sql.SQL.java

public static int update(final DataSource dataSource, final String sql, final Object[] parameters)
        throws SQLException {
    Connection connection = null;
    try {//from w  ww .j  av a 2 s. c  o m
        connection = dataSource.getConnection();
        return update(connection, sql, parameters);
    } catch (SQLException e) {
        log().error(e);
        throw e;
    } finally {
        close(connection);
    }
}

From source file:com.glaf.core.jdbc.DBConnectionFactory.java

public static boolean checkConnection(java.util.Properties props) {
    Connection connection = null;
    try {//from ww w .ja v  a 2 s.  c  om
        if (StringUtils.isNotEmpty(props.getProperty(DBConfiguration.JDBC_DATASOURCE))) {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(props.getProperty(DBConfiguration.JDBC_DATASOURCE));
            connection = ds.getConnection();
        } else {
            ConnectionProvider provider = ConnectionProviderFactory.createProvider(props);
            if (provider != null) {
                connection = provider.getConnection();
            }
        }
        if (connection != null) {
            return true;
        }
    } catch (Exception ex) {
        logger.error(ex);
        ex.printStackTrace();
    } finally {
        JdbcUtils.close(connection);
    }
    return false;
}

From source file:ca.nrc.cadc.db.DBUtil.java

private static void testDS(DataSource ds, boolean keepOpen) throws SQLException {
    Connection con = null;//from w ww . j  a  va2 s. co m
    try {
        con = ds.getConnection();
        DatabaseMetaData meta = con.getMetaData();
        if (!log.getEffectiveLevel().equals(Level.DEBUG))
            return;
        log.debug("connected to server: " + meta.getDatabaseProductName() + " " + meta.getDatabaseMajorVersion()
                + "." + meta.getDatabaseMinorVersion() + " driver: " + meta.getDriverName() + " "
                + meta.getDriverMajorVersion() + "." + meta.getDriverMinorVersion());
    } finally {
        if (!keepOpen && con != null)
            try {
                con.close();
            } catch (Exception ignore) {
            }
    }
}