3 Replies Latest reply on May 25, 2009 7:11 PM by Peter Johnson

    Exception javax.naming.NoInitalContextException

    Claudemir Franco Newbie

      Hi,


      I need to insert some data in my database(hsqldb), but is not working. I am using the connection via a datasource, and by doing jndi lookup. When you run the code, I receive the exception:
      --------------------------------------------------------------------------------
      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
      ---------------------------------------------------------------------------------

      Please somebody help me.? ;o)

      My code:

      package org.jboss.soa.esb.dbaccess;

      import java.sql.Connection;
      import java.sql.PreparedStatement;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.sql.DataSource;

      public class InsertRecords {
      /** String driver = "org.hsqldb.jdbcDriver";
      String url = "jdbc:hsqldb:hsqldb\\localDB";
      String username = "sa";
      String password = "";
      Class.forName(driver);
      Connection conn = DriverManager.getConnection(url, username, password);
      return conn;**/

      public static void main(String[] args) throws Exception {

      Context initContext = new InitialContext();
      Context envContext = (Context)initContext.lookup("java:/comp/env");
      DataSource ds = (DataSource) envContext.lookup("jdbc/FtpZip");
      Connection conn = null;
      PreparedStatement pstmt = null;
      try {
      conn = ds.getConnection();
      String query = "insert into teste(name_file) values('lala')";
      pstmt = conn.prepareStatement(query); // create a statement
      pstmt.setInt(1, 1); // set input parameter 1
      //pstmt.setString(2, "deptname"); // set input parameter 2
      //pstmt.setString(3, "deptLocation"); // set input parameter 3
      pstmt.executeUpdate(); // execute insert statement
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      pstmt.close();
      conn.close();
      }
      }
      }

      Thanks..

      []´s

      Claudemir