N
N
Nikolay Baranenko2017-08-20 23:24:24
Java
Nikolay Baranenko, 2017-08-20 23:24:24

Why is the error ORA-01008: not all variables bound?

Hello.
trying to send parametrized select

String sql=
"select *\n"+
"FROM EVENTS a \n"+
" where \n"
"   a.TIME_RECEIVED BETWEEN ? AND ?\n"
" and a.original_data like ?\n"
" order by a.time_created"

        try (Connection conn = DriverManager.getConnection(DB_URL_Connection, DB_UserName, DB_Password)) {
            PreparedStatement prepStmt = conn.prepareStatement(sql);
            prepStmt.setDate(1, new java.sql.Date(fDATETIME.getTime()));
            prepStmt.setDate(2, new java.sql.Date(tDATETIME.getTime()));
            prepStmt.setString(3, "%Хост: %");
            ResultSet result = prepStmt.executeQuery(sql);

Why does the error occur in this case
Exception in thread "main" java.lang.RuntimeException: ORA-01008: not all variables are bound
and how to solve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baranenko, 2017-08-21
@drno-reg

the solution looks like this
changed the data type to TimeStamp and sql was superfluous in ResultSet result = prepStmt.executeQuery(sql);

Calendar fDATETIME = Calendar.getInstance(); // creates calendar
        Calendar tDATETIME = Calendar.getInstance();
        SimpleDateFormat ShortDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        SimpleDateFormat DateFormatForSQLOracle = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        fDATETIME.setTime(ShortDateFormat.parse(DATETIME_FROM));
        tDATETIME.setTime(ShortDateFormat.parse(DATETIME_TO));

        try (Connection conn = DriverManager.getConnection(DB_URL_Connection, DB_UserName, DB_Password)) {
            PreparedStatement prepStmt = conn.prepareStatement(sql);
            prepStmt.setTimestamp(1, java.sql.Timestamp.valueOf(DateFormatForSQLOracle.format(fDATETIME.getTime())));
            prepStmt.setTimestamp(2, java.sql.Timestamp.valueOf(DateFormatForSQLOracle.format(tDATETIME.getTime())));
            prepStmt.setString(3, "%Хост: %");

            ResultSet result = prepStmt.executeQuery();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question