0

I have a problem with retrieving data from databases that I created, the condition is that the data that the user has input would depend on the EmployeeCode that they typed in (A,B,or C) if they typed "A" as their code, their data inputs should proceed to Database A, so I created three databases on mysql for different codes (A,B,C). the problem is the InsertRecord() is functioning well because the data inputs of the user is going to the correct Database where it should suppose to go but the GetAllRecords() is not functioning.

private String sqlInsert(){
    if(employeeCode.equals("A")){
        return "insert into MachineProblem2A(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)";
    }else if(employeeCode.equals("B")){
        return "insert into MachineProblem2B(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)";
    }else if(employeeCode.equals("C")){
        return "insert into MachineProblem2C(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)";
    } return null;

}

    private void insertRecord(Connection conn){

    try{


        PreparedStatement pstmnt= conn.prepareStatement(sqlInsert());
        pstmnt.setString(1, employeeName);
        pstmnt.setString(2, employeeCode);
        pstmnt.setDouble(3, employeeSales);
        pstmnt.setDouble(4, gross);
        pstmnt.setDouble(5, commission);
        pstmnt.setDouble(6, result);

        //now commit to database
        pstmnt.executeUpdate();

    }catch(SQLException sqle){
        sqle.printStackTrace();
    }

}

insertRecord() is functioning well because whenever a user input data, it's going through the correct database whether it is MachineProblem2A,B,or C.

but on my GetAllRecords(), i always received a null pointer exception error. I have no idea what went wrong since the function sqlInsert that I created is working and it is similar to sqlRetrieve but it is not working.

private String sqlRetrieve(){
    if(employeeCode.equals("A")){
        return "select*from MachineProblem2A";
    }else if(employeeCode.equals("B")){
        return "select*from MachineProblem2B";
    }else if(employeeCode.equals("C")){
        return "select*from MachineProblem2C";
     else return null;


}

public ResultSet getAllRecords(Connection conn){
    ResultSet records = null;

    try{

        String sql=sqlRetrieve();
        PreparedStatement pstmnt= conn.prepareStatement(sql);

        records= pstmnt.executeQuery();


    }catch(SQLException sqle){
        sqle.printStackTrace();
    }
    return records;
}

**EDIT **

How come employeeCode is NULL whenever i use it on GetAllRecords?? I tried to declare a string and concatenate it to my prepared statement and it worked, but calling employeeCode gives me null.

String Try="A";
// get records

public ResultSet getAllRecords(Connection conn){
    ResultSet records = null;

    try{


        PreparedStatement pstmnt= conn.prepareStatement("select * from MachineProblem2".concat(Try));

    //IF I TRIED USING "select * from MachineProblem2".concat(employeeCode) or +employeeCode it won't work. //

        records= pstmnt.executeQuery();


    }catch(SQLException sqle){
        sqle.printStackTrace();
    }
    return records;
}

but works perfectly on other functions that I created including the insertRecord

public void computeGross(){

    switch(employeeCode){
    case "A":
        setGross(grossA+(getEmployeeSales()*grossSalesA));
        break;
    case "B":
        setGross(grossB+(getEmployeeSales()*grossSalesB));  
        break;
    case "C":
        setGross(grossC+(getEmployeeSales()*grossSalesC));  
        break;
    }   

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ZZZZZZZZZ
  • 197
  • 2
  • 10
  • Your `sqlRetrieve` method returns either an invalid SELECT statement (`select*from...`) or an INSERT statement. This doesn't look right. – Eran Sep 11 '16 at 08:51
  • I copied the wrong code, sorry about that. problem still exists tho, i still always get null pointer exception for my getAll Records. – ZZZZZZZZZ Sep 11 '16 at 08:56
  • `select*from` is invalid. Add spaces - `select * from ` – Eran Sep 11 '16 at 08:57
  • error still persists, im getting null pointer exception on my if else statement in sqlRetrieve() i don't know why because it works perfectly fine on my sqlInsert() – ZZZZZZZZZ Sep 11 '16 at 09:01
  • 1
    Are you sure `employeeCode` is not null? – Eran Sep 11 '16 at 09:02
  • No,im pretty sure about that since InsertRecord is working and im getting data on my databases, i just have a problem with retrieving them for display. – ZZZZZZZZZ Sep 11 '16 at 09:04
  • can you post the full stacktrace of the NPE ? Also note that the only thing that can be null in `sqlRetrieve` (and provoke a NPE) is `employeeCode`. –  Sep 11 '16 at 09:21
  • java.lang.NullPointerException edu.ust.iics.employee.model.EmployeeBean.sqlRetrieve(EmployeeBean.java:185) edu.ust.iics.employee.model.EmployeeBean.getAllRecords(EmployeeBean.java:203) edu.ust.iics.employee.controller.EmployeeHistoryServlet.doPost(EmployeeHistoryServlet.java:35) edu.ust.iics.employee.controller.EmployeeHistoryServlet.doGet(EmployeeHistoryServlet.java:28) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) – ZZZZZZZZZ Sep 11 '16 at 09:23
  • I have no clue why employeeCode would be null in sqlRetrieve because it's working on sqlInsert – ZZZZZZZZZ Sep 11 '16 at 09:24
  • Log `employeeCode` in `sqlRetrieve()` as first statement. See it's value. It'll make sure if it is `null`. – Balkrishna Rawool Sep 11 '16 at 10:13
  • im sorry but what do you mean by Log? – ZZZZZZZZZ Sep 11 '16 at 10:19
  • How did this happen? I declared and initialized a string named Try="A"; and concatenated it to my prepared statement, PreparedStatement pstmnt= conn.prepareStatement("select * from MachineProblem2".concat(Try)); the employeeCode is NULL. but only on this part. employeeCode is not null on my other functions including SQLInsert and InsertRecord. – ZZZZZZZZZ Sep 11 '16 at 10:27

0 Answers0