Query the database for a game's info. - Android Database

Android examples for Database:SQL Query

Description

Query the database for a game's info.

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_GAMES = "games";
    public final static String TABLE_GAMES_COLUMN_ID = "id";

    /**//  w  w  w.  j  a  v a2  s  .com
     * Query the database for a game's info.
     * 
     * @param sqlConnection
     * Your existing database Connection object. Must already be connected, as
     * this method makes no attempt at doing so.
     * 
     * @param gameId
     * The ID of the game you're searching for.
     * 
     * @return
     * The query's resulting ResultSet object. Could be empty or even null,
     * check for that and then the emptyness with the ResultSet's .next()
     * method.
     * 
     * @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 ResultSet grabGamesInfo(final Connection sqlConnection,
            final String gameId) throws SQLException {
        // prepare a SQL statement to be run on the database
        final String sqlStatementString = "SELECT * FROM " + TABLE_GAMES
                + " WHERE " + TABLE_GAMES_COLUMN_ID + " = ?";
        final PreparedStatement sqlStatement = sqlConnection
                .prepareStatement(sqlStatementString);

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

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

        return sqlResult;
    }
}

Related Tutorials