# 7
Okay. I include the program wich created and filled the DB. I testted with the table "musiccategories". I even tested with a Driver 3.x but wwith the same results.
-- code --
/*
* MusicDB.java
*
* Created on February 28, 2006, 2:27 PM
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package asg.databuild;
import java.sql.*;
public class MySQLMusicDB {
public static void main(String args[]) {
new MySQLMusicDB(args);
}
/** Creates a new instance of DerbyMusicDB */
public MySQLMusicDB(String args[]) {
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //NOI18N
} catch (Exception e) {
System.err.println("Class.forName: " + e.getMessage()); //NOI18N
System.exit(1);
}
createMusicDatabase(args);
}
void createMusicDatabase(String args[]) {
String port = "3306";
//String queryString = "select * from Recordings"; //NOI18N
Connection con;
Statement stmt;
String url = "jdbc:mysql://localhost:" + port + "/"; //NOI18N
try {
con = DriverManager.getConnection(url,"root","xxxxxx"); //NOI18N
stmt = con.createStatement();
try {
createUser(stmt, "Music", "music", "music"); //NOI18N
} catch (Exception e) {
}
try {
createSchema(stmt, "Music"); //NOI18N
} catch (Exception e) {
System.err.println("Unable to create user+schema on url " +
url );
e.printStackTrace(System.err) ;
}
con.close();
url += "Music";
con = DriverManager.getConnection(url, "music", "music"); //NOI18N
stmt = con.createStatement();
// Drop all tables first to avoid constraints
dropTable(stmt, "Tracks");
dropTable(stmt, "Recordings");
dropTable(stmt, "MusicCategories");
dropTable(stmt, "RecordingArtists");
dropTable(stmt, "ValidationOnly");
// Music Categories Table
createTable(stmt, "MusicCategories",
"MusicCategoryID INTEGER NOT NULL, MusicCategory VARCHAR(20)");
addConstraint(stmt, "MusicCategories",
"MusicCategoriesPK PRIMARY KEY (MusicCategoryID)");
insertRow(stmt, "MusicCategories", "1, 'Classical'");
insertRow(stmt, "MusicCategories", "2, 'Rock'");
insertRow(stmt, "MusicCategories", "3, 'Jazz'");
insertRow(stmt, "MusicCategories", "4, 'Rap'");
insertRow(stmt, "MusicCategories", "5, 'Country'");
insertRow(stmt, "MusicCategories", "6, 'Musical Theatre'");
insertRow(stmt, "MusicCategories", "7, 'Blues'");
// Recording Artists Table
createTable(stmt, "RecordingArtists",
"RecordingArtistID INTEGER NOT NULL, RecordingArtistName VARCHAR(36), Notes VARCHAR(200)");
addConstraint(stmt, "RecordingArtists",
"RecordingArtistsPK PRIMARY KEY (RecordingArtistID)");
insertRow(stmt, "RecordingArtists",
"1, 'The Philadelphia Orchestra', ''");
insertRow(stmt, "RecordingArtists",
"2, 'Indigo Girls', ''");
insertRow(stmt, "RecordingArtists",
"3, 'John Lennon', ''");
insertRow(stmt, "RecordingArtists",
"4, 'Karla Bonoff', ''");
insertRow(stmt, "RecordingArtists",
"5, 'Paul Simon', ''");
insertRow(stmt, "RecordingArtists",
"6, 'Gin Blossoms', ''");
insertRow(stmt, "RecordingArtists",
"7, 'Beatles', ''");
insertRow(stmt, "RecordingArtists",
"8, 'Steely Dan', ''");
insertRow(stmt, "RecordingArtists",
"9, 'It''s a Beautiful Day', ''");
// Recordings Table
createTable(stmt, "Recordings",
"RecordingID INTEGER NOT NULL, RecordingTitle VARCHAR(50), " +
"RecordingArtistID INTEGER, MusicCategoryID INTEGER, " +
"RecordingLabel VARCHAR(36), Format VARCHAR(20), " +
"NumberofTracks SMALLINT, Notes VARCHAR(200)");
addConstraint(stmt, "Recordings",
"RecordingsPK PRIMARY KEY (RecordingID)");
addConstraint(stmt, "Recordings",
"RecordingsRecordingArtistsFK FOREIGN KEY (RecordingArtistID) " +
"REFERENCES RecordingArtists (RecordingArtistID)");
addConstraint(stmt, "Recordings",
"RecordingsMusicCategoriesFK FOREIGN KEY (MusicCategoryID) " +
"REFERENCES MusicCategories (MusicCategoryID)");
insertRow(stmt, "Recordings",
"1, 'Orff: Carmina Burana', 1, 1, 'Sony Classical', '', 11, ''");
insertRow(stmt, "Recordings",
"2, 'Rites of Passage', 2, 2, 'Epic', '', 13, ''");
insertRow(stmt, "Recordings",
"3, 'Imagine', 3, 2, 'Warner Brothers', '', 21, ''");
insertRow(stmt, "Recordings",
"4, 'Karla Bonoff', 4, 2, 'Columbia', '', 10, ''");
insertRow(stmt, "Recordings",
"5, 'Graceland', 5, 2, 'Warner Brothers', '', 11, ''");
insertRow(stmt, "Recordings",
"6, 'Congratulations I''m Sorry', 6, 2, 'A&M Records', '', 13, ''");
insertRow(stmt, "Recordings",
"7, 'Sgt. Pepper''s Lonely Hearts Club Band', 7, 2, 'EMI Records', '', 12, ''");
// Tracks Table
createTable(stmt, "Tracks",
"TrackID INTEGER NOT NULL, TrackNumber SMALLINT, " +
"TrackTitle VARCHAR(100), TrackLength VARCHAR(10), " +
"RecordingID INTEGER");
addConstraint(stmt, "Tracks",
"TracksPK PRIMARY KEY (TrackID)");
addConstraint(stmt, "Tracks",
"TracksRecordingsFK FOREIGN KEY (RecordingID) " +
"REFERENCES Recordings (RecordingID)");
insertRow(stmt, "Tracks",
"1, 1, 'O Fortuna', '2:44', 1");
insertRow(stmt, "Tracks",
"2, 2, 'Fortune plango vulnera', '2:39', 1");
insertRow(stmt, "Tracks",
"3, 3, 'Veris leta facies', '3:26', 1");
insertRow(stmt, "Tracks",
"4, 4, 'Omnia Sol temperat', '1:47', 1");
insertRow(stmt, "Tracks",
"5, 5, 'Ecce gratum', '2:35', 1");
insertRow(stmt, "Tracks",
"6, 6, 'Tanz', '1:36', 1");
insertRow(stmt, "Tracks",
"7, 7, 'Floret silva', '3:15', 1");
insertRow(stmt, "Tracks",
"8, 8, 'Chramet, gip die varwe mir', '3:11', 1");
insertRow(stmt, "Tracks",
"9, 9, 'Reie', '1:49', 1");
insertRow(stmt, "Tracks",
"10, 10, 'Swazhie get umbe', '2:31', 1");
insertRow(stmt, "Tracks",
"11, 11, 'Were deu werlt alle min', '0:55', 1");
insertRow(stmt, "Tracks",
"12, 1, 'Three Hits', '', 2");
insertRow(stmt, "Tracks",
"13, 2, 'Galileo', '', 2");
insertRow(stmt, "Tracks",
"14, 3, 'Ghost', '', 2");
insertRow(stmt, "Tracks",
"15, 4, 'Joking', '', 2");
insertRow(stmt, "Tracks",
"16, 5, 'Jonas & Ezekial', '', 2");
insertRow(stmt, "Tracks",
"17, 6, 'Love Will Come To You', '', 2");
insertRow(stmt, "Tracks",
"18, 7, 'Romeo and Juliet', '', 2");
insertRow(stmt, "Tracks",
"19, 8, 'Virginia Woolf', '', 2");
insertRow(stmt, "Tracks",
"20, 9, 'Chickenman', '', 2");
insertRow(stmt, "Tracks",
"21, 10, 'Airplane', '', 2");
insertRow(stmt, "Tracks",
"22, 11, 'Nashville', '', 2");
insertRow(stmt, "Tracks",
"23, 12, 'Let It Be Me', '', 2");
insertRow(stmt, "Tracks",
"24, 13, 'Cedar Tree', '', 2");
insertRow(stmt, "Tracks",
"25, 1, 'Real Love', '2:30', 3");
insertRow(stmt, "Tracks",
"26, 2, 'Twist and Shout', '2:32', 3");
insertRow(stmt, "Tracks",
"27, 3, 'Help', '2:17', 3");
insertRow(stmt, "Tracks",
"28, 4, 'In My Life', '2:25', 3");
insertRow(stmt, "Tracks",
"29, 5, 'Strawberry Fields Forever', '4:06', 3");
insertRow(stmt, "Tracks",
"30, 6, 'A Day in the Life', '5:00', 3");
insertRow(stmt, "Tracks",
"31, 7, 'Revolution', '3:21', 3");
insertRow(stmt, "Tracks",
"32, 8, 'The Ballad of John & Yoko', '2:58', 3");
insertRow(stmt, "Tracks",
"33, 9, 'Julia', '2:57', 3");
insertRow(stmt, "Tracks",
"34, 10, 'Don''t Let Me Down', '3:30', 3");
insertRow(stmt, "Tracks",
"35, 11, 'Give Peace A Chance', '3:30', 3");
insertRow(stmt, "Tracks",
"36, 12, 'How?', '3:37', 3");
insertRow(stmt, "Tracks",
"37, 13, 'Imagine (Rehearsal)', '1:26', 3");
insertRow(stmt, "Tracks",
"38, 14, 'God', '4:04', 3");
insertRow(stmt, "Tracks",
"39, 15, 'Mother', '5:15', 3");
insertRow(stmt, "Tracks",
"40, 16, 'Stand By Me', '3:30', 3");
insertRow(stmt, "Tracks",
"41, 17, 'Jealous Guy', '4:10', 3");
insertRow(stmt, "Tracks",
"42, 18, 'Woman', '3:32', 3");
insertRow(stmt, "Tracks",
"43, 19, 'Beautiful Boy', '3:54', 3");
insertRow(stmt, "Tracks",
"44, 20, '(Just Like) Starting Over', '4:05', 3");
insertRow(stmt, "Tracks",
"45, 21, 'Imagine', '3:00', 3");
insertRow(stmt, "Tracks",
"46, 6, 'Isn''t It Always Love', '3:06', 4");
insertRow(stmt, "Tracks",
"47, 7, 'If He''s Ever Near', '3:15', 4");
insertRow(stmt, "Tracks",
"48, 8, 'Flying High', '3:27', 4");
insertRow(stmt, "Tracks",
"49, 9, 'Falling Star', '4:27', 4");
insertRow(stmt, "Tracks",
"50, 10, 'Rose In My Garden', '4:44', 4");
insertRow(stmt, "Tracks",
"51, 1, 'Someone To Lay Down Beside Me', '4:03', 4");
insertRow(stmt, "Tracks",
"52, 2, 'I Can''t Hold On', '3:42', 4");
insertRow(stmt, "Tracks",
"53, 3, 'Lose Again', '3:40', 4");
insertRow(stmt, "Tracks",
"54, 5, 'Faces In The Wind', '3:04', 4");
insertRow(stmt, "Tracks",
"55, 4, 'Home', '4:17', 4");
insertRow(stmt, "Tracks",
"56, 3, 'I Know What I Know', '3:13', 5");
insertRow(stmt, "Tracks",
"57, 4, 'Gumboots', '2:42', 5");
insertRow(stmt, "Tracks",
"58, 5, 'Diamonds On The Soles Of Her Feet', '5:34', 5");
insertRow(stmt, "Tracks",
"59, 6, 'You Can Call Me Al', '4:39', 5");
insertRow(stmt, "Tracks",
"60, 7, 'Under African Skies', '3:34', 5");
insertRow(stmt, "Tracks",
"61, 8, 'Homeless', '3:45', 5");
insertRow(stmt, "Tracks",
"62, 9, 'Crazy Love Vol. II', '4:17', 5");
insertRow(stmt, "Tracks",
"63, 10, 'That Was Your Mother', '2:51', 5");
insertRow(stmt, "Tracks",
"64, 11, 'All Around The World or The Myth of FingerPrints', '3:15', 5");
insertRow(stmt, "Tracks",
"65, 1, 'The Boy in the Bubble', '3:59', 5");
insertRow(stmt, "Tracks",
"66, 2, 'Graceland', '4:48', 5");
insertRow(stmt, "Tracks",
"67, 3, 'Follow You Down', '4:30', 6");
insertRow(stmt, "Tracks",
"68, 4, 'Not Only Numb', '3:06', 6");
insertRow(stmt, "Tracks",
"69, 5, 'As Long As It Matters', '4:31', 6");
insertRow(stmt, "Tracks",
"70, 6, 'Perfectly Still', '4:05', 6");
insertRow(stmt, "Tracks",
"71, 7, '7TH Inning Stretch', '0:14', 6");
insertRow(stmt, "Tracks",
"72, 8, 'My Car', '4:17', 6");
insertRow(stmt, "Tracks",
"73, 9, 'Virginia', '4:02', 6");
insertRow(stmt, "Tracks",
"74, 10, 'Whitewash', '3:19', 6");
insertRow(stmt, "Tracks",
"75, 11, 'I Can''t Figure You Out', '3:13', 6");
insertRow(stmt, "Tracks",
"76, 12, 'Memphis Time', '3:14', 6");
insertRow(stmt, "Tracks",
"77, 13, 'Competition Smile', '3:38', 6");
insertRow(stmt, "Tracks",
"78, 1, 'Day Job', '3:52', 6");
insertRow(stmt, "Tracks",
"79, 2, 'Highwire', '2:24', 6");
insertRow(stmt, "Tracks",
"80, 3, 'Lucy In The Sky With Diamonds', '', 7");
insertRow(stmt, "Tracks",
"81, 4, 'Getting Better', '', 7");
insertRow(stmt, "Tracks",
"82, 5, 'Fixing A Hole', '', 7");
insertRow(stmt, "Tracks",
"83, 6, 'She''s Leaving Home', '', 7");
insertRow(stmt, "Tracks",
"84, 7, 'Being For The Benefit Of Mr. Kite!', '', 7");
insertRow(stmt, "Tracks",
"85, 9, 'When I''m Sixty-Four', '', 7");
insertRow(stmt, "Tracks",
"86, 8, 'Within You Without You', '', 7");
insertRow(stmt, "Tracks",
"87, 10, 'Lovely Rita', '', 7");
insertRow(stmt, "Tracks",
"91, 11, 'Sgt. Pepper''s Lonely Hearts Club Band (Reprise)', '', 7");
insertRow(stmt, "Tracks",
"88, 12, 'A Day In The Life', '', 7");
insertRow(stmt, "Tracks",
"89, 1, 'Sgt. Pepper''s Lonely Hearts Club Band', '', 7");
insertRow(stmt, "Tracks",
"90, 2, 'With a Little Help From My Friends', '', 7");
createTable(stmt, "ValidationOnly", "Col1 INTEGER NOT NULL, Col2 VARCHAR(15)"); //NOI18N
System.out.println("Music database was created.");
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("Error on url " + url );
System.err.println("ERROR: SQLException: " + ex.getMessage());
ex.printStackTrace();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
void createUser(Statement stmt, String dbname, String username, String password) throws SQLException {
try {
stmt.executeUpdate("DROP USER " + username); //NOI18N
} catch (SQLException e) {
}
stmt.executeUpdate("GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + username + "'@'localhost' IDENTIFIED BY '" + password + "' WITH GRANT OPTION");
}
void createSchema(Statement stmt, String schemaName) throws SQLException {
try {
stmt.executeUpdate("DROP SCHEMA " + schemaName); //NOI18N
} catch (SQLException e) {
}
stmt.executeUpdate("CREATE SCHEMA " + schemaName); //NOI18N
}
void createTable(Statement stmt, String name, String cols) throws SQLException {
stmt.executeUpdate("CREATE TABLE " + name + " (" + cols + ")"); //NOI18N
}
void dropTable(Statement stmt, String name) throws SQLException {
try {
stmt.executeUpdate("DROP TABLE " + name); //NOI18N
} catch (SQLException e) {
}
}
void createView(Statement stmt, String name, String select) throws SQLException {
try {
stmt.executeUpdate("DROP VIEW " + name); //NOI18N
} catch (SQLException e) {
}
stmt.executeUpdate("CREATE VIEW " + name + " AS " + select); //NOI18N
}
void addConstraint(Statement stmt, String name, String constraint) throws SQLException {
stmt.executeUpdate("ALTER TABLE " + name + " ADD CONSTRAINT " + constraint); //NOI18N
}
void insertRow(Statement stmt, String name, String values) throws SQLException {
stmt.executeUpdate("INSERT INTO " + name + " VALUES (" + values + ")"); //NOI18N
}
}
Message was edited by:
Willi2793
hide password