DBMS Exercise Final Template
DBMS Exercise Final Template
Design Topic
Mentor
Profession
Class
Student ID
Name
Date
Catalog
Through the platform teachers can assign, collect, revise and count the
and downloaded as attachments, and support for the usual mark statistics
etc.
2. System analysis
2.1 The main tasks to be completed in this topic are.
database)
2. Collect the assignment (delete the tuples of the Course table in the
database)
3. Modify the assignment (modify the tuples in the Course table in the
database)
platform
database
2. the Student table contains the attributes Sno (student number and is the
(course name), Cgrade (the usual grade of the course), Tno (teacher
number, for the Course table of the external code), Sno (student number,
for the Course of the external code) Cno, Tno, Sno together constitute the
relationship
The Teacher table is linked to the Course table by the assign relationship
The Student table is linked to the Course table by the study relationship
third paradigm and therefore can greatly increase the efficiency of the
and each table does describe the properties of the objects in the topic
clearly. Thus, the problem raised in the topic description is well solved.
4. Overall ER diagram and pdm diagram generated from
ER diagram
Overall ER diagram
PDM diagram converted from ER diagram
Using this overall ER diagram with the advantage of no redundant
attributes when connecting and easy to export the database, the E-R
basis for various data models, thus more general, more abstract, and
The following commands are executed using the exported sql file pasted
into mysql
Command 1 : Create database
Command 2: If there is a table with the same name, drop all
language
First you need the plugin named mysql-connector-java-8.0.11, create a lib
folder in the project, put this plugin in it and add the project path, after
create a Connection object to connect to the database, and then write each
Add Teacher
Add Course
Delete Teacher
Delete Student
8. Java Code
package cn.knightzz;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
@SuppressWarnings("all")
public class Test {
// 2.Connect database;
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dbms_course?useSSL=false&serverTimezone=GMT
%2B8", "root", "123456");
// Execute SQL;
int cmd;
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Which operation to perform?\n 1.Search\t 2.ADD\t 3.Edit\t
4.Delete\t 5.Quit");
cmd = in.nextInt();
if (cmd == 1) {
Select(connection);
} else if (cmd == 2) {
Add(connection);
} else if (cmd == 3) {
Alter(connection);
} else if (cmd == 4) {
Delete(connection);
} else {
break;
}
}
// 6.Release resource;
stmt.close();
connection.close();
in.close();
}
// Search Function
public static void Select(Connection con) throws SQLException {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(
"SELECT * FROM student,course,teacher WHERE teacher.Tno=course.Tno AND
course.Sno=student.Sno");
while (rs.next()) {
int sno = rs.getInt("student.Sno");
int tno = rs.getInt("teacher.Tno");
int cno = rs.getInt("course.Cno");
String sname = rs.getString("student.Sname");
String cname = rs.getString("course.Cname");
String tname = rs.getString("teacher.Tname");
double shomework = rs.getDouble("student.Shomework");
int cgrade = rs.getInt("course.Cgrade");
System.out.println("Name:" + sname + " 课程名:" + cname + " 老师:" + tname + "
分数:" + cgrade + " 作业完成情况"
+ (shomework * 100) + "%");
}
}
// Add Function
public static void Add(Connection con) throws SQLException {
String Name[] = { "Student", "Teacher", "Course" };
System.out.println("Which tuple to add?\n 1." + Name[0] + "\t2." + Name[1] + "\t3."
+ Name[2] + "\t4.返回上一级");
Scanner sc = new Scanner(System.in);
int opt = sc.nextInt();
Statement st = con.createStatement();
int flag;
if (opt == 1) {
int sno;
String sname;
double shomework;
System.out.print("Sno:");
sno = sc.nextInt();
System.out.print("Sname:");
sname = sc.next();
System.out.print("Shomework:");
shomework = sc.nextDouble();
flag = st.executeUpdate("INSERT INTO `student`(Sno,Sname,Shomework) VALUES
(" + sno + ",'" + sname + "',"
+ shomework + ")");
if (flag > 0) {
System.out.println("add Student success!");
}
} else if (opt == 2) {
int tno, cno;
String tname;
System.out.print("Tno:");
tno = sc.nextInt();
System.out.print("Tname:");
tname = sc.next();
flag = st.executeUpdate("INSERT INTO `teacher`(Tno,Tname) VALUES (" + tno +
",'" + tname + "')");
if (flag > 0) {
System.out.println("add Teacher success!");
}
} else if (opt == 3) {
int cno, cgrade, tno, sno;
String cname;
System.out.print("Cno:");
cno = sc.nextInt();
System.out.print("Cname:");
cname = sc.next();
System.out.print("Tno:");
tno = sc.nextInt();
System.out.print("Sno:");
sno = sc.nextInt();
System.out.print("Cgrade:");
cgrade = sc.nextInt();
flag = st.executeUpdate("INSERT INTO `course`(Cno,Cname,Cgrade,Tno,Sno)
VALUES (" + cno + ",'" + cname
+ "'," + cgrade + "," + tno + "," + sno + ")");
if (flag > 0) {
System.out.println("add Course success!");
}
}
}
// update Function
public static void Alter(Connection con) throws SQLException {
String Name[] = { "Student", "Teacher", "Course" };
System.out.println("Which tuple to edit? 1." + Name[0] + "\t2." + Name[1] + "\t3." +
Name[2] + "\t4.Back to previous level");
Scanner sc = new Scanner(System.in);
int opt = sc.nextInt();
Statement st = con.createStatement();
int flag;
if (opt == 1) {
int sno;
System.out.println("please input the Sno");
sno = sc.nextInt();
System.out.println("Which property to modify?\n1.Sname\t 2.Shomework\t 3.Back
to previous level");
int index;
index = sc.nextInt();
System.out.println("Enter the modified value:");
if (index == 1) {
String sname;
sname = sc.next();
flag = st.executeUpdate("UPDATE student SET Sname=" + sname + " WHERE
Sno=" + sno);
if (flag > 0) {
System.out.println("Sno " + sno + "of Student of Sname edit success");
}
} else if (index == 2) {
double shomework;
shomework = sc.nextDouble();
flag = st.executeUpdate("UPDATE student SET Shomework=" + shomework + "
WHERE Sno=" + sno);
System.out.println("Sno : " + sno + " of Student 的 Shomework update success");
}
} else if (opt == 2) {
int tno;
System.out.println("please input the Tno");
tno = sc.nextInt();
System.out.println("Which property to modify ? \n1.Tname\t 2.Back to previous
level");
int index;
index = sc.nextInt();
System.out.println("Enter the modified value:");
if (index == 1) {
String tname;
tname = sc.next();
flag = st.executeUpdate("UPDATE teacher SET Tname=" + tname + " WHERE
Tno=" + tno);
if (flag > 0) {
System.out.println("Tno 为" + tno + "of Teacher of Tname update success");
}
}
} else if (opt == 3) {
int cno;
System.out.println("please input the Cno");
cno = sc.nextInt();
System.out.println("Which property to modify? \n1.Cname\t 2.Cgrade\t 3.Back to
previous level");
int index;
index = sc.nextInt();
System.out.println("Enter the modified value:");
if (index == 1) {
String cname;
cname = sc.next();
flag = st.executeUpdate("UPDATE course SET Cname=" + cname + " WHERE
Cno=" + cno);
if (flag > 0) {
System.out.println("Cno 为" + cno + "of course of Cname edit success!");
}
} else if (index == 2) {
int cgrade;
cgrade = sc.nextInt();
flag = st.executeUpdate("UPDATE course SET Cgrade=" + cgrade + " WHERE
Cno=" + cno);
System.out.println("Cno " + cno + "of course of Cgrade edit success");
}
}
}
// delete function
public static void Delete(Connection con) throws SQLException {
String Name[] = { "Student", "Teacher", "Course" };
System.out.println("Which tuple to delete ?\n1." + Name[0] + "\t2." + Name[1] + "\
t3." + Name[2] + "\t4.Back to previous level");
Scanner sc = new Scanner(System.in);
int opt = sc.nextInt();
Statement st = con.createStatement();
int flag;
if (opt == 1) {
int sno;
System.out.println("please input the Sno");
sno = sc.nextInt();
flag = st.executeUpdate("DELETE FROM student WHERE Sno=" + sno);
if (flag > 0) {
System.out.println("delete Student success ");
}
} else if (opt == 2) {
int tno;
System.out.println("please input the Tno");
tno = sc.nextInt();
flag = st.executeUpdate("DELETE FROM teacher WHERE Tno=" + tno);
if (flag > 0) {
System.out.println("delete Teacher success");
}
} else if (opt == 3) {
int cno;
System.out.println("please input the Cno");
cno = sc.nextInt();
flag = st.executeUpdate("DELETE FROM course WHERE Cno=" + cno);
if (flag > 0) {
System.out.println("delete Course success");
}
}
}
}