Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primer commit topic 4 #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> courses = new ArrayList<String>();

HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

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<String> it=map.keySet().iterator();
String key;
Iterator<String>itSchudele;
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();
}

}
}
Original file line number Diff line number Diff line change
@@ -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<String> lengua = new ArrayList<String>();
ArrayList<String> matematica = new ArrayList<String>();
ArrayList<String> geografia = new ArrayList<String>();

HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

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<String> 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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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<String> lengua = new ArrayList<String>();
ArrayList<String> matematica = new ArrayList<String>();
ArrayList<String> geografia = new ArrayList<String>();

HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

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();
}
}
}