Q12.java 1.92 KB
Newer Older
Matias Ferreira committed
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package com.roshka.jdbc;

import java.util.Scanner;
import java.sql.*;


public class Q12 {

	public static void main( String [] args ) {
		String url = "jdbc:postgresql://localhost:5433/testdb";
		String user = "test";
		String password = "pqntslc";
		Scanner entrada = new Scanner(System.in);
		String codigo;
		try {
			/* QUERY
			 * SELECT fullname, name, code, paid
			 * FROM (enrollments en
			 * LEFT JOIN courses co
			 * ON en.course_iid = co.course_iid) AS en_co
			 * LEFT JOIN students st
			 * ON en_co.student_iid = st.student_iid
			 * where paid = 'false' and code=codigo;
			 *  */ 
			
			Connection connection = DriverManager.getConnection(url, user,password);
			System.out.println("estoy conectado a la base de datos");
			Statement st = connection.createStatement();
			System.out.println("Ingrese el codigo del curso: ");
			codigo = entrada.nextLine();
			String query = "SELECT fullname, name, enrollment_iid, paid "
							+ "FROM (enrollments en "
							+ "LEFT JOIN courses co "
							+ "ON en.course_iid = co.course_iid) AS en_co "
							+ "LEFT JOIN students st "
							+ "ON en_co.student_iid = st.student_iid "
							+ "WHERE paid = 'false' AND code='"+codigo+"';";
			
			ResultSet rs = st.executeQuery(query);
			while(rs.next() ) {
				String paid = rs.getString("paid");
				String nombre = rs.getString("fullname");
				String enrollment_iid = rs.getString("enrollment_iid");
				System.out.println("EL alumno "+nombre+" ya ha pagado? 's' o 'n'");
				String respuesta = entrada.next();
				if(respuesta.equals("s") ) {
					String update = "UPDATE enrollments "
										+ "SET paid = 'true', "
										+ "payment_date = CURRENT_DATE "
										+ "WHERE enrollment_iid = '"+enrollment_iid+"';";	
					st.executeUpdate(update);
					System.out.println("se ha actualizado la base de datos");
				}
			}
			
			System.out.println("termino la actualizacion");
		}catch(SQLException e) {
			e.printStackTrace();;
		}
	}
}