Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Coding Issues

Options
  • 20-03-2009 3:09pm
    #1
    Registered Users Posts: 269 ✭✭


    Help i am trying to access a database through a web service i am using Apache Axis 2 + Netbeans + JDBC here is my code however i am getting a null pointer exception

    No value seems to be returning from my databaseutility2 or database connection class ie. null pointer exception
    import ie.travel.webservices.LookupService;
    /**
     *
     * @author Organ
     */
    public class test {
    public static void main(String[]args){
        LookupService service = new LookupService();
        String data = "Bart";
        //service.getDepartmentDetailsName(data);
        String result= service.getDepartmentbyName(data);
        System.out.println(result);
         //System.out.println(output);
         
    }
    }
    
    public class LookupService {
    
    public LookupService(){
    }
    
    public String getDepartmentbyName(String DeptName)
    {
        DatabaseUtility2 database = new DatabaseUtility2();
        Department mydepartment = new Department();
       try{
        mydepartment.setDeptName(DeptName);
        String departmentselect= mydepartment.searchDepartmentName();
        //return departmentselect;
       String databaseresult= database.selectRecord(departmentselect);
       return databaseresult;
        }
       catch(NullPointerException ex)
       {
        return "error"+ex;
       
       }
     }
    }
    
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package ie.travel.employee;
    import ie.travel.databaseutility.DatabaseUtility;
    
    /**
     *
     * @author Organ
     */
    public class Department {
    private String DeptName;
    private String DeptHead;
    private String DeptTel;
    private String DeptEmail;
    
    public void Department(){
    
    }
    
        public String getDeptEmail() {
            return DeptEmail;
        }
    
        public void setDeptEmail(String DeptEmail) {
            this.DeptEmail = DeptEmail;
        }
    
        public String getDeptHead() {
            return DeptHead;
        }
    
        public void setDeptHead(String DeptHead) {
            this.DeptHead = DeptHead;
        }
    
        public String getDeptName() {
            return DeptName;
        }
    
        public void setDeptName(String DeptName) {
            this.DeptName = DeptName;
        }
    
        public String getDeptTel() {
            return DeptTel;
        }
    
        public void setDeptTel(String DeptTel) {
            this.DeptTel = DeptTel;
        }
    
        public String searchDepartmentName()
        {
            //DatabaseUtility traveldatabase = new DatabaseUtility();
            String deptname = getDeptName();
          String select="SELECT * FROM Department WHERE DeptName="+"'"+deptname+"'"+";";
            //String result = traveldatabase.selectRecord(" "+"'"+deptname+"'"+";");
            //return result;
            return select;
            
        }
        }
    
    
    import org.apache.axis2.context.*;
    //import org.apache.axis2.engine.ServiceLifeCycle;
    //import javax.sql.*;
    import java.sql.*;
    //import ie.travel.databaseutility.DatabaseConnection; 
    public class DatabaseUtility2 extends DatabaseConnection {
       //private DatabaseConnection connect;
        private Connection conn;
    	public DatabaseUtility2(){
            
        }
    
    	public String selectRecord(String query)
         {
          String queryresult=null;
            conn = (Connection) MessageContext.getCurrentMessageContext().getProperty(DatabaseConnection.DB_CONNECTION);
    		
            try{
    		Statement stmt = conn.createStatement();
    		ResultSet result = stmt.executeQuery(query);
    
    		while(result.next())
    		 {
    			queryresult= " " + result.getString(1)+ " " + result.getString(2)+ " " + result.getString(3)+ " " + result.getString(4);
     		 }
    		}
    		catch(SQLException e){
    			 System.out.println("Error" + e);
    		}
    		if(queryresult==null)
    		{
    			queryresult="  "+query+ " Is Not Present In Travel 2.0";
    		}
    		return(queryresult);
    
        }
    }
    
    package ie.travel.databaseutility;
    
    /**
     *
     * @author Organ
     */
    
    import org.apache.axis2.context.ConfigurationContext;
    import org.apache.axis2.description.AxisService;
    import org.apache.axis2.engine.ServiceLifeCycle;
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    public class DatabaseConnection implements ServiceLifeCycle {
        public static final String DB_CONNECTION = "dbconnection";
    
        public void startUp(ConfigurationContext configctx, AxisService service) {
            try {
                String url = "jdbc:mysql://localhost:3306///Travel";
                String username="root";
                String password=" ";
                Connection conn = DriverManager.getConnection(url, username, password);
                configctx.setProperty(DB_CONNECTION, conn);
            } catch (Exception e) {
                System.out.println("Error establishing connection to Travel 2.0 Database"+e);
            }
        }
    
        public void shutDown(ConfigurationContext configctx, AxisService service) {
            Connection conn = (Connection) configctx.getProperty(DB_CONNECTION);
            if (conn != null) {
                try {
                    // closing the DB
                    conn.close();
                } catch (SQLException e) {
                    System.out.println("Error while closing Travel 2.0 Database"+e);
                }
            }
        }
    }
    

    I did the example just using normal jdbc it worked on commandline but would not work on apache axis 2

    My example is based on this example http://www.developer.com/db/article.php/3735771


Comments

  • Registered Users Posts: 1,998 ✭✭✭lynchie


    You've added a service.xml for your service to initialise your db connection? Have you verified it is called? A few logs / system.outs in your code will help u see if it is called.


  • Registered Users Posts: 269 ✭✭cyberwit


    I have the following as the xml service document. But where do i put it
    
    <service name="DatabaseUtility"
             class="databaseutility.DatabaseConnection">
       <description>Travel 2.0 Database/description>
          <messageReceivers>
             <messageReceiver
                mep="http://www.w3.org/2004/08/wsdl/in-only"
                class="org.apache.axis2.rpc.receivers.
                       RPCInOnlyMessageReceiver"/>
             <messageReceiver
                mep="http://www.w3.org/2004/08/wsdl/in-out"
                class="org.apache.axis2.rpc.receivers.
                       RPCMessageReceiver"/>
       </messageReceivers>
       <parameter name="ServiceClass">
          databaseutility.DatabaseUtility
       </parameter>
    </service>
    
    


  • Registered Users Posts: 1,998 ✭✭✭lynchie


    Have a look at the axis user guide but afaik you place it in the meta-inf folder of your aar file.


Advertisement