N
N
Nikolay Baranenko2017-08-21 09:42:50
Java
Nikolay Baranenko, 2017-08-21 09:42:50

What would be the best way to use this method?

Hello.
Recently, I began to get rid of the so-called spaghetti code in JSP and switch to a separate front end and back end
as a result of transformations I came to this look on the back end

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));

String sql="select * from EVENTS a where a.TIME_CREATE between ? and ? and lower(a.original_data) like ?";
        try
        {
            Class.forName(ClassDriverName);
        } catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e.getMessage());
        }
        try (Connection conn = DriverManager.getConnection(DB_URL_Connection, DB_UserName, DB_Password)) {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setTimestamp(1, java.sql.Timestamp.valueOf(DateFormatForSQLOracle.format(fDATETIME.getTime())));
            preparedStatement.setTimestamp(2, java.sql.Timestamp.valueOf(DateFormatForSQLOracle.format(tDATETIME.getTime())));
                    preparedStatement.setString(3, "%Хост :%".toLowerCase())
            }
            ResultSet result = preparedStatement.executeQuery();
            while (result.next()) {
                System.out.println(result.getString("ID"));
                sql_EVENT_OK = "                select to_char(t.ELT) as ELT, to_char(t.IDX) as IDX /*, t.**/\n" +
                        "                from\n" +
                        "                        EVENT_CUSTOM_ATTRIBUTES t\n" +
                        "                where\n" +
                        "                t.event_id=?\n" +
                        "        and t.idx=?\n";
                preparedStatement = conn.prepareStatement(sql_EVENT_OK);
                preparedStatement.setString(1, result.getString("ID"));
                preparedStatement.setString(2, "StateChangedByEvent");
                ResultSet result_ELT= preparedStatement.executeQuery();
                while (result_ELT.next()) {
                    sql_EVENT_OK="        SELECT *\n" +
                            "        FROM EVENTS a\n" +
                            "        where\n" +
                            "        a.id=?";
                    preparedStatement = conn.prepareStatement(sql_EVENT_OK);
                    preparedStatement.setString(1, result_ELT.getString("ELT"));
                    ResultSet result_EVENT_OK = preparedStatement.executeQuery();
                    while (result.next()) {
                        System.out.println(result_EVENT_OK .getTimestamp("TIME_CREATED"));
                    }
                }
conn.close();

What is the best way to use this method?
I'm waiting for criticism...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2017-08-21
@drno-reg

  • Use an ORM, or at least Spring JDBC, so you don't mess around with joins, type conversions, etc.
  • Learn to test your code with unit and integration tests. You know what the code should do, so you can write tests.
  • No need:
    try
            {
                Class.forName(ClassDriverName);
            } catch (ClassNotFoundException e)
            {
                throw new RuntimeException(e.getMessage());
            }

    This code is not optimal. Never.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question