Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2006
    Messaggi
    258

    Connessione java-mysql....

    Salve ragazzi,
    vorrei chiedervi un aiuto per una connessione java-mysql.
    Ho realizzato un'applicazione JSF che con questa funzione mi realizza tranquillamente l'inserimento in una tabella di mysql, tramite l'uso di Datasource.
    codice:
    public String AddNewUser() {
    		try {
    			Context ctx = new InitialContext();
    
    			if (ctx == null) {
    
    				throw new Exception("Boom - No Context");
    
    			}
    
    			Context envCtx = (Context) ctx.lookup("java:comp/env");
    			DataSource ds = (DataSource) ctx.lookup("java:comp/env/MysqlJNDI"); // "java:comp/env/jdbc/nomedb"
    			if (ds != null) {
    				Connection conn = ds.getConnection();
    				if (conn != null) {
    					PreparedStatement pst = null;
    					pst = conn
    							.prepareStatement("INSERT INTO utenti(username,password,teamname,type,firstname,lastname,city,date,address,profession,email) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
    					pst.setString(1, login);
    					pst.setString(2, password);
    					pst.setString(3, teamname);
    					pst.setString(4, role);
    					pst.setString(5, firstName);
    					pst.setString(6, lastName);
    					pst.setString(7, city);
    					pst.setString(8, date);
    					pst.setString(9, address);
    					pst.setString(10, profession);
    					pst.setString(11, email);
    					pst.executeUpdate();
    					pst.close();
    					conn.close();
    				}
    			}
    
    		} catch (Exception e) {
    			e.printStackTrace();
    
    		}
    		return "addedUsr";
    	}
    Per motivi di pulizia ed efficienza del codice,vista la presenza di numerose query ho pensato di tirare fuori una classe esclusivamente per la connessione al db ed ho realizzato questa classe chiamata Singleton
    codice:
    package giu;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    public class Singleton {
    	private static Singleton singleton=null;
    	private static javax.sql.DataSource ds=null;
    	
    	static
    	{
    		try { 
    			Context ctx = new InitialContext(); 
    			if (ctx == null)
    			{ 
    				throw new Exception("No Context"); 
    			} 
    			ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/MysqlJNDI");
    		}catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	
    	}
    	
    	
    	public static Singleton getInstance()
    	{
    		if(singleton != null)
    		{
    			singleton=new Singleton();
    		}
    		return singleton;
    	}
    	
    	public javax.sql.DataSource getDataSource()
    	{
    		return ds;
    	}
    }
    Il problema è che andando a modificare la mia vecchia funzione per l'inserimento per tenere presente le nuove modifiche ho un errore che mi dice che
    MysqlJNDI non appartiente al contesto.

    Ecco la nuova funzione,come pensavo dovesse essere strutturata.Potete aiutarmi?Grazie
    codice:
    public String AddNewUser() throws SQLException {
    		
    		try{
    		y=1;
    		javax.sql.DataSource dataSource=Singleton.getInstance().getDataSource();
    		
    		java.sql.Connection conn=dataSource.getConnection();
    				if (conn != null) {
    			
    			PreparedStatement pst = null;
    			
    			pst = conn.prepareStatement("INSERT INTO utenti(username,password,teamname,type,firstname,lastname,city,date,address,profession,email) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
    			
    			pst.setString(1, login);
    			pst.setString(2, password);
    			pst.setString(3, teamname);
    			pst.setString(4, role);
    			pst.setString(5, firstName);
    			pst.setString(6, lastName);
    			pst.setString(7, city);
    			pst.setString(8, date);
    			pst.setString(9, address);
    			pst.setString(10, profession);
    			pst.setString(11, email);
    			pst.executeUpdate();
    			y=7;
    			pst.close();
    			y=8;
    			conn.close();
    			y=9;
    		}
    		y=10;
    		}
    		
    		catch(Exception e){y=99;};
    		return "addedUsr";
    	}
    Il problema è che ricado subito nel ramo dell'eccezione,perchè?
    Grazie mille e scusate la mia inesperienza.

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2006
    Messaggi
    258
    scusate....
    ce ne ho messo di tempo ma ho risolto l'errore....
    dove c'è
    codice:
    public static Singleton getInstance()
    	{
    		if(singleton != null)
    		{
    			singleton=new Singleton();
    		}
    		return singleton;
    	}
    ci voleva

    codice:
    public static Singleton getInstance()
    	{
    		if(singleton == null)
    		{
    			singleton=new Singleton();
    		}
    		return singleton;
    	}
    mi ero confuso....la classe funziona perfettamente,spero possa servire a qualcuno...
    alla prossima

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.