![]() |
|
![]() |
| LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Junior Member Join Date: Sep 2006
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 4 | Database connection with JNDI If we are using JNDI data source is it mandatory to add about this in web.xml please give an example with source |
| | |
| | #2 (permalink) |
| Moderator Join Date: Apr 2006 Location: India
Posts: 637
Thanks: 57
Thanked 91 Times in 70 Posts
Rep Power: 20 | Re: Database connection with JNDI Yes, if u want to get a database connection using JNDI u need to define it in the web.xml. This is how u have to do --resource-ref-- description--DB Connection --/description-- --res-ref-name--jdbc/somename --/res-ref-name-- --res-type--javax.sql.DataSource--/res-type-- --res-auth--Container --/res-auth-- --/resource-ref-- And also a context.xml in ur Web application META-INF folder --Context path="/Webapp1"-- --Resource name="jdbc/somename" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/urdbname" username="root" password="XXXX" maxActive="100" maxIdle="10" maxWait="10000"/-- --/Context-- This is how u have to define it And after that write a DB connection class which creates a Connection Pool using this JNDI import java.sql.*; import javax.sql.*; import javax.naming.*; public class ConnectionPool { private static DataSource ds = null; private ConnectionPool() {} static { try { Context ctx = new InitialContext(); ds = (DataSource) ctx.lookup("java:comp/env/jdbc/somename"); } catch (NamingException ne) { } catch (Exception e) { } } public static Connection getConnection() { Connection conn = null; try { conn = ds.getConnection(); } catch (SQLException se) { } catch (Exception e) { } return conn; } } and in ur some dao class use this conn = ConnectionPool.getConnection(); stmt = conn.createStatement(); Please use XML opening and closing tags in ur web.xml and context.xml instead of -- |
| | |
![]() |
| Tags |
| connection , database , jndi |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Database Engineer with 2-4 Years Exp - Noida | sridhar | Experienced Jobs | 2 | 05-09-06 09:20 PM |
| 5 common PHP database Problems | vjsreevs | PERL, PYTHON, PHP & MY SQL | 0 | 04-09-06 03:43 PM |
| regarding oracle versions | Madhuri | ORACLE , SQL SERVER , SYBASE & Others | 7 | 07-07-06 06:06 PM |
| connection with database | chandra | DATA STRUCTURES, C, C++, VC ++ | 3 | 20-04-06 07:04 AM |
| java, jdbc, servlets | raghav | JAVA Technologies | 11 | 14-03-06 09:59 AM |
| More Interview Questions Here... |