1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.trifulcas.country; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Pagos2006 { public static void main(String[] args) { // Calcula los pagos realizados en 2006 y muestralo por la consola // Paso 1: SQL me voy al workbench String sql = "SELECT sum(amount) total FROM payment where year(payment_date)=2006 group by year(payment_date)" ; // Paso 2, monto la conexión y toda la mandanga try { Class.forName( "com.mysql.cj.jdbc.Driver" ); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/sakila" , "root" , "" ); // Es lo mismo usar normal o preparado PreparedStatement st = con.prepareStatement(sql); // No añado parámetros, directo al resultset ResultSet rs = st.executeQuery(); if (rs.next()) { Double total = rs.getDouble( "total" ); System.out.println(total); } else { System.out.println( "No hay pagos en esa fecha" ); } } catch (Exception ex) { System.out.println(ex.getMessage()); } } } |