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.
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 Singletoncodice: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"; }
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 checodice: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; } }
MysqlJNDI non appartiente al contesto.
Ecco la nuova funzione,come pensavo dovesse essere strutturata.Potete aiutarmi?Grazie
Il problema è che ricado subito nel ramo dell'eccezione,perchè?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"; }
Grazie mille e scusate la mia inesperienza.

Rispondi quotando