Answered step by step
Verified Expert Solution
Question
1 Approved Answer
* * * * * Below is what I am having trouble translating to java * * * * * * * - - Query
Below is what I am having trouble translating to java
Query #
SELECT pet.petName, COUNTservicesserviceID
FROM PET
JOIN VISITS
ON pet.petID visits.petID
JOIN SERVICES
ON services.serviceID visits.serviceID
GROUP BY pet.petname
ORDER BY COUNTservicesserviceID DESC
LIMIT ;
Query #
SELECT pet.petName, SUMservicesserviceCost
FROM PET
JOIN VISITS
ON pet.petID visits.petID
JOIN SERVICES
ON services.serviceID visits.serviceID
GROUP BY pet.petname
ORDER BY SUMservicesserviceCost DESC
LIMIT ;
Current code I have below
import java.sql;
public class GallagherJDBC
public static void mainString args throws Exception
try
connect to database
Class.forNameorgpostgresql.Driver";
Connection conn DriverManager.getConnectionjdbc:postgresql:localhost:AnimalHospital "postgres", ;
System.out.printlnConnected to the database successfully!";
drop tables if they exists
dropTableconn;
create and populate tables
createTableconn;
queries data from selected table and criteria
queryDataconn;
catch Exception e
TODO: handle exception
eprintStackTrace;
System.err.printlnegetClassgetName: egetMessage;
private static void queryDataConnection conn
System.out.printlnRunning query!";
try
Statement stmt conn.createStatement;
String queryStmt "select pet.petName, countservicesserviceId
"from pet
"join visits
on pet.petID visits.serviceID
"join services
on services.serviceID visits.serviceID
"group by pet.petName
"order by countservicesserviceID desc
"limit ;
ResultSet rs stmtexecuteQueryqueryStmt;
System.out.printlnPetnametCount;
while rsnext
Instructions
Create a database in PGAdmin named AnimalHospital.
Use Query Editor in PGAdmin to open the Attached SQL file.
The logic in the SQL file creates, populates, joins, and queries the following tables: PET, SERVICES, and VISITS.
Create a java source file that uses JDBC and methods to implement the logic from the attached sql file. Use methods to create recreate,
populate repopulate, and query the tables.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started