Example usage for java.sql PreparedStatement execute

List of usage examples for java.sql PreparedStatement execute

Introduction

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

Prototype

boolean execute() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

Usage

From source file:com.keybox.manage.db.AuthDB.java

/**
 * updates password for admin using auth token
 */// w  w  w . j a  va  2s .c om
public static boolean updatePassword(Auth auth) {
    boolean success = false;

    Connection con = null;
    try {
        con = DBUtils.getConn();

        String prevSalt = getSaltByAuthToken(con, auth.getAuthToken());
        PreparedStatement stmt = con
                .prepareStatement("select * from users where auth_token like ? and password like ?");
        stmt.setString(1, auth.getAuthToken());
        stmt.setString(2, EncryptionUtil.hash(auth.getPrevPassword() + prevSalt));
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {

            String salt = EncryptionUtil.generateSalt();
            stmt = con.prepareStatement("update users set password=?, salt=? where auth_token like ?");
            stmt.setString(1, EncryptionUtil.hash(auth.getPassword() + salt));
            stmt.setString(2, salt);
            stmt.setString(3, auth.getAuthToken());
            stmt.execute();
            success = true;
        }

        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);
    return success;
}

From source file:com.flexive.core.Database.java

/**
 * Store a FxString in a translation table that only consists of one(!) translation column
 *
 * @param string     string to be stored
 * @param con        existing connection
 * @param table      storage table//from w w w.ja v  a  2s .c  o  m
 * @param dataColumn name of the data column
 * @param idColumn   name of the id column
 * @param id         id of the given string
 * @throws SQLException if a database error occured
 */
public static void storeFxString(FxString string, Connection con, String table, String dataColumn,
        String idColumn, long id) throws SQLException {
    if (!string.isMultiLanguage()) {
        throw new FxInvalidParameterException("string", LOG, "ex.db.fxString.store.multilang", table)
                .asRuntimeException();
    }
    PreparedStatement ps = null;
    try {
        ps = con.prepareStatement("DELETE FROM " + table + ML + " WHERE " + idColumn + "=?");
        ps.setLong(1, id);
        ps.execute();
        ps.close();
        if (string.getTranslatedLanguages().length > 0) {
            ps = con.prepareStatement("INSERT INTO " + table + ML + " (" + idColumn + ",LANG,DEFLANG,"
                    + dataColumn + ") VALUES (?,?,?,?)");
            ps.setLong(1, id);
            String curr;
            for (long lang : string.getTranslatedLanguages()) {
                curr = string.getTranslation(lang);
                if (curr != null && curr.trim().length() > 0) {
                    ps.setInt(2, (int) lang);
                    ps.setBoolean(3, lang == string.getDefaultLanguage());
                    ps.setString(4, curr);
                    ps.executeUpdate();
                }
            }
        }
    } finally {
        if (ps != null)
            ps.close();
    }
}

From source file:com.keybox.manage.db.AuthDB.java

/**
 * updates the admin table based on auth id
 *
 * @param con  DB connection//from w w w .  jav a  2  s.c o  m
 * @param auth username and password object
 */
public static void updateLogin(Connection con, Auth auth) {

    try {
        PreparedStatement stmt = con.prepareStatement(
                "update users set username=?, auth_type=?, auth_token=?, password=?, salt=? where id=?");
        stmt.setString(1, auth.getUsername());
        stmt.setString(2, auth.getAuthType());
        stmt.setString(3, auth.getAuthToken());
        if (StringUtils.isNotEmpty(auth.getPassword())) {
            String salt = EncryptionUtil.generateSalt();
            stmt.setString(4, EncryptionUtil.hash(auth.getPassword() + salt));
            stmt.setString(5, salt);
        } else {
            stmt.setString(4, null);
            stmt.setString(5, null);
        }
        stmt.setLong(6, auth.getId());
        stmt.execute();

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

}

From source file:com.foxelbox.foxbukkit.database.DatabaseConnectionPool.java

private void _runSQL(Connection connection, String sql) throws SQLException {
    PreparedStatement stmt = connection.prepareStatement(sql);
    stmt.execute();
    stmt.close();// ww w.j a v  a2  s.  c o m
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

public String checkAccess(DataSource dataSource) {
    try {/*from w  w w  . j  av a2 s .co m*/
        Connection connection = dataSource.getConnection();
        PreparedStatement selectOne = connection.prepareStatement(SELECT_ONE);
        selectOne.execute();
        return "ok";
    } catch (SQLException e) {
        return "failed with " + e.getMessage();
    }
}

From source file:intapp.databasemigration.Engine.PrepareTargetDatabaseActor.java

@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof PrepareDatabaseRequest) {
        PrepareDatabaseRequest request = (PrepareDatabaseRequest) message;
        this.engine = sender();

        request.Scripts.forEach(script -> {

            try {
                System.out.println("Loading sql for resource " + script);
                InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(script);
                StringWriter writer = new StringWriter();
                IOUtils.copy(resourceAsStream, writer, Charset.defaultCharset());
                String sql = writer.toString();
                queries.add(sql);//  w ww . j a  va  2s. c  o m

            } catch (Exception ex) {
                System.err.println(ex);
            }
        });

        this.pgConnectionActor.tell("get", self());

    } else if (message instanceof Connection) {
        this.connection = (Connection) message;
        this.queries.forEach(sql -> {
            try {
                PreparedStatement s1 = connection.prepareStatement(sql);
                s1.execute();
            } catch (Exception ex) {
                System.err.println(ex);
            }
        });

        System.out.println("SQL queries executed");
        this.engine.tell(this.getResponse(), null);
    }
}

From source file:conexaoBanco.GerenciarProduto.java

public void deletarProduto(int id) {
    if (con != null) {
        //INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
        String sql = "delete from produtos where codigo = " + id;
        try {// www.j av a2s  .c  om
            PreparedStatement valores = con.prepareStatement(sql);
            valores.execute();
            //ResultSet result = (ResultSet) valores.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.tec.webapp.jdbc.entity.support.IdentifierCallback.java

/** {@inheritDoc} */
@Override()//from  www.  j a  v a  2  s.c  o m
public Long doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
    ResultSet generatedKeys = null;
    try {
        ps.execute();

        generatedKeys = ps.getGeneratedKeys();

        if (generatedKeys.next()) {
            return Long.valueOf(generatedKeys.getLong(1));
        } else {
            throw new SQLException("Unable to insert new record " + ps.toString());
        }
    } finally {
        if (generatedKeys != null) {
            generatedKeys.close();
        }
    }
}

From source file:conexaoBanco.GerenciarProduto.java

public void adicionarProduto(String nome, double preco, String imagem, String descricao,
        Integer codigoVendedor) {
    if (con != null) {
        //INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
        String sql = "insert into produtos(nome, preco, imagem,descricao,vendedor) values ("
                + arrumarString(nome) + "," + preco + "," + arrumarString(imagem) + ","
                + arrumarString(descricao) + "," + codigoVendedor + ")";
        try {/*from w  w  w. jav  a  2  s .  c  om*/
            PreparedStatement valores = con.prepareStatement(sql);
            valores.execute();
            //ResultSet result = (ResultSet) valores.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:conexaoBanco.GerenciarProduto.java

public void atualizarProduto(int id, String nome, double preco, String imagem, String descricao,
        Integer codigoVendedor) {
    if (con != null) {
        //INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
        String sql = "UPDATE produtos SET nome = " + arrumarString(nome) + ", preco = " + preco + ", imagem = "
                + arrumarString(imagem) + ", descricao = " + arrumarString(descricao) + " where codigo = " + id
                + " and vendedor = " + codigoVendedor;
        try {/*from w w  w .  j a  va2  s.c  o m*/
            PreparedStatement valores = con.prepareStatement(sql);
            valores.execute();
            //ResultSet result = (ResultSet) valores.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}