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;
}
}