PreparedStatement 最大查询,参数未在 Java 中执行
Posted
技术标签:
【中文标题】PreparedStatement 最大查询,参数未在 Java 中执行【英文标题】:PreparedStatement Largest Query with Parameter Not Executing in Java 【发布时间】:2017-09-29 23:32:48 【问题描述】:我正在尝试执行一个最大的 SQL 并使用 6 个日期参数并显示在 JTable
但它没有执行,我不知道实际问题是什么。
但令人困惑的是我能够使用Statement
接口执行具有固定参数的相同查询并显示在JTable中,但无法使用PreparedStatement
显示
我的源代码:
public String start_date;
public String end_date;
public void getMonthWithDropCombobox()
if(month_sands.getSelectedIndex() != -1)
int monthnumber = month_sands.getSelectedIndex() + 1;
if(monthnumber>=9)
month=String.valueOf(monthnumber);
else
month="0"+String.valueOf(monthnumber);
String dateString = year_sands.getSelectedItem().toString()+"-"+month+"-"+"01";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.US);
LocalDate date = LocalDate.parse(dateString, dateFormat);
LocalDate startDate=date.withDayOfMonth(1);
LocalDate endDate = date.withDayOfMonth(date.getMonth().maxLength());
start_date=startDate.toString();
end_date=endDate.toString();
public void getAllVendorDetails()
getMonthWithDropCombobox();
StockAndSales sas1=new StockAndSales();
if(company_dropdown_sas.getSelectedItem().equals("View All Vendors"))
try
String totalStockAndSales="select '' as Vendor_Company, '', product, price,sellprice, openingstock as openingStock, "
+ "openingstock*price as op_value, receipts as receipts, receipts*price as re_value, totalstock as totalstock, "
+ "totalstock*price as ts_value, sales as sales, sales*sellprice as s_value, return as returns,return*sellprice "
+ "as rt_value, closingstock as closingstock, closingstock*price as cl_value from purchase_table where date "
+ "between ? and ? "
+ "union all select orgname , 'TOTAL', '', sum(price),sum(sellprice), sum(openingstock) as openingStock, "
+ "sum(openingstock*price) as op_value, sum(receipts) as receipts, sum(receipts*price) as re_value, sum(totalstock) "
+ "as totalstock, sum(totalstock*price) as ts_value, sum(sales) as sales, sum(sales*sellprice) as s_value, sum(return) "
+ "as returns,sum(return*sellprice) as rt_value, sum(closingstock) as closingstock, sum(closingstock*price) as cl_value "
+ "from purchase_table where date "
+ "between ? and ? "
+ "group by orgname union all select 'Grand Total' , '', '',sum(price),sum(sellprice), sum(openingstock) as "
+ "openingStock, sum(openingstock*price) as op_value, sum(receipts) as receipts, sum(receipts*price) as re_value, "
+ "sum(totalstock) as totalstock, sum(totalstock*price) as ts_value, sum(sales) as sales, sum(sales*sellprice) as "
+ "s_value, sum(return) as returns,sum(return*sellprice) as rt_value, sum(closingstock) as closingstock, "
+ "sum(closingstock*price) as cl_value from purchase_table where date "
+ "between ? and ? "
+ "orde0r by closingstock asc";
PreparedStatement ps_tsas=connection.prepareStatement(totalStockAndSales);
ps_tsas.setString(1, start_date);
ps_tsas.setString(2, end_date);
ps_tsas.setString(3, start_date);
ps_tsas.setString(4, end_date);
ps_tsas.setString(5,start_date);
ps_tsas.setString(6, end_date);
ResultSet set_tsas=ps_tsas.executeQuery();
sas1.table_sas.setModel(DbUtils.resultSetToTableModel(set_tsas));
sas1.show_month_year_sas.setText("All Vendors Stock and Sales");
sas1.name_of_user.setText(user_name_display.getText());
sas1.setVisible(true);
return;
catch (Exception e)
// TODO: handle exception
【问题讨论】:
不要只吞下异常(// TODO:处理异常)。至少打印出异常消息+堆栈跟踪。有了异常消息,帮助也容易得多(假设抛出异常) 查询中日期的字符串表示可能与数据库端的预期格式不匹配。尝试使用PreparedStatement
的setDate(int parameterIndex, java.sql.Date)
方法。
【参考方案1】:
日期用setDate()
代替setString()
:
ps_tsas.setDate(1, java.sql.Date.valueOf(start_date));
以实际日期为例:
ps_tsas.setDate(1, java.sql.Date.valueOf("2017-05-22"));
其他日期相同。
【讨论】:
我不知道如何感谢你,但我真的很高兴你帮助了我,这对我有用 +1,但我在这里有点困惑,在我之前的所有准备好的声明中,我使用了 setString对于它工作的所有日期参数,但为什么它不适用于这个特定的查询。任何合理的答案以上是关于PreparedStatement 最大查询,参数未在 Java 中执行的主要内容,如果未能解决你的问题,请参考以下文章
选择查询中的 JDBC PreparedStatement 和参数(?)