Answered step by step
Verified Expert Solution
Link Copied!

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 #1
SELECT pet.petName, COUNT(services.serviceID)
FROM PET
JOIN VISITS
ON pet.petID = visits.petID
JOIN SERVICES
ON services.serviceID = visits.serviceID
GROUP BY pet.petname
ORDER BY COUNT(services.serviceID) DESC
LIMIT 3;
-- Query #2
SELECT pet.petName, SUM(services.serviceCost)
FROM PET
JOIN VISITS
ON pet.petID = visits.petID
JOIN SERVICES
ON services.serviceID = visits.serviceID
GROUP BY pet.petname
ORDER BY SUM(services.serviceCost) DESC
LIMIT 3;
****************************************************************************
**************Current code I have below****************************
import java.sql.*;
public class GallagherJDBC2{
public static void main(String[] args) throws Exception {
try {
//1. connect to database
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/AnimalHospital", "postgres", "9271");
System.out.println("Connected to the database successfully!");
//2. drop tables if they exists
dropTable(conn);
//3. create and populate tables
createTable(conn);
//4. queries data from selected table and criteria
queryData(conn);
} catch (Exception e){
// TODO: handle exception
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+ e.getMessage());
}
}
private static void queryData(Connection conn){
System.out.println("Running query!");
try {
Statement stmt = conn.createStatement();
String queryStmt = "select pet.petName, count(services.serviceId)"+
"from pet "+
"join visits "+
"on pet.petID = visits.serviceID "+
"join services "+
"on services.serviceID = visits.serviceID "+
"group by pet.petName "+
"order by count(services.serviceID) desc "+
"limit 3";
ResultSet rs = stmt.executeQuery(queryStmt);
System.out.println("Petname"+"\tCount");
while (rs.next()){
}
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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions