首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中不执行参数的PreparedStatement最大查询

问在Java中不执行参数的PreparedStatement最大查询
EN

Stack Overflow用户
提问于 2017-05-02 12:30:02
回答 1查看 64关注 0票数 1

我正在尝试执行一个最大的SQL,使用6个日期参数并在JTable中显示,但它没有执行,我不知道实际问题是什么。

但令人困惑的是,我能够使用Statement接口执行固定参数的相同查询,并在JTable中显示,但无法使用PreparedStatement显示。

我的源代码:

代码语言:javascript
复制
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
        }
        }

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-02 12:41:56

而不是setString(),而是用于日期的setDate():

代码语言:javascript
复制
ps_tsas.setDate(1, java.sql.Date.valueOf(start_date));

实际日期示例:

代码语言:javascript
复制
ps_tsas.setDate(1, java.sql.Date.valueOf("2017-05-22"));

其他日期也一样。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43737953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档