Skip to content
Browse files

Add SUBD project.

  • Loading branch information...
1 parent 32a3597 commit e8ac7c5f9225a9e507bdcc178aadec041480a59a @nbeloglazov committed
View
3 .gitignore
@@ -2,4 +2,5 @@ lib
classes
*.class
.idea
-*.iml
+*.iml
+target
View
82 subd/lab.sql
@@ -0,0 +1,82 @@
+drop table Lesson;
+drop table Classroom;
+drop table ClassroomTypeSubject;
+drop table ClassroomType;
+drop table Course;
+drop table Subject;
+drop table Teacher;
+
+-- Create for Oracle
+create table Teacher (ID integer not null primary key,
+ Name varchar(255) not null,
+ Title varchar(255));
+create table Subject (ID integer not null primary key,
+ Name varchar(255) not null);
+create table Course (ID integer not null primary key,
+ TeacherID integer references Teacher(ID),
+ SubjectID integer references Subject(ID),
+ HoursPerWeek integer not null check (HoursPerWeek > 0),
+ StudentsNumber integer not null check (StudentsNumber > 0));
+create table ClassroomType (ID integer not null primary key,
+ Name varchar(255) not null);
+create table ClassroomTypeSubject (ClassroomTypeID integer not null references ClassroomType(ID),
+ SubjectID integer not null references Subject(ID),
+ constraint pk primary key (ClassroomTypeID, SubjectID));
+create table Classroom (ID integer not null primary key,
+ ClassroomTypeID integer references ClassroomType(ID),
+ Number integer not null check (Number > 0),
+ Building integer not null check (Building > 0),
+ Capacity integer not null check (Capacity > 0));
+create table Lesson (ID integer not null primary key,
+ CourseID integer references Course(ID),
+ ClassroomID integer references Classroom(ID),
+ DayOfWeek integer not null check (DayOfWeek >= 1 and DayOfWeek <= 7),
+ StartTime integer not null,
+ Duration integer not null);
+
+
+-- Initialization
+insert into Teacher values (1, 'Ivanov Ivan Ivanovich', 'Prof');
+insert into Teacher values (2, 'Sidorov Sidor Sidorovich', 'Dr');
+insert into Teacher values (3, 'John Black', 'Mr');
+insert into Teacher values (4, 'Smit Brown', 'PhD');
+insert into Teacher values (5, 'White House', 'Hi');
+
+insert into Subject values (1, 'Mathematical analysis');
+insert into Subject values (2, 'Discrete mathematics');
+insert into Subject values (3, 'Philosophy');
+insert into Subject values (4, 'Databases');
+insert into Subject values (5, 'Psychology');
+
+insert into Course values (1, 2, 3, 2, 50);
+insert into Course values (2, 3, 4, 4, 100);
+insert into Course values (3, 4, 5, 8, 150);
+insert into Course values (4, 5, 1, 16, 200);
+insert into Course values (5, 1, 2, 32, 250);
+
+insert into ClassroomType values (1, 'computer');
+insert into ClassroomType values (2, 'Lesson');
+insert into ClassroomType values (3, 'pracktice');
+
+insert into ClassroomTypeSubject values (1, 4);
+insert into ClassroomTypeSubject values (2, 1);
+insert into ClassroomTypeSubject values (2, 2);
+insert into ClassroomTypeSubject values (2, 3);
+insert into ClassroomTypeSubject values (2, 4);
+insert into ClassroomTypeSubject values (2, 5);
+insert into ClassroomTypeSubject values (3, 1);
+insert into ClassroomTypeSubject values (3, 2);
+
+insert into Classroom values (1, 1, 205, 3, 30);
+insert into Classroom values (2, 1, 206, 3, 30);
+insert into Classroom values (3, 2, 207, 4, 250);
+insert into Classroom values (4, 2, 208, 5, 250);
+insert into Classroom values (5, 3, 209, 3, 30);
+insert into Classroom values (6, 3, 210, 2, 30);
+
+insert into Lesson values (1, 1, 3, 1, 780, 120);
+insert into Lesson values (2, 1, 5, 2, 780, 120);
+insert into Lesson values (3, 4, 1, 3, 840, 120);
+insert into Lesson values (4, 4, 4, 4, 780, 120);
+insert into Lesson values (5, 3, 3, 5, 780, 120);
+insert into Lesson values (6, 3, 4, 5, 980, 120);
View
25 subd/pom.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>subd</groupId>
+ <artifactId>subd</artifactId>
+ <version>1.0</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <version>5.1.6</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.6</version>
+ </dependency>
+
+ </dependencies>
+
+</project>
View
28 subd/queries.sql
@@ -0,0 +1,28 @@
+select Teacher.Name from Teacher
+inner join Course on Course.TeacherID = Teacher.ID
+inner join Lesson on Course.ID = Lesson.CourseID
+inner join Classroom on Classroom.ID = Lesson.ClassroomID
+where Classroom.Number = &classroom and Lesson.DayOfWeek = &day;
+
+
+select Teacher.Name from Teacher
+inner join Course on Course.TeacherID = Teacher.ID
+inner join Lesson on Course.ID = Lesson.CourseID
+where Lesson.DayOfWeek not in (1, 4);
+
+
+select t1.DayOfWeek, count(*) as NumberOfClassrooms
+from (select Lesson.ClassroomID, Lesson.DayOfWeek
+ from Lesson
+ group by Lesson.DayOfWeek, Lesson.ClassroomID) t1
+where ((((select count(*)
+ from (select Lesson.ClassroomID, Lesson.DayOfWeek
+ from Lesson
+ group by Lesson.DayOfWeek, Lesson.ClassroomID) t3
+ where t3.DayOfWeek = t1.DayOfWeek))
+ = (select min(NumberOfClassrooms)
+ from (select count(*) as NumberOfClassrooms
+ from (select LEsson.ClassroomID, Lesson.DayOfWeek
+ from Lesson group by Lesson.DayOfWeek, Lesson.ClassroomID) t4
+ group by t4.DayOfWeek))))
+group by t1.DayOfWeek;
View
34 subd/src/main/java/by/bsu/fpmi/beloglazov/subd/Application.java
@@ -0,0 +1,34 @@
+package by.bsu.fpmi.beloglazov.subd;
+
+import by.bsu.fpmi.beloglazov.subd.gui.SubdFrame;
+
+import javax.sql.RowSet;
+import javax.swing.JFrame;
+import java.sql.SQLException;
+import java.util.Map;
+
+public class Application {
+
+ public static void main(String[] args) throws Exception {
+ JFrame frame = new SubdFrame();
+ frame.pack();
+ frame.setLocationRelativeTo(null);
+ frame.setVisible(true);
+ for (Entity entity : Entity.values()) {
+ printEntities(entity);
+ }
+ }
+
+ private static void printEntities(Entity entity) throws SQLException {
+ RowSet rowSet = DBUtils.getAll(entity);
+ System.out.println(entity.getName());
+ while (rowSet.next()) {
+ for (String field : entity.getFields()) {
+ System.out.printf("%s: %s ", field, rowSet.getObject(field));
+ }
+ System.out.println();
+ }
+ System.out.println();
+ rowSet.close();
+ }
+}
View
33 subd/src/main/java/by/bsu/fpmi/beloglazov/subd/DBUtils.java
@@ -0,0 +1,33 @@
+package by.bsu.fpmi.beloglazov.subd;
+
+import com.sun.rowset.JdbcRowSetImpl;
+import org.apache.commons.lang.StringUtils;
+
+import javax.sql.RowSet;
+import javax.sql.rowset.JdbcRowSet;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public final class DBUtils {
+
+ private static final String USERNAME = "scott";
+ private static final String PASSWORD = "tiger";
+ private static final String URL = "jdbc:mysql://localhost:3306/subd";
+
+ public static Connection getConnection() throws SQLException {
+ return DriverManager.getConnection(URL, USERNAME, PASSWORD);
+ }
+
+ public static RowSet getAll(Entity entity) throws SQLException {
+
+ JdbcRowSet rowSet = new JdbcRowSetImpl(getConnection());
+ String query = String.format("select %s from %s;",
+ StringUtils.join(entity.getFields(), ", "),
+ entity.getName());
+ rowSet.setCommand(query);
+ rowSet.execute();
+ return rowSet;
+ }
+
+}
View
41 subd/src/main/java/by/bsu/fpmi/beloglazov/subd/Entity.java
@@ -0,0 +1,41 @@
+package by.bsu.fpmi.beloglazov.subd;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.WordUtils;
+import org.apache.commons.lang.enums.EnumUtils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public enum Entity {
+
+ TEACHER("ID", "Name", "Title"),
+ SUBJECT("ID", "Name"),
+ COURSE("ID", "TeacherID", "SubjectID", "HoursPerWeek", "StudentsNumber"),
+ CLASSROOM_TYPE("ID", "Name"),
+ CLASSROOM_TYPE_SUBJECT("ClassroomTypeID", "SubjectID"),
+ CLASSROOM("ID", "ClassroomTypeID", "Number", "Building", "Capacity"),
+ LESSON("ID", "CourseID", "ClassroomID", "DayOfWeek", "StartTime", "Duration");
+
+
+ private List<String> fields;
+
+ private Entity(String... fields) {
+ this.fields = Collections.unmodifiableList(Arrays.asList(fields));
+ }
+
+ public List<String> getFields() {
+ return fields;
+ }
+
+ public String getName() {
+ String[] parts = name().toLowerCase().split("_");
+ for (int i = 0; i < parts.length; i++) {
+ parts[i] = StringUtils.capitalize(parts[i]);
+ }
+ return StringUtils.join(parts);
+ }
+
+
+}
View
98 subd/src/main/java/by/bsu/fpmi/beloglazov/subd/gui/EntityTableModel.java
@@ -0,0 +1,98 @@
+package by.bsu.fpmi.beloglazov.subd.gui;
+
+import by.bsu.fpmi.beloglazov.subd.DBUtils;
+import by.bsu.fpmi.beloglazov.subd.Entity;
+
+import javax.sql.RowSet;
+import javax.swing.event.TableModelListener;
+import javax.swing.table.TableModel;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class EntityTableModel implements TableModel {
+
+ private RowSet rowSet;
+ private Entity entity;
+ private int rowCount;
+ private int columnCount;
+ private List<Class<?>> columnClasses;
+ private Map<String, Object> updates;
+
+ public EntityTableModel(Entity entity) throws SQLException, ClassNotFoundException {
+ this.entity = entity;
+ this.rowSet = DBUtils.getAll(entity);
+ rowCount = 0;
+ rowSet.first();
+ while (rowSet.next()) {
+ rowCount++;
+ }
+ columnCount = entity.getFields().size();
+ columnClasses = new ArrayList<Class<?>>(columnCount);
+ for (int i = 0; i < columnCount; i++) {
+ columnClasses.add(Class.forName(rowSet.getMetaData().getColumnClassName(i + 1)));
+ }
+ updates = new HashMap<String, Object>();
+ }
+
+ public void addTableModelListener(TableModelListener l) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public int getRowCount() {
+ return rowCount;
+ }
+
+ public int getColumnCount() {
+ return columnCount;
+ }
+
+ public String getColumnName(int columnIndex) {
+ return entity.getFields().get(columnIndex);
+ }
+
+ public Class<?> getColumnClass(int columnIndex) {
+ return columnClasses.get(columnIndex);
+ }
+
+ public boolean isCellEditable(int rowIndex, int columnIndex) {
+ return true;
+ }
+
+ public Object getValueAt(int rowIndex, int columnIndex) {
+ try {
+ Object result = getUpdate(rowIndex, columnIndex);
+ if (result != null) {
+ return result;
+ }
+ rowSet.absolute(rowIndex + 1);
+ return rowSet.getObject(columnIndex + 1);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
+ try {
+ rowSet.absolute(rowIndex + 1);
+ rowSet.updateObject(columnIndex + 1, aValue);
+ setUpdate(rowIndex, columnIndex, aValue);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void removeTableModelListener(TableModelListener l) {
+
+ }
+
+ private void setUpdate(int row, int column, Object value) {
+ updates.put(row + ";" + column, value);
+ }
+
+ private Object getUpdate(int row, int column) {
+ return updates.get(row + ";" + column);
+ }
+}
View
20 subd/src/main/java/by/bsu/fpmi/beloglazov/subd/gui/SubdFrame.java
@@ -0,0 +1,20 @@
+package by.bsu.fpmi.beloglazov.subd.gui;
+
+import by.bsu.fpmi.beloglazov.subd.Entity;
+
+import javax.swing.JFrame;
+import javax.swing.JTable;
+import javax.swing.WindowConstants;
+
+public class SubdFrame extends JFrame {
+
+ public SubdFrame() throws Exception {
+ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+ addTable();
+ }
+
+ private void addTable() throws Exception {
+ JTable table = new JTable(new EntityTableModel(Entity.TEACHER));
+ getContentPane().add(table);
+ }
+}

0 comments on commit e8ac7c5

Please sign in to comment.
Something went wrong with that request. Please try again.