Salve a tutti,
sto impazzendo per un problema per la connessione ad un db.
Vi posto le classi e poi vi spiego il problema
codice:
package schedule_database;

import java.sql.*;
import java.util.*;

import schedule_database.DbGateway;
import schedule_eccezioni.applException;

public class DbManager implements InterfacciaCreazione, InterfacciaEliminazione, InterfacciaLettura{
	
	private QueryManager qm=null; //oggetto della classe QueryManager
	private DbGateway dbg=null; //oggetto della classe DbGateway
	
	//costruttore
	public DbManager(){
		dbg=new DbGateway();
		qm=new QueryManager(dbg);
	}
	
	//connessione del database
	public void ConnessioneDb() throws applException{
		dbg.ConnessioneDb();
	}
	
	//disconnessione del database
	public void DisconnessioneDb() throws applException{
		dbg.DisconnessioneDb();
	}
	
	//rollback delle transazioni
	public void ErroreRollback() throws applException{
		dbg.ErroreRollback();
	}
	
	
	//recupera l'id del docente partendo dall'username e dalla password
	public int OttieniId (String username, String password) throws Exception{
		ResultSet risultato=null; //oggetto simile ad una tabella che conterrà i record recuperati dal database
		int id_utente=0;

		risultato=dbg.EsecuzioneQuery(qm.LoginUtente(username, password)); //impostazione ed esecuzione della query
		
		try{
			if (risultato.next()){
				id_utente=risultato.getInt(alias.docente_id); //in id_utente viene salvato l'id dell'utente corrispondente ad username e password
			}
		}
		catch (SQLException e ){
			throw new applException ("ERRORE nell'eseguire la query di lettura dell'utente con "+
										"login "+username);
		}
		return id_utente;
	}
	
	//recupera i dati di un docente
	public HashMap<String, String> OttieniDatiDocente (Integer id_docente) throws applException, Exception{ //viene passato un Integer perchè poi dovrà essere convertito in stringa
		ResultSet rs_risultato=null;
		HashMap<String, String> mp_risultato=new HashMap<String, String>();
		
		try{
			rs_risultato=dbg.EsecuzioneQuery(qm.OttieniDatiDocente(id_docente.intValue())); //creazione ed esecuzione della query
				
				//aggiunta uno ad uno degli degli attributi trovati nel db nella struttura hashmap
			if (rs_risultato.next()){
				mp_risultato.put(alias.docente_id, new String(id_docente.toString()));
				mp_risultato.put(alias.docente_nome, rs_risultato.getString(alias.docente_nome));
				mp_risultato.put(alias.docente_cognome, rs_risultato.getString(alias.docente_cognome));
				mp_risultato.put(alias.docente_dipartimento, rs_risultato.getString(alias.docente_dipartimento));
				Integer piano=new Integer (rs_risultato.getInt(alias.docente_piano));
				mp_risultato.put(alias.docente_piano, new String (piano.toString()));
				Integer stanza=new Integer (rs_risultato.getInt(alias.docente_stanza));
				mp_risultato.put(alias.docente_stanza, new String (stanza.toString()));
				mp_risultato.put(alias.docente_telefono, rs_risultato.getString(alias.docente_telefono));
				mp_risultato.put(alias.docente_email, rs_risultato.getString(alias.docente_email));
				mp_risultato.put(alias.docente_user, rs_risultato.getString(alias.docente_user));
				mp_risultato.put(alias.docente_psw, rs_risultato.getString(alias.docente_psw));
			}
		}
		catch (SQLException e){
			throw new applException ("ERRORE nell'eseguire la query di lettura dell'utente");
		}
		return mp_risultato;
	}
	
	//recupera l'id del docente partendo dall'username
	public int PrelevaIdDocente (String username) throws applException, Exception{
		ResultSet risultato=null;
		int id=0;
		
		try{
			risultato=dbg.EsecuzioneQuery(qm.PrelevaIdDocente(username));
			if (risultato.next())
				id=risultato.getInt(1);
			}
		catch (SQLException e){
			throw new applException ("ERRORE nell'esecuzione della query dell'utente");
		}
		return id;
	}
}



package schedule_database;

import java.sql.*;

import schedule_eccezioni.applException;
import schedule_util.*;


public class DbGateway {
	
	ConfigurazioneSchedule config=new ConfigurazioneSchedule(); //oggetto per leggere dal file XML
	
	private static final String NOME_DRIVER="com.mysql.jdbc.Driver"; //nome del driver
	public String NOME_SERVER=new String(); //nome del server
	public String PORTA=new String(); //numero della porta del server
	public String NOME_DATABASE=new String(); //nome del database
	public String USERNAME=new String(); //username
	public String PASSWORD=new String(); //password
	
	private String URL=new String(); //URL della stringa di connessione
	private String STRINGA_CONNESSIONE=new String(); //stringa di connessione
	
	private Connection connessione=null;
	private Statement statement=null;
	private PreparedStatement pstmt=null;
	
	//costruttore
	public DbGateway(){
		this.NOME_SERVER=config.getPath("SCHEDULE"+".NOME_SERVER");
		this.PORTA=config.getPath("SCHEDULE"+".PORTA");
		this.NOME_DATABASE=config.getPath("SCHEDULE"+".NOME_DATABASE");
		this.USERNAME=config.getPath("SCHEDULE"+".USERNAME");
		this.PASSWORD=config.getPath("SCHEDULE"+".PASSWORD");
		
		if (NOME_SERVER==null){
			this.NOME_SERVER=config.getPath("databanker.NOME_SERVER");
		}
		if (PORTA==null){
			this.PORTA=config.getPath("databanker.PORTA");
		}
		if (NOME_DATABASE==null){
			this.NOME_DATABASE=config.getPath("databanker.NOME_DATABASE");
		}
		if (USERNAME==null){
			this.USERNAME=config.getPath("databanker.USERNAME");
		}
		if (PASSWORD==null){
			this.PASSWORD=config.getPath("databanker.PASSWORD");
		}
		
		this.URL="jdbc:mysql://" + NOME_SERVER + PORTA + "/" + NOME_DATABASE;
		this.STRINGA_CONNESSIONE = URL + "?user=" + USERNAME + "&password=" + PASSWORD;
	}
	
	//connessione
	public void ConnessioneDb() throws applException{
		try{
			Class.forName(NOME_DRIVER); //carica il driver JDBC
		}
		catch (ClassNotFoundException e){
			throw new applException ("ERRORE nel caricamento del driver JDBC");
		}
		try{
			connessione=DriverManager.getConnection(STRINGA_CONNESSIONE); //creazione connessione al database
		}
		catch (SQLException e){
			throw new applException ("ERRORE nella connessione al database");
		}
	}
	
	public void ErroreRollback() throws applException{
		try{
			connessione.rollback();
		}
		catch (SQLException e){
			throw new applException ("ERRORE nell'operazione di rollback");
		}
	}
	
	//connessione del database
	public void DisconnessioneDb() throws applException{
		try{
			connessione.close();
			connessione=null;
		}
		catch (SQLException e){
			throw new applException ("ERRORE nella disconnessione al database");
		}
	}
	
	//impostazione della query
	public PreparedStatement PreparaStatement(String query) throws applException{
		try{
			if ((query!=null) && (query!=""))
				//punto in cui il programma si blocca perchè connessione=null
				pstmt=connessione.prepareStatement(query); //imposta la query in pstmt di tipo PreparedStatement
		}
		catch (SQLException e){
			throw new applException ("ERRORE nella fase di preparazione della query");
		}
		return pstmt;
	}
	
	//esecuzione della query
	public ResultSet EsecuzioneQuery (PreparedStatement statement) throws applException{
		ResultSet risultato=null;
		
		try{
			if (statement!=null)
				risultato=statement.executeQuery();
		}
		catch (SQLException e){
			throw new applException ("ERRORE durante l'eliminazione delle tuple dalla tavola");
		}
		return risultato;
	}
	
	
	
}



package schedule_database;

import java.sql.*;

import schedule_eccezioni.*;

public class QueryManager {
	
	private DbGateway dbg=null;
	private String query=null;
	
	//costruttore
	public QueryManager (DbGateway dbg){
		this.dbg=dbg; //viene inizializzato il DbGateway perchè in questa classe si utilizza il metodo dbg.PreparaStatement
	}
	
	public PreparedStatement LoginUtente (String username, String password) throws Exception{
		PreparedStatement statement=null; //oggetto di tipo PreparedStatement per contenere la query
 
		//creazione della query
		query="SELECT * FROM " + alias.tabella_docenti + 
		" WHERE (" + alias.docente_user + "=? and " + alias.docente_psw + "=?)";
		
		try{
			System.out.println (dbg.PORTA+" "+dbg.NOME_DATABASE+" "+dbg.PASSWORD+" "+dbg.NOME_SERVER+" "+dbg.USERNAME);
			statement=dbg.PreparaStatement(query); //impostazione della query
			statement.setString(1, username);
			statement.setString(2, password);
		}
		catch (SQLException e){
			throw new applException ("ERRORE nella preparazione della query di lettura dell'utente "+username);
		}
		return statement;
	}
	
	public PreparedStatement OttieniDatiDocente (int id_docente) throws Exception{
		PreparedStatement statement=null;
		
		query="SELECT * FROM " + alias.tabella_docenti + "WHERE (" + alias.docente_id + "=?)";
		statement=dbg.PreparaStatement(query);
		
		try{
			statement.setInt(1, id_docente);
		}
		catch (SQLException e){
			throw new Exception ("ERRORE nella preparazione della query");
		}
		return statement;
	}
	
	public PreparedStatement PrelevaIdDocente (String username) throws Exception{
		PreparedStatement statement=null;
		
		query="SELECT " + alias.docente_id + " FROM " + alias.tabella_docenti + " WHERE " + alias.docente_user + "=?";
		statement=dbg.PreparaStatement(query);
		
		try{
			statement.setString(1, username);
		}
		catch (SQLException e){
			throw new Exception("ERRORE nella preparazione della query di lettura delle autorizzazioni " + "dell'utente con login " + username);
		}
		return statement;
	}

}

Il problema è che la classe dbgateway effettua correttamente la connessione al db però nel metodo PreparaStatement della classe querymanager questa connessione viene persa. Infatti se in questo metodo si scrive System.out.println(connessione), come output in consolle avremo null.

Cosa posso fare???? vi prego aiutatemi