-1
基本上我有一個存儲過程,它接受8個參數,並基於所有這些參數只執行查詢並給出輸出,但現在我有一種情況,即可以通過單值或多值,在1到8個參數之間的任何地方,它應該完美地工作,並且它應該基於已經傳遞的參數返回一些值。 我的StoredProcedure在MySQL中,我通過Java代碼中的CallableStatement調用它。 以下是我的StoredProcedure的片段。使用可選參數調用存儲過程
DELIMITER $$
CREATE PROCEDURE get_details(IN cus_name varchar(255),u_id int, ent_type varchar(255)
,branchId varchar(255), from_date datetime,to_date datetime, val varchar(255),rep_status varchar(255)
)
BEGIN
SELECT
r.id,r.parent_request_id,r.customer_master_id,
r.user_master_id,r.request_status,
cm.customer_name,
u.unique_id,
c.branch_id,c.created_date,c.entity_name,
c.entity_status,c.entity_type,c.no_of_documents,
c.no_of_parties,c.request_id,c.validity,c.itr_validity
FROM request as r
left join user_master as u on r.user_master_id = u.id
left join customer_master as cm on u.customer_master_id = cm.id
left join customer_entity as c on r.id= c.request_id
where
customer_name = cus_name and
unique_id = u_id and
entity_type = ent_type and
branch_id = branchId and
created_date >= from_date and
created_date <= to_date and
validity = val and
request_status = rep_status and
entity_status='parent';
END$$
DELIMITER ;
和下面是我的代碼來調用這個StoredProcedure的Java中
String query = "{ call get_details(?,?,?,?,?,?,?,?)}";
ResultSet resultSet = null;
List<SuperAdmin> admins = new ArrayList<SuperAdmin>();
try(
Connection connection = getconnection();
CallableStatement callableStatement = connection.prepareCall(query)){
if(ReportDTO.getCustomerName() != null){
callableStatement.setString(1, ReportDTO.getCustomerName());
}
if(ReportDTO.getUniqueId() != null){
callableStatement.setString(2, ReportDTO.getUniqueId());
}
if(ReportDTO.getEntity_type() != null){
callableStatement.setString(3, ReportDTO.getEntity_type());
}
if(ReportDTO.getBranchId() != null){
callableStatement.setString(4, ReportDTO.getBranchId().toString());
}
if(ReportDTO.getFilter_from() != null){
Date newFilterDate_from = dateFormat.parse(ReportDTO.getFilter_from().toString());
String newFormattedDate = dateFormat2.format(newFilterDate_from);
callableStatement.setString(5, newFormattedDate);
}
if(ReportDTO.getFilter_to() != null){
Date newFilterDate_to = (Date) dateFormat.parse(ReportDTO.getFilter_to().toString());
String newFormattedDateTo = dateFormat2.format(newFilterDate_to);
callableStatement.setString(6,newFormattedDateTo);
}
if(ReportDTO.getValidity() != null){
callableStatement.setString(7, ReportDTO.getValidity());
}
if(ReportDTO.getReport_status() != null){
callableStatement.setString(8, ReportDTO.getReport_status());
}
resultSet = callableStatement.executeQuery();
while(resultSet.next()){
SuperAdmin sAdmin = new SuperAdmin();
sAdmin.setRequest_status(resultSet.getString("request_status"));
sAdmin.setCustomer_entity_type(resultSet.getString("entity_type"));
sAdmin.setCustomer_entity_name(resultSet.getString("entity_name"));
sAdmin.setRequest_id(resultSet.getInt("id"));
sAdmin.setCustomer_entity_created_date(resultSet.getDate("created_date"));
sAdmin.setCustomer_entity_status(resultSet.getString("entity_status"));
sAdmin.setCustomer_entity_no_of_documents(resultSet.getString("no_of_documents"));
sAdmin.setCustomer_entity_no_of_parties(resultSet.getString("no_of_parties"));
sAdmin.setCustomer_entity_validity(resultSet.getString("validity"));
sAdmin.setCustomer_entity_itr_validity(resultSet.getString("itr_validity"));
sAdmin.setCustomer_entity_branch_id(resultSet.getLong("branch_id"));
admins.add(sAdmin);
}
}
任何幫助將是非常appreciable.Thanks提前做好。
你得到任何與此錯誤或你想要做什麼? –
存儲過程不支持可變數量的參數 – e4c5
@YCF_L是的,我確實得到 值java.sql.SQLException:沒有在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)爲參數1 \t規定值 \t在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) \t在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) \t在com.mysql.jdbc.SQLError.createSQLException( SQLError.java:920) – Mavericks