Answer the question
In order to leave comments, you need to log in
How to create a method from a SQL query?
This query in PostrgreSQL returns true if there is such a record in the database and false if not:
SELECT count(*) <> 0 FROM customers
WHERE name = 'Bob' AND purchase = 'bike'
Answer the question
In order to leave comments, you need to log in
almost the same as:
ResultSet resultSet = statement.executeQuery("SELECT count(*) as cnt FROM customers WHERE name = 'Bob' AND purchase = 'bike'");
resultSet.next(); // not first() with PGSL JDBC it is forward only cursor
final int cnt = resultSet.getInt(1);
System.out.println("cnt: " + cnt);
String query = "SELECT count(*) <> 0 FROM accounts WHERE username = 'bob';";
System.out.println("Executing count query: " + query);
ResultSet resultSet = connection.createStatement().executeQuery(query);
resultSet.next();
final boolean status = resultSet.getBoolean(1);
System.out.println("status: " + status);
Well, perhaps you should clarify whether you are working with jdbc or with Spring JDBC, Spring JPA, Hibernate, etc.
Spring has such a thing as exists*
For example,
existsCustomerByNameAndPurchase(String name, String purchase);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question