我没有说明我准备的陈述

时间:2017-01-20 01:42:09

标签: mysql jdbc prepared-statement

我正在对数据库预处理语句进行查询,但它没有正确显示。

我打印声明时收到此错误。

  

com.mysql.jdbc.JDBC42PreparedStatement@157b62f9:SELECT * FROM 2015-wind WHERE TimeStamp BETWEEN'2015-01-01'''2015-01-25'EN ConnectingArea IN (**未指明**)

10YAT-APG - L(我打印我的字符串,它给我一个输出)。

有谁知道这里发生了什么?

public List<Wind2015> getResultsWind(String beginDate1, String endDate1, String[] connectingAreas1) throws Exception{
    int count = 0;
    List<Wind2015> myWind2015s = new ArrayList<>();

    SimpleDateFormat readFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
            Locale.ENGLISH);
    Date date2 = readFormat.parse(beginDate1);
    Date date3 = readFormat.parse(endDate1);


    String beginDate = new SimpleDateFormat("yyyy-MM-dd").format(date2);
    String endDate = new SimpleDateFormat("yyyy-MM-dd").format(date3);

    ArrayList<String> connectingArea = new ArrayList<>(Arrays.asList(connectingAreas1));

    StringBuilder inputs = new StringBuilder();

    for (int i = 0; i < connectingArea.size(); i++) {
        if (i < connectingArea.size()-1) {
            inputs.append("?,");
        } else {
            inputs.append("?");
        }   
    }


    String connectingAreaInputs = inputs.toString();

    Connection connection = null;
    PreparedStatement prepareStatement = null;
    ResultSet myRs = null;

    System.out.println(connectingAreaInputs);

    try {
        connection = getConnection();

        String sql = "SELECT * FROM `2015-wind` WHERE `TimeStamp` BETWEEN ? AND ? AND `ConnectingArea` IN ("+ connectingAreaInputs +")";

        prepareStatement = connection.prepareStatement(sql);


        prepareStatement.setString(count+=1,beginDate);
        prepareStatement.setString(count+=1, endDate);

        System.out.println(prepareStatement);
        for (String string : connectingArea) {
             System.out.println(string);
            count+=1;
            prepareStatement.setString(count, string);
        }



        myRs = prepareStatement.executeQuery();

        Wind2015 wind2015 = null;

        while (myRs.next()) {

            String timeStamp = myRs.getString("Timestamp");
            String connectingArea1 = myRs.getString("ConnectingArea");
            String value = myRs.getString("ActualWindEnergy");

            wind2015 = new Wind2015(timeStamp, value, connectingArea1);

            myWind2015s.add(wind2015);
        }

        return myWind2015s;

    } finally {
        close(connection, prepareStatement, myRs);
    }

}

1 个答案:

答案 0 :(得分:0)

您正在使用以下行打印预准备语句:

System.out.println(prepareStatement);
在之前

将值分配给IN (...)表达式中的动态占位符,因此它们(正确地)显示为“未[指定]”。

之前 之后的之后将移动到