Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PreparedStatement在Java中未执行参数的最大查询_Java_Mysql_Swing_Jdbc_Prepared Statement - Fatal编程技术网

PreparedStatement在Java中未执行参数的最大查询

PreparedStatement在Java中未执行参数的最大查询,java,mysql,swing,jdbc,prepared-statement,Java,Mysql,Swing,Jdbc,Prepared Statement,我正在尝试执行一个最大的SQL和6个日期参数,并显示在JTable中,但它没有执行,我不知道实际的问题是什么 但令人困惑的是,我能够使用语句界面使用固定参数执行相同的查询,并在JTable中显示,但无法使用PreparedStatement 我的源代码: public String start_date; public String end_date; public void getMonthWithDropCombobox() { if(month_s

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

但令人困惑的是,我能够使用
语句
界面使用固定参数执行相同的查询,并在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
        }
        }

    }
有日期的
setDate()
而不是
setString()

ps_tsas.setDate(1, java.sql.Date.valueOf(start_date));
具有真实日期的示例:

ps_tsas.setDate(1, java.sql.Date.valueOf("2017-05-22"));

其他日期也一样。

不要只接受异常(//TODO:handle exception)。至少打印出异常消息+堆栈跟踪。对于异常消息,帮助也会容易得多(假设抛出异常),查询中日期的字符串表示可能与DB端的预期格式不匹配。尝试使用
PreparedStatement
setDate(int-parameterIndex,java.sql.Date)
方法。我不知道如何感谢您,但是,我真的很高兴您帮助了我,这对我来说是+1,但我有点困惑,在我之前准备的所有语句中,我对所有的日期参数都使用了setString,但是为什么它不适用于这个特定的查询。有没有合理的答案