Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- public class CreateSampleTables {
- public static void main(String args[]) {
- Statement stmt = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- System.out.println("Loaded Driver");
- String url = "jdbc:mysql://cs.stcc.edu/silvestri?allowMultiQueries=true";
- String user = "readonly";
- String password = "readonly";
- Connection con = DriverManager.getConnection(url, user, password);
- System.out.println("Connected to database");
- stmt = con.createStatement();
- System.out.println("Dropping Existing Tables");
- stmt.executeUpdate(""
- + "drop table if exists Department;"
- + "drop table if exists Enrollment;"
- + "drop table if exists TaughtBy;");
- System.out.println("Creating Tables");
- stmt.executeUpdate(""
- + "create table Department ("
- + " deptId char(4) not null, "
- + " name varchar(25) unique, /* works in MYSQL */ "
- + " chairId varchar(9), " + " collegeId varchar(4), "
- + " constraint pkDepartment primary key (deptId));"
- + "create table Enrollment (" + " ssn char(9) not null,"
- + " courseId char(5) not null," + " dateRegistered date,"
- + " grade char(1),"
- + " constraint pkEnrollment primary key (ssn, courseId));"
- + "create table TaughtBy (" + " courseId char(5),"
- + " ssn char(9));");
- System.out.println("Loading Tables");
- stmt.executeUpdate(""
- + "insert into Department values ('CS', 'Computer Science', '111221115', 'SC');"
- + "insert into Department values ('MATH', 'Mathematics', '111221116', 'SC');"
- + "insert into Department values ('CHEM', 'Chemistry', '111225555', 'SC');"
- + "insert into Department values ('EDUC', 'Education', '333114444', 'EDUC');"
- + "insert into Department values ('ACCT', 'Accounting', '333115555', 'BUSS');"
- + "insert into Department values ('BIOL', 'Biology', '111225555', 'SC');"
- + "insert into Enrollment values ('444111110', '11111', now(), 'A');"
- + "insert into Enrollment values ('444111110', '11112', now(), 'B');"
- + "insert into Enrollment values ('444111110', '11113', now(), 'C');"
- + "insert into Enrollment values ('444111111', '11111', now(), 'D');"
- + "insert into Enrollment values ('444111111', '11112', now(), 'F');"
- + "insert into Enrollment values ('444111111', '11113', now(), 'A');"
- + "insert into Enrollment values ('444111112', '11114', now(), 'B');"
- + "insert into Enrollment values ('444111112', '11115', now(), 'C');"
- + "insert into Enrollment values ('444111112', '11116', now(), null);"
- + "insert into Enrollment values ('444111113', '11111', now(), null);"
- + "insert into Enrollment values ('444111113', '11113', now(), null);"
- + "insert into Enrollment values ('444111114', '11115', now(), null);"
- + "insert into Enrollment values ('444111115', '11115', now(), null);"
- + "insert into Enrollment values ('444111115', '11116', now(), null);"
- + "insert into Enrollment values ('444111116', '11111', now(), null);"
- + "insert into Enrollment values ('444111117', '11111', now(), null);"
- + "insert into Enrollment values ('444111118', '11111', now(), null);"
- + "insert into Enrollment values ('444111118', '11112', now(), null);"
- + "insert into Enrollment values ('444111118', '11113', now(), null);"
- + "insert into TaughtBy values ('11111', '111221111');"
- + "insert into TaughtBy values ('11112', '111221111');"
- + "insert into TaughtBy values ('11113', '111221111');"
- + "insert into TaughtBy values ('11114', '111221115');"
- + "insert into TaughtBy values ('11115', '111221110');"
- + "insert into TaughtBy values ('11116', '111221115');"
- + "insert into TaughtBy values ('11117', '111221116');"
- + "insert into TaughtBy values ('11118', '111221112');"
- );
- System.out.println("Done.");
- } catch (SQLException e) {
- System.out.println("SQL Error! " + e.getMessage());
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- stmt.close();
- } catch (Exception e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement