Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: DAO exception

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DAO exception

    Hi all iam running this code for datasource from server .iam getting this error .Please do let me know.

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    import org.apache.log4j.Logger;
     
    public class AbstractDAO
    {
        /** The datasource used by this object. */
        protected DataSource dataSource = null;
    	protected String jndiDataSourceName = null;
    	public static String DEFAULT_SCHEMA;
    	private static Logger logger = Logger.getLogger(AbstractDAO.class);
    	/**
    	 * Constructor for the class.
    	 * @param jndiDataSourceName the JNDI name of the datasource
    	 * @throws AbstractDAO if the datasource cannot be located
    	 */
    	protected AbstractDAO(String jndiDataSourceName) throws DAOException
    	{
    		this.jndiDataSourceName = jndiDataSourceName;
    		try
    		{
    			dataSource = getDataSource(this.jndiDataSourceName);
    		}
    		catch(NamingException ne)
    		{
    			logger.error("AbstractDAO:: AbstractDAO() -Unable to create the Data Access Object " + ne);
    			throw new DAOException("AbstractDAO:: AbstractDAO() - Unable to create the Data Access Object",ne);
    		}
    	 }
    	/**
    	 * Looks up and returns a DataSource object from the Naming service.
    	 *
    	 * @param jndiName the name used to bind the DataSource object
    	 *  in the Naming service
    	 * @return the DataSource object specified by the jndiName parameter
    	 * @throws NamingException if the DataSource object cannot be located
    	 *  within the Naming service
    	 */
    	public DataSource getDataSource(String jndiName) throws NamingException
    	{
    		InitialContext context = new InitialContext();
    		this.dataSource = (DataSource) context.lookup(jndiName);
    		return dataSource;
    	}
    	/**
    	 * Returns a Connection object from a specific DataSource.
    	 * @param jndiDataSourceName the name of the DataSource
    	 * @return Connection the request Connection object
    	 * @throws Exception is an error is encountered
    	 */
    	protected Connection getConnection() throws SQLException
    	{
    		Connection connection = dataSource.getConnection();
    		if(DEFAULT_SCHEMA != null)		{
    			connection.createStatement().execute("ALTER SESSION SET CURRENT_SCHEMA="+DEFAULT_SCHEMA);
    			}
    		return connection;
    	}
        /**
         *  This method is used to check the string value is null or not
        */
    	protected boolean isBlank(String value)
    	{
    	    if(value == null || value.equals(""))
    	    {
    	    	return true;
    	    }
    	    else
    	    {
    	    	return false;
    	    }
    	}
    	/**
    	 * Method      : close
    	 * Description : This Method closes object for a specific DataSource.
    	 * @param resultSet
    	 * @param statement
    	 * @param connection
    	 */
    	protected void close(ResultSet resultSet, Statement statement, Connection connection) throws DAOException
    	{
    		try{
    			if(resultSet!= null)
    			{
    	    		try
    	    		{
    	    			resultSet.close();
    	    		}
    	    		catch(Exception ex)
    	    		{
    	    			logger.error("AbstractDAO:: close() -  Unable to close result set object");
    	    			throw new DAOException("AbstractDAO:: close() - Unable to close result set object");
    	    		}
    			}
    			if(statement != null)
    			{
    				try
    				{
    					statement.close();
    				}
    				catch(Exception ex)
    				{
    					logger.error("AbstractDAO:: close() -  Error While closing the statement "+ex);
    					throw new DAOException("AbstractDAO:: close() - Unable to close statement object");
    				}
    			}
    			if(connection != null)
    			{
    				try
    				{
    					connection.close();
    				}
    				catch(Exception ex)
    				{
    					logger.error("AbstractDAO:: close() -  Error While closing the connection "+ex);
    					throw new DAOException("AbstractDAO:: close() - Unable to close connection object");
    				}
    			}
    		}
    		catch(Exception exception)
    		{
    			logger.error("AbstractDAO:: close() -  Error While closing the connection "+exception);
    			throw new DAOException("AbstractDAO:: close() -  Error While closing the connection");
    		}
    	}
    	/**
    	 * Method       : roll back
    	 * Description  : This method roll back the transaciton.
    	 * @param connection
    	 */
        protected void rollback(Connection connection) throws DAOException
        {
    	if (connection != null)
    	    try {
    	    	connection.rollback(); 
    	    }catch(Exception ex)
    	    {
    	    	logger.error("AbstractDAO:: rollback() -  Error While roll back the transaciton "+ex);
    			throw new DAOException("AbstractDAO:: rollback() - Unable to roll back the transaciton");	
    	    }
        }
          /**
         * Method       : executeUpdate
         * Description  : this method used to update the DB.
         * @param connection
         * @param sql
         * @throws DAOException
         */
        protected void executeUpdate(Connection connection, String sql) throws DAOException
        {
        	logger.info("AdminGuiAbstractDAO:: executeUpdate() - In executeUpdate method");
        	PreparedStatement statment = null;
     		try
     		{     			
     			statment = connection.prepareStatement(sql);						
     		    statment.executeUpdate(); 			
     		}
     		catch(SQLException sqle)
     		{
    		    logger.debug("AbstractDAO:: executeUpdate() - Unable to Update " + sqle);
    		    throw new DAOException(sqle);
    		}
     		catch(Exception exception)
     		{			
    		    logger.debug("AbstractDAO:: executeUpdate() - Error in executeUpdate."+exception);
    		    throw new DAOException("AbstractDAO:: executeUpdate() - "+exception);
    		}
     		finally
     		{
    		    close(null, statment, null);
    		}
        }
    }

    iam getting the below error for this application
    12-10@16:35:36 ERROR(AbstractDAO.java (AbstractDAO.<init>()):34) - AbstractDAO::AbstractDAO() -Unable to create the Data Access Object 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

    thanks
    Last edited by nrao; December 12th, 2010 at 09:46 PM.


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: DAO exception

    Hi Rao,
    Exception are due to when you called Class constructor with jndi name from datasource then this jndi name must be bind with your datasource flle like (oracle-ds.xml,hibernate-config.xml loading these jndi setting at sratup).

    public Connection abstractConnDAO(){

    /*Using Datasource*/
    Context ctx = null;
    DataSource ds = null;
    /*Connection con = null;
    try {
    ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("java:/DefaultDS");
    con = ds.getConnection();
    }
    catch (NamingException e) {
    e.printStackTrace();
    con = null;
    }
    return con;
    }

    Example-Configuration for jndi name
    In hibernate config
    <property name="connection.datasource">java:/DefaultDS</property>

    Iin oracle-ds.xml file

    <datasources>
    <local-tx-datasource>
    <jndi-name>DefaultDS</jndi-name>
    ---
    ----
    ---
    </local-tx-datasource>
    </datasources>

    Note- This jndi name should be same as in file ..

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DAO exception

    yes it is the same but even though its showing the same exception can please help me

Similar Threads

  1. BAd Padding exception
    By Bverly in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 26th, 2010, 03:41 PM
  2. SecurityManager exception
    By phpuser123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 20th, 2010, 05:04 AM
  3. Exception Errors
    By TIMBERings in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 02:13 AM
  4. Error of "ClassNotFound Exception"
    By multicoder in forum Exceptions
    Replies: 3
    Last Post: July 1st, 2009, 02:58 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM