3 Replies Latest reply on Apr 17, 2003 5:26 PM by Juha Lindfors

    Nullpointer Exception when i do lookup for sqlserver 2000

    pinky Newbie

      Hi,

      Guys please help me asap i stuck here from past 1 week.

      This is kaladhar, i have installed java 1.4 and jboss3.2 on linux. I have installed sqlserver type 4 driver also( jtds.jdbc.Driver).

      I have written a small program which connects to the sqlserver database which is on remote machine(win 2000).

      I am getting initial context object, but when i do lookup, i am getting NullPointerException. following is the code
      --------------------------------------------------------

      import javax.sql.*;
      import java.sql.*;
      import javax.naming.*;
      import java.util.*;
      /**
      * Microsoft SQL Server JDBC test program
      */
      public class ConnectSQL
      {

      public ConnectSQL() {
      // Get connection
      System.out.println("Kaladhar-1");
      try
      {
      //Class.forName("net.sourceforge.jtds.jdbc.Driver");

      Connection connection = getConnection("java:/MSSQL");
      System.out.println("Kaladhar-3" );
      if (connection != null)
      {
      System.out.println();
      System.out.println("Successfully connected");
      System.out.println();
      // Meta data
      DatabaseMetaData meta = connection.getMetaData();
      System.out.println("\nDriver Information");
      System.out.println("Driver Name: "
      + meta.getDriverName());
      System.out.println("Driver Version: "
      + meta.getDriverVersion());
      System.out.println("\nDatabase Information ");
      System.out.println("Database Name: "
      + meta.getDatabaseProductName());
      System.out.println("Database Version: "+
      meta.getDatabaseProductVersion());
      }
      }catch (Exception e)
      {
      e.printStackTrace();
      }
      } // Test
      public static void main (String args[]) throws Exception
      {
      ConnectSQL cSQL = new ConnectSQL();
      }

      private Connection getConnection(String SPoolName)
      {
      Connection conn = null;
      Statement st = null;
      ResultSet rs = null;
      DataSource ds = null;
      InitialContext ctx = null;
      try
      {
      Properties prop = new Properties();
      prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.Nam
      ingContextFactory");
      prop.put(Context.PROVIDER_URL,"jnp://192.168.175.42:1099/");
      prop.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.inte
      rfaces");
      ctx = new InitialContext(prop);
      System.out.println("OBJECT---" + ctx);
      //ctx = new InitialContext();
      ds =(DataSource)ctx.lookup(SPoolName);
      System.out.println("OBJECT DS" + ds);
      conn = ds.getConnection();
      System.out.println("OBJECT" + conn);

      //System.out.println("From cDBAccess.getConnection(), poolname: " +
      // SPoolName + " Datasource is: " + ds + " Connection is: " + con
      n);
      }
      catch(NamingException e)
      {
      //System.out.println("Naming Exception while getting connection...1"
      );
      System.out.println("EXCEPTION IN DBACCESS DB NAMING EXCEPTION:"+e.ge
      tMessage());
      e.printStackTrace();
      }
      catch(SQLException ex)
      {
      //System.out.println("Exception while getting connection...2");
      System.out.println("EXCEPTION IN DBACCESS LOOKUP:"+ex.getMessage());
      ex.printStackTrace();
      }catch(Exception e)
      {
      System.out.println("EXCEPTION IN Kaladhar:");
      e.printStackTrace();
      }

      return conn;
      }//end of getConnection()
      }