3 Replies Latest reply: Oct 12, 2007 6:11 AM by user3123886 RSS

    connecting to Sql server 2000

    807605
      I have used the following code to connect to a SQL server 2000 database in a lan.I don't get any error while registering the type 4 driver.But When I try to create a statement I do get a null pointer exception.
      MY code is -

      package leave.beans.database;
      import java.sql.Connection; //connection interface
      import java.sql.Statement; // Statement interface
      import java.sql.DriverManager; // DriverManager class
      import java.sql.ResultSet; // ResultSet interface
      import java.sql.ResultSetMetaData;
      import java.sql.DatabaseMetaData; // ResultSetMetaData interface
      import java.sql.SQLException; // SqlException interface

      /**
      * @author VINOD
      * database connection
      */


      public class DatabaseConnection
      {

      Connection connection=null;
      Statement statement=null;
      static final private String jdbc_driver="com.microsoft.jdbc.sqlserver.SqlServerDriver";
      static final private String dBase_url="jdbc:microsoft:sqlserver://baseserver:1433;databasename="+"BASE_URNA";

      public DatabaseConnection()
      {
           connection=null;
                statement=null;
           } // default constructor of the bean class

      public void setConnection()
      {

      try
      {
      Class.forName(jdbc_driver); // static method of class Class
      connection=DriverManager.getConnection(dBase_url,"baserf","rf"); //It returns an interface which is used throughout the application
      statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); // a statement object is created.

      //CreatStatement() takes two arguments-1st arg specifies type of scrolling(values:TYPE_SCROLL_SENSITIVE & TYPE_SCROLL_INSENSITIVE)
      // and the 2nd arg specifies whether the resultset is read only or updatable(values:CONCUR_READ_ONLY & CONCUR_UPDATABLE)


      }
      catch(Exception e) // Exception is used instead of SQLException because static method forName() throws java.lang.ClassNotFoundException.

      {  System.out.println(e.getMessage());}
      }

      public Connection getConnection() // getter methods
           {
                setConnection();
                return connection;
           }
      public Statement getStatement()
           {
                setConnection();
                return statement;
           }

      public void closeResources() // Releases the resources
      {
      try
      {
      statement.close();
      connection.close();
      }
      catch(SQLException se)
      {
      System.out.print("\nException: " + se.getMessage() + " Error: " + se.getErrorCode() );
      se.printStackTrace();
      }
      catch(Exception e)
      {
      System.err.print(e.toString() );
      e.printStackTrace();
      }
      }
      } // End of java bean class.

      plese help..