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 931 korisnika na forumu :: 50 registrovanih, 9 sakrivenih i 872 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: 8u47, A.R.Chafee.Jr., airsuba, Alibaba1981, amaterSRB, bojank, bokisha253, Boris90, Bubimir, CikaKURE, DejanSt, deLacy, Dimitrije Paunovic, Dimitrise93, djuradj, Dvojac005, elenemste, Faki-Valjevo, FOX, Georgius, goxin, grenadir, Hans Gajger, Herman Terrance Aubrey, HrcAk47, hyla, ikan, Još malo pa deda, Karla, Kichma, kybonacci, mercedesamg, milenko crazy north, milutin134, moldway, nemkea71, Pikac-47, prle122, Rogan33, rovac, Srle993, ss10, Stanlio, stegonosa, Trpe Grozni, vathra, Vladko, x9, zlly, |_MeD_|