0

Java で準備済みステートメントを使用して選択クエリを実行しようとしています。Where 句では、以下に示すように Timestamp タイプ列の条件をチェックしています。

String selectSQL = "select * from db.keycontacts WHERE CREATEDDATETIME>?";
PreparedStatement preparedStatement = connect.prepareStatement(selectSQL);
preparedStatement.setTimestamp(1, convertStrToTimestamp(lastSyncTimeStamp));
resultSet = preparedStatement.executeQuery(selectSQL );

//timestampString を java.sql.Timestamp に変換する関数

private java.sql.Timestamp convertStrToTimestamp(String dateTimeStr){

      java.sql.Timestamp timeStampDate = null;
      try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//2015-05-11 18:26:55
            java.util.Date dateObj = (java.util.Date)formatter.parse(dateTimeStr);
            timeStampDate = new Timestamp(dateObj.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

      return timeStampDate;
  }

クエリを実行すると、次の例外が発生します。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1

それで、ここで正確にどこが間違っているのですか?

前もって感謝します。

4

1 に答える 1

3

からパラメータを削除します

resultSet = preparedStatement.executeQuery(selectSQL );

そしてに変更

resultSet = preparedStatement.executeQuery( );

渡されたクエリは、パラメータを設定していない単純な文字列 ( ) であるpreparedStatement.executeQuery(selectSQL );渡されたクエリよりも優先されるため、構文エラーが発生します。connect.prepareStatement(selectSQL);"select * from db.keycontacts WHERE CREATEDDATETIME>?"?

また、ステートメントはステートメントから継承されるPreparedStatement preparedStatement = connect.prepareStatement(selectSQL);ため、ステートメントは準備されていると言うことができます。これは、準備せずにクエリを実行します。executeQuery()

于 2015-05-14T06:15:13.753 に答える