Konektovanje na MySQL bazu preko DataSource +/- JSP

Konektovanje na MySQL bazu preko DataSource +/- JSP

offline
  • Pridružio: 13 Okt 2005
  • Poruke: 26

Kad stavim sledeci kod direktno u JSP stranicu lepo uspem da se konektujem na bazu.Medjutim,probala sam radi preglednosti da napravim posebnu klasu za konektovanje sa init() metodom i ako u taj metod stavim kod,i pozovem metod iz JSP-a onda ne radi!

InitialContext ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/typewriter"); Connection con = ds.getConnection(); Statement stmt = con.createStatement();

Da nije mozda problem sto se taj context.xml ne odnosi na bilo koju klasu,vec samo na jsp-ove,mislim lupam al mozda... Smile
Ima neko ideju?



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • real 
  • Elitni građanin
  • Pridružio: 25 Nov 2004
  • Poruke: 2297
  • Gde živiš: Kragujevac

Primer (možda bude od neke pomoći):

When placed on the bean class, the @Resource annotation registers a reference to the external resource into the JNDI ENC of the EJB bean class:

@Stateful
@Resource(name="jdbc/OracleDB",

type=javax.sql.DataSource,

mappedName="java:/DefaultDS")
public class TravelAgentBean implements TravelAgentRemote {
...
}



In this example, the @Resource annotation is binding a javax.sql.DataSource into the jdbc/OracleDB ENC name. The mappedName( ) attribute provides a global, vendor-specific identifier so that the application server can locate the desired resource. Code within the travelAgentBean can locate this data source under the java:comp/env/jdbc/OracleDB JNDI ENC name. Here's how a client bean would use this context to look up the reference:

InitialContext jndiContext = new InitialContext( );
DataSource oracle = (DataSource)
jndiContext.lookup("java:comp/env/jdbc/OracleDB");



When the @Resource annotation is used on the bean class, the name( ) and type( ) attributes are required. As stated earlier, mappedName( ) or additional vendor-specific annotation metadata may be required by the vendor to truly identify the resource.



offline
  • Pridružio: 13 Okt 2005
  • Poruke: 26

Hmmm...sa EJB baratam slabo,takoreci nikako, a nisam provalila kako bi mi ovo pomoglo.Ja mogu bez problema da se povezem na bazu direktno iz JSP-a,ali ne i iz neke obicne klase,sto sam i nasla u Connector/J dokumentaciji:
Note: *This code only works when you are using servlets
* or EJBs in a J2EE application server. If you are
* using connection pooling in standalone Java code, you
* will have to create/configure datasources using whatever
* mechanisms your particular connection pooling library
* provides.

Ali ne znam sta to treba da konfigurisem Bebee Dol

Dopuna: 21 Nov 2006 18:32

Resila sam problem,u stvari ne znam ni gde je bio problem,sve sam lepo ponovo pisala pa je proradilo. Wink

Ko je trenutno na forumu
 

Ukupno su 1079 korisnika na forumu :: 45 registrovanih, 6 sakrivenih i 1028 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: aramis s, ArchaBasha, babaroga, bojcistv, Bubimir, cavatina, chica, Denaya, Dimitrije Paunovic, Doca, Duh sa sekirom, FOX, galerija, Georgius, gorval, HrcAk47, jackreacher011011, Još malo pa deda, Krusarac, Lidija, ljuba, Matija, mačković, MB120mm, mercedesamg, Mercury, Metanoja, Millennium, Milometer, milutin134, mkukoleca, mnn2, moldway, Parker, Polemarchoi, procesor, royst33, simazr, Simon simonović, sombrero, Tila Painen, tmanda323, vathra, Vlada78, voja64