martedì 22 ottobre 2013

Q19

Given the code fragment:

             try {
                  String query = "SELECT * FROM Employee WHERE ID=110";
                  Statement stmt = conn.createStatement();
                  ResultSet rs = stmt.executeQuery(query);
                  System.out.println("Employee ID: " + rs.getInt("ID"));
            } catch (Exception se) {
                  System.out.println("Error");
            }

Assume that the SQL query matches one record.

What is the result of compiling and executing this code?

A.
The code prints Error.

B.
The code prints the employee ID.

C.
Compilation fails due to an error at line 13.

D.
Compilation fails due to an error at line 14.

La domanda non è molto chiara. Sono mancanti numeri di linea e quindi non si capisce a cosa si riferiscono le risposte C e D.

Comunque sia il codice così come è scritto non compila perché l’oggetto conn non è mai stato definito :
Statement stmt = conn.createStatement();  à conn cannot be resolved

Quindi la risposta è probabilmente C se conn viene usato alla linea 13 .


Se invece era prevista la definizione e l’istanziamento  della connessione il codice compila bene. In questo caso a run time il flusso si interrompe al momento di usare il ResultSet, infatti questo oggetto va scorso con un ciclo (while, next) e non può essere usato in questo modo. Definiamo una connessione (in questo caso pratico per Postgres) e scriviamo un codice che compila:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Principale {
      public static void main(String[] args)
      {
            try {
            Connection conn = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/test","test", "test");
            String query = "SELECT * FROM Employee WHERE ID=110";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            System.out.println("Employee ID: " + rs.getInt("ID"));
            } catch (Exception se) {
                  System.out.println("Error");
                  }
      }}
In questo caso si ha un errore che viene catturato dal catch. L’errore è :
Il «ResultSet» non è correttamente posizionato; forse è necessario invocare «next()».
Perciò la risposta sarebbe A : si stampa Error.

Per ottener un codice che possa funzionare si deve scorrere il ResultSet in maniera corretta, ad esempio :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Principale {
      public static void main(String[] args) throws SQLException {
            Connection conn = DriverManager.getConnection(
                        "jdbc:postgresql://localhost:5432/test", "test", "test");
            String query = "SELECT * FROM Employee WHERE ID=110";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                  System.out.println("Employee ID: " + rs.getInt("ID"));
            }
      }
}

Riferimenti:

 

Nessun commento:

Posta un commento