KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleDatabaseCreateDemo


1
2 /**
3  * Demonstrates how to create a database through the JDBC driver. This
4  * will create a database in the local directory, create three tables and
5  * fill them with sample data.
6  * <p>
7  * This demo distribution should contain a created database in the local
8  * directory. If the 'data' directory doesn't exist or was deleted then it
9  * can be recreated by running this.
10  */

11
12 import java.sql.*;
13
14 public class SimpleDatabaseCreateDemo {
15
16   /**
17    * Application start method.
18    */

19   public static void main(String JavaDoc[] args) {
20
21     System.out.println();
22
23     // Register the Mckoi JDBC Driver
24
try {
25       Class.forName("com.mckoi.JDBCDriver").newInstance();
26     }
27     catch (Exception JavaDoc 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     // This URL specifies we are creating a local database. The
37
// configuration file for the database is found at './ExampleDB.conf'
38
// The 'create=true' argument means we want to create the database. If
39
// the database already exists, it can not be created.
40
String JavaDoc url = "jdbc:mckoi:local://ExampleDB.conf?create=true";
41
42     // The username/password for the database. This will be the username/
43
// password for the user that has full control over the database.
44
// ( Don't use this demo username/password in your application! )
45
String JavaDoc username = "user";
46     String JavaDoc password = "pass1212";
47
48     // Make a connection with the database. This will create the database
49
// and log into the newly created database.
50
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     // --- Set up the database ---
62

63     try {
64       // Create a Statement object to execute the queries on,
65
Statement statement = connection.createStatement();
66       ResultSet result;
67
68       System.out.println("-- Creating Tables --");
69
70       // Create a Person table,
71
statement.executeQuery(
72       " CREATE TABLE Person ( " +
73       " name VARCHAR(100) NOT NULL, " +
74       " age INTEGER, " +
75       " lives_in VARCHAR(100) ) " );
76
77       // Create a ListensTo table (which person listens to what music)
78
statement.executeQuery(
79       " CREATE TABLE ListensTo ( " +
80       " person_name VARCHAR(100) NOT NULL, " +
81       " music_group_name VARCHAR(250) NOT NULL ) ");
82
83       // Create a MusicGroup table
84
statement.executeQuery(
85       " CREATE TABLE MusicGroup ( " +
86       " name VARCHAR(250) NOT NULL, " +
87       " country_of_origin VARCHAR(100) ) ");
88
89       // Insert records into the tables,
90

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       // Who listens to what music?
148
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       // Close the statement and the connection.
180
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     // Close the the connection.
192
try {
193       connection.close();
194     }
195     catch (SQLException e2) {
196       e2.printStackTrace(System.err);
197     }
198
199   }
200
201 }
202
Popular Tags