Reputation: 99
I wanna ask about how to get message response from PostgreSQL when we execute a query.
Message response from PostgreSQL what I mean are like this, for example:
I am using JDBC.
I have tried to get it from executeUpdate()
from Statement class. but it's only return int that representing whether the query executed successfully or not.
Is there any way to do it?
Upvotes: 3
Views: 4425
Reputation: 2032
I am not sure whether you get statements like
Query returned successfully: 1 row affected, 11 ms execution time.
but you have alternative to use like statement.executeUpdate()
which returns the the no of rows that have been affected by the execution of the DML statement.
Coming to the other statement
ERROR: duplicate key value violates unique constraint "pk_grouptab"
Whenever an error has occurred in the database, a SQLException
is thrown, you need to explicitly catch the exception and handle it. SQLException Api in addition, please check this link where it briefly describes exception handling in JDBC.
If you want to check if there were any warnings after you execute a query you can use getWarnings
and use methods like getMessage()
to get the message of the warning.
Below is a skeleton
try {
// execute sql statements here
log.info(statement.getWarnings().getMessage());
}
catch(SQLException ex) {
// an exception has occurred get all info related to the exception
log.info(ex.getErrorCode());
log.info(ex.getMessage());
ex.printStackTrace();
}
finally {
// close all open connections
}
Hope this helps you.
Upvotes: 3