diff --git a/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/Driver.java b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/Driver.java new file mode 100644 index 00000000..4065a9e9 --- /dev/null +++ b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/Driver.java @@ -0,0 +1,57 @@ +package com.topic4.choco; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class Driver { + + private static Connection con = null; + private static Driver instance = null; + + private Driver() { + try { + String driver = "com.mysql.jdbc.Driver"; + String url = "jdbc:mysql://127.0.0.1/high_school"; + String pwd = "root"; + String usr = "root"; + Class.forName(driver); + + con = DriverManager.getConnection(url, usr, pwd); + + } catch (SQLException ex) { + + System.err.println(ex.getMessage()); + + } catch (ClassNotFoundException ex) { + + System.err.println(ex.getMessage()); + + } + } + + public static synchronized Driver getInstance() { + if (instance == null) { + instance = new Driver(); + System.out.println("Conexion Realizada!!"); + } + return instance; + } + + public static Connection getConnect() { + return con; + } + + private static void closeConnection() { + try { + con.close(); + } catch (Exception e) { + System.out.println("Error al cerrar la conexión."); + } + } + + public static void delInstance() { + closeConnection(); + instance = null; + } +} diff --git a/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/ListCourse.java b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/ListCourse.java new file mode 100644 index 00000000..db79f23c --- /dev/null +++ b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/ListCourse.java @@ -0,0 +1,71 @@ +package com.topic4.choco; + +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +import com.mysql.jdbc.PreparedStatement; + +public class ListCourse { + public static void main(String[] args) { + try { + + ArrayList courses = new ArrayList(); + + HashMap> map = new HashMap>(); + + Driver conn = Driver.getInstance(); + String operator = ">"; + String query = "Select Teacher.first_name as firstName, Teacher.teacher_id, Teacher.last_name as lastName," + + " Course.name as courseName,Schedule_time.beginning_hours as " + + "startHours, Schedule_time.ending_hours endHours,Schedule_time.date as dateSchudele" + + " from Schedule_time " + "left join Course " + + "on Course.schedule_time_id = Schedule_time.schedule_time_id " + "left join Teacher_course " + + "on Teacher_course.course_id = Course.course_id " + "left join Teacher " + + "on Teacher.teacher_id = Teacher_course.teacher_id " + "where Teacher_course.teacher_id = ?;"; + PreparedStatement prepStmt = (PreparedStatement) conn.getConnect().prepareStatement(query); + prepStmt.setInt(1, 1); + prepStmt.execute(); + ResultSet rst = prepStmt.executeQuery(); + String courseName=null; + + String teacher; + while (rst.next()) { + teacher="Teacher \n"+rst.getString("lastName")+" "+rst.getString("firstName"); + if("1".equalsIgnoreCase(rst.getString("teacher_id"))){ + courses.add(rst.getString("dateSchudele") + " " + rst.getString("startHours") +" "+rst.getString("endHours")+" " + + rst.getString("courseName")); + } + if("2".equalsIgnoreCase(rst.getString("teacher_id"))){ + courses.add(rst.getString("dateSchudele") + " " + rst.getString("startHours") +" "+rst.getString("endHours")+" " + + rst.getString("courseName")); + } + if("3".equalsIgnoreCase(rst.getString("teacher_id"))){ + courses.add(rst.getString("dateSchudele") + " " + rst.getString("startHours") +" "+rst.getString("endHours")+" " + + rst.getString("courseName")); + } + map.put(teacher, courses); + } + //recorro el mapa + Iterator it=map.keySet().iterator(); + String key; + IteratoritSchudele; + while(it.hasNext()){ + key=it.next(); + System.out.println(key +"\n Schudele \n "); + itSchudele=map.get(key).iterator(); + while(itSchudele.hasNext()){ + System.out.println(itSchudele.next()); + } + } + + + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + Driver.delInstance(); + } + + } +} diff --git a/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/Main.java b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/Main.java new file mode 100644 index 00000000..28d3b306 --- /dev/null +++ b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/Main.java @@ -0,0 +1,60 @@ +package com.topic4.choco; + +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +public class Main { + public static void main(String[] args) { + try { + ArrayList lengua = new ArrayList(); + ArrayList matematica = new ArrayList(); + ArrayList geografia = new ArrayList(); + + HashMap> map = new HashMap>(); + + Driver con = Driver.getInstance(); + Statement stmt = con.getConnect().createStatement(); + ResultSet rst = stmt.executeQuery("SELECT * FROM Course crs LEFT JOIN Student_course std_c ON crs.course_id = std_c.course_id " + + "LEFT JOIN Student std ON std_c.student_id = std.student_id " + + "LEFT JOIN Teacher_course tc ON crs.course_id = tc.course_id " + + "LEFT JOIN Teacher tch ON tc.teacher_id = tch.teacher_id ORDER BY std.last_name"); + + while (rst.next()) { + + if (rst.getString("name").equalsIgnoreCase("Lengua")) { + lengua.add(rst.getString("std.first_name") + " " + rst.getString("std.last_name")); + map.put("Course: "+rst.getString("name")+"\n"+"Teacher: " + rst.getString("tch.first_name") + ", " + + rst.getString("tch.last_name") + "\n" + "Students: \n" , lengua); + } + + if (rst.getString("name").equalsIgnoreCase("musica")) { + matematica.add(rst.getString("std.first_name") + " " + rst.getString("std.last_name")); + map.put("Course: "+rst.getString("name")+"\n"+"Teacher: " + rst.getString("tch.first_name") + ", " + + rst.getString("tch.last_name") + "\n" + "Students: \n" , matematica); + } + + if (rst.getString("name").equalsIgnoreCase("historia")) { + geografia.add(rst.getString("std.first_name") + " " + rst.getString("std.last_name")); + map.put("Course: "+rst.getString("name")+"\n"+"Teacher: " + rst.getString("tch.first_name") + ", " + + rst.getString("tch.last_name") + "\n" + "Students: \n" , geografia); + } + + } + + Iterator it = map.keySet().iterator(); + while (it.hasNext()) { + String key = it.next(); + System.out.println(key + map.get(key)+ "\n"); + } + + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + Driver.delInstance(); + } + } + +} diff --git a/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/PercentageOfStudents.java b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/PercentageOfStudents.java new file mode 100644 index 00000000..a2fff77a --- /dev/null +++ b/FernandoEmanuelBarrios/Topic4_choco/src/main/java/com/topic4/choco/PercentageOfStudents.java @@ -0,0 +1,44 @@ +package com.topic4.choco; + +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; + +import com.mysql.jdbc.PreparedStatement; + +public class PercentageOfStudents { + public static void main(String[] args) { + try { + ArrayList lengua = new ArrayList(); + ArrayList matematica = new ArrayList(); + ArrayList geografia = new ArrayList(); + + HashMap> map = new HashMap>(); + + Driver con = Driver.getInstance(); + String query = "UPDATE `high_school`.`Student_course` SET `final_note`= ((parcial_note_1+parcial_note_2+parcial_note_3)/3) " + + "WHERE `student_course_id`=?"; + PreparedStatement prepareStmt = (PreparedStatement) con.getConnect().prepareStatement(query); + for (int i = 1; i <= 20; i++) { + prepareStmt.setInt(1, i); + prepareStmt.executeUpdate(); + } + Driver conn = Driver.getInstance(); + String operator=">"; + query = "select Course.course_id, Course.name,avg(Student_course.final_note) as Promedio from Course left join Student_course on Student_course.course_id = Course.course_id where Course.course_id = ? and Student_course.final_note "+operator+ " 6 Group By Course.name Order By Course.course_id"; + PreparedStatement prepStmt = (PreparedStatement) conn.getConnect().prepareStatement(query); + prepStmt.setInt(1, 1); + prepStmt.execute(); + ResultSet rst = prepStmt.executeQuery(); + while(rst.next()){ + System.out.println(rst.getString("Promedio")); + } + + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + Driver.delInstance(); + } + } +}