Q10.java 1.2 KB
Newer Older
Matias Ferreira committed
1 2 3 4 5 6 7 8 9 10 11 12

import java.sql.*;


public class Q10 {

	public static void main( String [] args ) {
		String url = "jdbc:postgresql://localhost:5433/testdb";
		String user = "test";
		String password = "pqntslc";
		try {
			/* QUERY
13 14
			 * select distinct fullname
			 * from ( (enrollments en
Matias Ferreira committed
15 16
			 * left join students st)
			 * on en.student_iid = st.student_iid) as en_st
17
			 * left join courses co
Matias Ferreira committed
18 19
			 * on en_st.course_iid = co.course_iid) as en_st_co
			 * where en_st_co.code = 'a1010';
20
			 *  */
Matias Ferreira committed
21 22 23 24 25 26 27 28 29
			Connection connection = DriverManager.getConnection(url, user,password);
			Statement st = connection.createStatement();
			String query = "SELECT DISTINCT fullname "
							+ "FROM ((enrollments en "
							+ "LEFT JOIN students st "
							+ "ON en.student_iid = st.student_iid) AS en_st "
							+ "LEFT JOIN courses co "
							+ "ON en_st.course_iid = co.course_iid) AS en_st_co "
							+ "WHERE en_st_co.code = 'a1010';";
30

Matias Ferreira committed
31
			ResultSet rs = st.executeQuery(query);
32
			System.out.println("Imprimiendo la lista de los alumnos del curso con codigo \'a1010\' ");
Matias Ferreira committed
33 34 35 36 37 38 39 40 41
			while(rs.next() ) {
				String fullname = rs.getString("fullname");
				System.out.println(fullname);
			}
		}catch(SQLException e) {
			e.printStackTrace();;
		}
	}
}