How to check if there is a specific number in the database

This is my entire code:

import java.sql.*;
import java.io.*;

public class VerInformacaoPassageiro {

    public static void main(String args[]) {
        String dbname = "BD22";
        String dbuser = "postgres";
        String password = "12345";
        String url = "jdbc:postgresql://localhost:5432/" + dbname;

        try {
            BufferedReader in;
            in = new BufferedReader( new InputStreamReader( System.in ));
            System.out.print("Numero de identificacao: ");
            String identificacao = in.readLine();

            Connection c = DriverManager.getConnection(url, dbuser, password);
            c.setAutoCommit(false);
            Statement stmt = c.createStatement();
            String query = "SELECT nomeP, sexo, destinopretendido, dataviagem " +
                    "FROM passageiros " +
                    "WHERE nidentificacao='" + identificacao + "';";
            System.out.println("QUERY: " + query);
            ResultSet rs = stmt.executeQuery(query);
            System.out.println( "Informacao do passageiro com numero de identificacao " + identificacao);
            System.out.println( "---------------------------------------");
            while ( rs.next() ) {
                int nidentificacaoP = rs.getInt("nidentificacao");
                String nome = rs.getString("nomeP");
                String sexo = rs.getString("sexo");
                String destinopretendido = rs.getString("destinopretendido");
                String dataviagem = rs.getString("dataviagem");
                if (nidentificacaoP == NULL)
                    System.out.print("Identificacao nao encontrada");
                else
                    System.out.println( nome + " do sexo "  + sexo + " para o destino " + destinopretendido + " no dia " + dataviagem );
            }

            rs.close();
            stmt.close();
            c.close();
        }
        catch (Exception e) {
            System.err.println( e.getClass().getName()+": "+ e.getMessage() );
            System.exit(0);
        }
    }
}

But my doubt is in this part of the code:

    if (nidentificacaoP == NULL)
        System.out.print("Identificacao nao encontrada");
    else
        System.out.println( nome + " do sexo "  + sexo + " para o destino " + destinopretendido + " no dia " + dataviagem );
}

rs.close();
stmt.close();
c.close();

My goal is to find a certain ID number in a database that will give me a passenger’s information but I want to use an if to write "ID not found" if this ID is not in the database but I don’t know how to do this. (I left it as NULL inside the if because I didn’t know what to put in it so it won’t be empty, so I can submit it here on Stack Overflow). Someone can help me with this? what should I write inside the if to check if the ID exists?

>Solution :

Assuming identificacao is a unique identifier, the query will return either one or no rows, so you don’t need to process the result set with a while, but with an if:

// Single row found
if (rs.next()) {
    int nidentificacaoP = rs.getInt("nidentificacao");
    String nome = rs.getString("nomeP");
    String sexo = rs.getString("sexo");
    String destinopretendido = rs.getString("destinopretendido");
    String dataviagem = rs.getString("dataviagem");
    System.out.println
        (nome + " do sexo "  + sexo + " para o destino " + 
         destinopretendido + " no dia " + dataviagem);
// No row found
} else {
    System.out.print("Identificacao nao encontrada");
}

Mandatory side note:
Concatenating the condition like that is at best a bad practice and at worst leaves your application vulnerable to SQL Injection attacks if identificacao is received from user-controlled input. You should probably convert this query to a PreparedStatemet with placeholders.

Leave a Reply