Results 1 to 2 of 2
- 02-19-2011, 10:21 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
create a poolable connection using mysql eclipse helios and apache tomcat 6 with java
Hello everyone,
I am trying to setup a poolable connection using eclipse helios, apache tomcat 6, mysql, and java.
I was able to get a connection through a jsp page that would return data following this tutorial from:
Apache Tomcat 6.0 - JNDI Datasource HOW-TO
But now I want to create a class in java that will get my connection from the context.xml.
I am in desperate need here, please help!
- 02-19-2011, 10:34 PM #2
Member
- Join Date
- Feb 2010
- Posts
- 26
- Rep Power
- 0
Here is my context.xml stored my META-INF
Java Code:<?xml version="1.0" encoding="UTF-8"?> <Context> <!-- Specify a JDBC datasource --> <Resource name="jdbc/wrights_auto" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="4" username="root" password="homedb" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/wrights_auto" /> </Context>
Here is the class I am running and it fails on this line:
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/wrights_auto");
error message: Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:645)
Java Code:package com.adc.wrights.common; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.naming.InitialContext; import javax.sql.DataSource; public class DAO { public void doSomething() throws Exception { /* * Create a JNDI Initial context to be able to * lookup the DataSource * * In production-level code, this should be cached as * an instance or static variable, as it can * be quite expensive to create a JNDI context. * * 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. */ InitialContext ctx = new InitialContext(); /* * Lookup the DataSource, which will be backed by a pool * that the application server provides. DataSource instances * are also a good candidate for caching as an instance * variable, as JNDI lookups can be expensive as well. */ DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/wrights_auto"); /* * The following code is what would actually be in your * Servlet, JSP or EJB 'service' method...where you need * to work with a JDBC connection. */ Connection conn = null; Statement stmt = null; try { conn = ds.getConnection(); /* * Now, use normal JDBC programming to work with * MySQL, making sure to close each resource when you're * finished with it, which allows the connection pool * resources to be recovered as quickly as possible */ stmt = conn.createStatement(); stmt.execute("select * from clients"); stmt.close(); stmt = null; conn.close(); conn = null; } finally { /* * close any jdbc instances here that weren't * explicitly closed during normal code path, so * that we don't 'leak' resources... */ if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } } /** * @param args * @throws Exception * @throws IOException * @throws ServletException * @throws SQLException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub DAO conn = new DAO(); conn.doSomething(); //Connection con = conn.dataSource.getConnection(); }Last edited by computerbum; 02-19-2011 at 10:35 PM. Reason: made an error
Similar Threads
-
Helios do not start tomcat 7
By pjava in forum Java ServletReplies: 9Last Post: 12-23-2010, 06:29 PM -
apache tomcat 7 and eclipse
By marie in forum EclipseReplies: 1Last Post: 11-04-2010, 05:40 PM -
Hibernate perspective in Eclipse Helios
By redforce.bala in forum EclipseReplies: 0Last Post: 10-01-2010, 07:55 AM -
Problem with running eclipse-jee-helios-win32-x86_64
By rohit_mali55@yahoo.in in forum EclipseReplies: 2Last Post: 09-09-2010, 03:48 PM -
Video Tutorial Eclipse 3.3 Create Web Application with Tomcat
By irnbru in forum EclipseReplies: 1Last Post: 10-05-2008, 11:09 AM


LinkBack URL
About LinkBacks

Bookmarks