Q10.java 1.2 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
package com.roshka.jdbc;

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
			 * 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';
			 *  */ 
			
			Connection connection = DriverManager.getConnection(url, user,password);
			System.out.println("estoy conectado a la base de datos");
			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';";
			
			ResultSet rs = st.executeQuery(query);
			while(rs.next() ) {
				String fullname = rs.getString("fullname");
				System.out.println(fullname);
			}
		}catch(SQLException e) {
			e.printStackTrace();;
		}
	}
}