1 2 11 12 import java.sql.*; 13 14 public class SimpleDatabaseCreateDemo { 15 16 19 public static void main(String [] args) { 20 21 System.out.println(); 22 23 try { 25 Class.forName("com.mckoi.JDBCDriver").newInstance(); 26 } 27 catch (Exception e) { 28 System.out.println( 29 "Unable to register the JDBC Driver.\n" + 30 "Make sure the classpath is correct.\n" + 31 "For example on Win32; java -cp ../../mckoidb.jar;. SimpleApplicationDemo\n" + 32 "On Unix; java -cp ../../mckoidb.jar:. SimpleApplicationDemo"); 33 return; 34 } 35 36 String url = "jdbc:mckoi:local://ExampleDB.conf?create=true"; 41 42 String username = "user"; 46 String password = "pass1212"; 47 48 Connection connection; 51 try { 52 connection = DriverManager.getConnection(url, username, password); 53 } 54 catch (SQLException e) { 55 System.out.println( 56 "Unable to create the database.\n" + 57 "The reason: " + e.getMessage()); 58 return; 59 } 60 61 63 try { 64 Statement statement = connection.createStatement(); 66 ResultSet result; 67 68 System.out.println("-- Creating Tables --"); 69 70 statement.executeQuery( 72 " CREATE TABLE Person ( " + 73 " name VARCHAR(100) NOT NULL, " + 74 " age INTEGER, " + 75 " lives_in VARCHAR(100) ) " ); 76 77 statement.executeQuery( 79 " CREATE TABLE ListensTo ( " + 80 " person_name VARCHAR(100) NOT NULL, " + 81 " music_group_name VARCHAR(250) NOT NULL ) "); 82 83 statement.executeQuery( 85 " CREATE TABLE MusicGroup ( " + 86 " name VARCHAR(250) NOT NULL, " + 87 " country_of_origin VARCHAR(100) ) "); 88 89 91 System.out.println("-- Inserting Data --"); 92 93 System.out.println("-- Adding to Person Table --"); 94 statement.executeQuery( 95 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 96 " ( 'Robert Bellamy', 24, 'England' ) "); 97 statement.executeQuery( 98 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 99 " ( 'Grayham Downer', 59, 'Africa' ) "); 100 statement.executeQuery( 101 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 102 " ( 'Timothy French', 24, 'Africa' ) "); 103 statement.executeQuery( 104 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 105 " ( 'Butch Fad', 53, 'USA' ) "); 106 statement.executeQuery( 107 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 108 " ( 'Judith Brown', 34, 'Africa' ) "); 109 statement.executeQuery( 110 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 111 " ( 'Elizabeth Kramer', 24, 'USA' ) "); 112 statement.executeQuery( 113 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 114 " ( 'Yamnik Wordsworth', 14, 'Australia' ) "); 115 statement.executeQuery( 116 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 117 " ( 'Domonic Smith', 25, 'England' ) "); 118 statement.executeQuery( 119 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 120 " ( 'Ivan Wilson', 23, 'England' ) "); 121 statement.executeQuery( 122 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 123 " ( 'Lisa Williams', 24, 'England' ) "); 124 statement.executeQuery( 125 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 126 " ( 'Xenia, Warrior Princess', 32, 'Rome' ) "); 127 statement.executeQuery( 128 " INSERT INTO Person ( name, age, lives_in ) VALUES " + 129 " ( 'David Powell', 25, 'New Zealand' ) "); 130 131 System.out.println("-- Adding to MusicGroup Table --"); 132 statement.executeQuery( 133 " INSERT INTO MusicGroup " + 134 " ( name, country_of_origin ) VALUES " + 135 " ( 'Oasis', 'England' ), " + 136 " ( 'Fatboy Slim', 'England' ), " + 137 " ( 'Metallica', 'USA' ), " + 138 " ( 'Nirvana', 'USA' ), " + 139 " ( 'Beatles', 'England' ), " + 140 " ( 'Fela Kuti', 'Africa' ), " + 141 " ( 'Blur', 'England' ), " + 142 " ( 'Muddy Ibe', 'Africa' ), " + 143 " ( 'Abba', 'Sweden' ), " + 144 " ( 'Madonna', 'USA' ), " + 145 " ( 'Cure', 'England' ) " ); 146 147 System.out.println("-- Adding to ListensTo Table --"); 149 statement.executeQuery( 150 " INSERT INTO ListensTo " + 151 " ( person_name, music_group_name ) VALUES " + 152 " ( 'David Powell', 'Metallica' ), " + 153 " ( 'David Powell', 'Cure' ), " + 154 " ( 'Xenia, Warrior Princess', 'Madonna' ), " + 155 " ( 'Lisa Williams', 'Blur' ), " + 156 " ( 'Lisa Williams', 'Cure' ), " + 157 " ( 'Lisa Williams', 'Beatles' ), " + 158 " ( 'Ivan Wilson', 'Cure' ), " + 159 " ( 'Ivan Wilson', 'Beatles' ), " + 160 " ( 'Yamnik Wordsworth', 'Abba' ), " + 161 " ( 'Yamnik Wordsworth', 'Fatboy Slim' ), " + 162 " ( 'Yamnik Wordsworth', 'Fela Kuti' ), " + 163 " ( 'Elizabeth Kramer', 'Nirvana' ), " + 164 " ( 'Judith Brown', 'Fela Kuti' ), " + 165 " ( 'Judith Brown', 'Muddy Ibe' ), " + 166 " ( 'Butch Fad', 'Metallica' ), " + 167 " ( 'Timothy French', 'Blur' ), " + 168 " ( 'Timothy French', 'Oasis' ), " + 169 " ( 'Timothy French', 'Nirvana' ), " + 170 " ( 'Grayham Downer', 'Fela Kuti' ), " + 171 " ( 'Grayham Downer', 'Beatles' ), " + 172 " ( 'Robert Bellamy', 'Oasis' ), " + 173 " ( 'Robert Bellamy', 'Beatles' ), " + 174 " ( 'Robert Bellamy', 'Abba' ), " + 175 " ( 'Robert Bellamy', 'Blur' ) " ); 176 177 System.out.println("--- Complete ---"); 178 179 statement.close(); 181 connection.close(); 182 183 } 184 catch (SQLException e) { 185 System.out.println( 186 "An error occured\n" + 187 "The SQLException message is: " + e.getMessage()); 188 189 } 190 191 try { 193 connection.close(); 194 } 195 catch (SQLException e2) { 196 e2.printStackTrace(System.err); 197 } 198 199 } 200 201 } 202 | Popular Tags |