Scusa ho sbagliato
codice:
public class Persona extends HttpServlet{
static int attivo;
static double lat;
static double lon;
static int codChip;
static String nomeDB = "localizzazione";
static String surname;
static String name;
static String istituto;
static int stanza;
static int telefono;
static int count;
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream();
res.setContentType("text/html");
String cognome = req.getParameter("cognome");
String nome = req.getParameter("nome");
// Connessione al database
Connection connDb = null;
// Prepara lo statement per lanciare il comando al database
PreparedStatement comandoSQL; // SQL
// Set di record risultante (elenco di righe della tabella che
// soddisfano il criterio della SELECT). SOLO PER LA QUERY SELECT!
ResultSet rsQuery; // Set di record
ResultSet risQuery;
ResultSet rslQuery;
String SQL;
try {
// Creazione di una istanza MySQL
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Apertura della connessione al database
connDb = DriverManager.getConnection("jdbc:mysql://127.0.0.1/"
+ nomeDB, "root", "manuela");
// Chiedo al db il chip della persona
SQL = "SELECT Cod_Chip, Attivo from ChipInterni WHERE Cognome='"
+ cognome + "' and Nome='" + nome + "';";
comandoSQL = connDb.prepareStatement(SQL);
rsQuery = comandoSQL.executeQuery();
// Creo il vettore risultato scorrendo tutto il ResultSet
while (rsQuery.next()) {
codChip = rsQuery.getInt(1);
attivo = rsQuery.getInt(2);
}
if (attivo != 0) {
SQL = "SELECT COUNT(Cod_Chip) FROM Coordinate where Cod_Chip = '"
+ codChip + "';";
comandoSQL = connDb.prepareStatement(SQL);
rslQuery = comandoSQL.executeQuery();
while (rslQuery.next()) {
count = rslQuery.getInt(1);
// Chiedo al db le coordinate del chip precedentemente
// chiesto
SQL = "SELECT Lat, Lon, Tempo FROM Coordinate WHERE Cod_Chip='"
+ codChip
+ "' ORDER BY Tempo LIMIT "
+ (count - 1)
+ ",1;";
comandoSQL = connDb.prepareStatement(SQL);
risQuery = comandoSQL.executeQuery();
while (risQuery.next()) {
lat = risQuery.getDouble(1);
lon = risQuery.getDouble(2);
}
//modificare in base ai valori restituiti dal DB (puntino di localizzazione)
out.println("<HEAD><TITLE> Localizzazione </TITLE></HEAD><BODY>");
out.println("<h1> CNR_Localizzazione_risultato </h1>");
out.println("Hai inserito: "+cognome+", "+nome);
out.println("</BODY>");
// Chiedo ed estraggo le informazioni sulla persona cercata
// e
// trovata
// Chiedo al db le informazioni circa la persona voluta
SQL = "SELECT Cognome, Nome, Istituto, N_Stanza, N_Telefono from PersoneInterne WHERE Cod_Chip='"
+ codChip + "';";
comandoSQL = connDb.prepareStatement(SQL);
rsQuery = comandoSQL.executeQuery();
while (rsQuery.next()) {
surname = rsQuery.getString(1);
name = rsQuery.getString(2);
istituto = rsQuery.getString(3);
stanza = rsQuery.getInt(4);
telefono = rsQuery.getInt(5);
}
//modificare in base ai valori restituiti dal DB (dati circa la persona)
out.println("<HEAD><TITLE> Localizzazione </TITLE></HEAD><BODY>");
out.println("<h1> CNR_Localizzazione_risultato </h1>");
out.println("Hai inserito: "+cognome+", "+nome);
out.println("</BODY>");
}
} else
System.out.println("La persona non è stata trovata");
connDb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}