Answer the question
In order to leave comments, you need to log in
How to get up on the correct data retrieval from the database?
To obtain information from the database in 90% of cases, the following scheme of the method is used:
public static String getAnything() throws SQLException, FrameworkException {
OraclePreparedStatement preparedStatement = null;
OracleConnection connection = (OracleConnection) TransactionScope.getConnection();
ResultSet resultSet = null;
try {
String sql = ""; // SQL запрос
preparedStatement = (OraclePreparedStatement)connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
String anything = "";
while (resultSet.next()){
anything = resultSet.getString(1); // Получаем нужные данные
}
return anything;
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
if (resultSet != null)
resultSet.close();
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
TransactionScope.releaseConnection(connection);
}
}
getString(String sql, String...binds) { // binds - переменные, которые подставятся в SQL запрос
}
getArray(String sql, String...binds) {
}
Answer the question
In order to leave comments, you need to log in
You can rewrite on Spring JdbcTemplate
a rather convenient thing (and it will be easy for you to switch to it)
(in the latest version, you can make specific parameters instead of "?", which is convenient)
SQL can be taken out to a file, but it is often more convenient when they are in the code. Look at the situation.
From the point of view of further frequent refactoring of the base, it is better to use JPA (ala Hibernate),
but it is not always easy to write the desired query there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question