Ensures that a given user exists in the database. - Android Database

Android examples for Database:Table Exists

Description

Ensures that a given user exists in the database.

Demo Code


//package com.book2s;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    public final static String TABLE_USERS = "users";
    public final static String TABLE_USERS_COLUMN_ID = "id";
    public final static String TABLE_USERS_COLUMN_NAME = "name";

    /**// ww w .  ja va2 s  . com
     * Ensures that a given user exists in the database. If the user already
     * exists in the database then this method doesn't do very much. If the
     * user does not already exist in the database, then this method will
     * insert them into it.
     * 
     * @param sqlConnection
     * An <strong>already valid</strong> connection to the database. This
     * connection will not be closed after we are finished performing
     * operations here.
     * 
     * @param userId
     * The ID of the user as a long. This is the user's Facebook ID.
     * 
     * @param userName
     * The name of the user as a String.
     * 
     * @throws SQLException
     * If at some point there is some kind of connection error or query problem
     * with the SQL database then this Exception will be thrown.
     */
    public static void ensureUserExistsInDatabase(
            final Connection sqlConnection, final long userId,
            final String userName) throws SQLException {
        // prepare a SQL statement to be run on the database
        String sqlStatementString = "SELECT * FROM " + TABLE_USERS
                + " WHERE " + TABLE_USERS_COLUMN_ID + " = ?";
        PreparedStatement sqlStatement = sqlConnection
                .prepareStatement(sqlStatementString);

        // prevent SQL injection by inserting data this way
        sqlStatement.setLong(1, userId);

        // run the SQL statement and acquire any return information
        final ResultSet sqlResult = sqlStatement.executeQuery();

        if (sqlResult.next())
        // user exists in the database. no further actions needs to be taken
        {

        } else
        // user does not exist in the database. we need to put them in there
        {
            // prepare a SQL statement to be run on the database
            sqlStatementString = "INSERT INTO " + TABLE_USERS + " ("
                    + TABLE_USERS_COLUMN_ID + ", "
                    + TABLE_USERS_COLUMN_NAME + ") VALUES (?, ?)";
            sqlStatement = sqlConnection
                    .prepareStatement(sqlStatementString);

            // prevent SQL injection by inserting data this way
            sqlStatement.setLong(1, userId);
            sqlStatement.setString(2, userName);

            // run the SQL statement
            sqlStatement.executeUpdate();
        }

        closeSQLStatement(sqlStatement);
    }

    /**
     * Releases a SQL PreparedStatement resource.
     * 
     * @parameter sqlStatement
     * A SQL PreparedStatement object. It's okay if this object is null or if
     * it was never given an actual SQL statement / query to run.
     */
    public static void closeSQLStatement(
            final PreparedStatement sqlStatement) {
        if (sqlStatement != null) {
            try {
                sqlStatement.close();
            } catch (final SQLException e) {

            }
        }
    }
}

Related Tutorials