我有一个Web应用程序,需要根据用户的输入将其数据存储在特定的表中。例如如果用户输入Code" A"他们的数据将保存到DatabaseA,而如果用户输入Code" B",数据将保存到DatabaseB。我需要知道是否有办法操作预准备语句,以便将代码连接到预准备语句字符串的末尾。
我有3个表,MachineProblem2A,2B和2C。有没有办法简单地将代码连接到我的sql字符串的末尾?
String sql="select * from MachineProblem2";
PreparedStatement pstmnt= conn.prepareStatement(sql);
我尝试过不同的方式,例如"从MachineProblem2" + employeeCode中选择* 但它不起作用。我试过"从MachineProblem2" .concat(employeeCode)中选择*它不能正常工作。 我甚至尝试过在条件语句存在的情况下创建的函数,它依赖于employeeCode,例如: if(employeeCode.equals(" A")返回"从MachineProblem2A中选择*"
所有这些都给了我一个空指针异常或 java.lang.InstantiationException:bean employeeRecords未在范围内找到错误。
我在sqlRet()和getAllRecords()上获得Null指针,但数据输入正确存储在指定的表中。我在检索输出时遇到问题。
private String sqlInsert(){
if(employeeCode.equals("A") && employeeSales > 2500){
return "insert into MachineProblem2A(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)";
}else if(employeeCode.equals("B") && employeeSales > 2000){
return "insert into MachineProblem2B(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)";
}else if(employeeCode.equals("C") && employeeSales > 1500){
return "insert into MachineProblem2C(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)";
} return null;
}
private String sqlRet(){
if(employeeCode.equals("A") && employeeSales > 2500){
return "select * from MachineProblem2A";
}else if(employeeCode.equals("B") && employeeSales > 2000){
return "select * from MachineProblem2B";
}else if(employeeCode.equals("C") && employeeSales > 1500){
return "select * from MachineProblem2C";
} 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();
}
}
// get records
public ResultSet getAllRecords(Connection conn){
ResultSet records = null;
try{
PreparedStatement pstmntA= conn.prepareStatement(sqlRet());
records= pstmntA.executeQuery();
}catch(SQLException sqle){
sqle.printStackTrace();
}
return records;
}
//错误//
java.lang.NullPointerException
edu.ust.iics.employee.model.EmployeeBean.sqlRet(EmployeeBean.java:183)
edu.ust.iics.employee.model.EmployeeBean.getAllRecords(EmployeeBean.java:225)
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)
答案 0 :(得分:0)
您可以使用以下查询
SELECT T1.COLUMN_NAME || T2.COLUMN_NAME || T3.COLUMN_NAME
FROM
TABLE_1 T1, TABLE_2 T2, TABLE_3 T3
WHERE T1.PRIMARY_KEY_COLUMN = T2.PRIMARY_KEY_COLUMN
AND
T1.PRIMARY_KEY_COLUMN = T3.PRIMARY_KEY_COLUMN
告诉我你需要它作为sql查询或java