KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleApplicationDemo


1
2 /**
3  * This demonstrates a simple stand-alone database application. This will
4  * start up the database that's found in this directory and perform some
5  * queries on the data.
6  * <p>
7  * The demo distribution should contain the database data files already
8  * prepared. However, if the 'data' directory was removed then run
9  * 'SimpleDatabaseCreateDemo' to remake it.
10  */

11
12 import java.sql.*;
13
14 public class SimpleApplicationDemo {
15
16   /**
17    * The demonstation 'main' 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 connecting with a local database. The
37
// configuration file for the database is found at './ExampleDB.conf'
38
String JavaDoc url = "jdbc:mckoi:local://ExampleDB.conf";
39
40     // The username/password for the database. This is set when the database
41
// is created (see SimpleDatabaseCreateDemo).
42
String JavaDoc username = "user";
43     String JavaDoc password = "pass1212";
44
45     // Make a connection with the database.
46
Connection connection;
47     try {
48       connection = DriverManager.getConnection(url, username, password);
49     }
50     catch (SQLException e) {
51       System.out.println(
52      "Unable to make a connection to the database.\n" +
53      "The reason: " + e.getMessage());
54       return;
55     }
56
57     // --- Do some queries ---
58
System.out.println();
59
60     try {
61       // Create a Statement object to execute the queries on,
62
Statement statement = connection.createStatement();
63       ResultSet result;
64
65       // How many rows are in the 'Person' table?
66
result = statement.executeQuery("SELECT COUNT(*) FROM Person");
67       if (result.next()) {
68     System.out.println("Rows in 'Person' table: " + result.getInt(1));
69       }
70       System.out.println();
71
72       // What's the average age of the people stored?
73
result = statement.executeQuery("SELECT AVG(age) FROM Person");
74       if (result.next()) {
75     System.out.println("Average age of people: " + result.getDouble(1));
76       }
77       System.out.println();
78
79       // List the names of all the people that live in Africa ordered by name
80
result = statement.executeQuery(
81       "SELECT name FROM Person WHERE lives_in = 'Africa' ORDER BY name");
82       System.out.println("All people that live in Africa:");
83       while (result.next()) {
84     System.out.println(" " + result.getString(1));
85       }
86       System.out.println();
87
88       // List the name and music group of all the people that listen to
89
// either 'Oasis' or 'Beatles'
90
result = statement.executeQuery(
91       " SELECT Person.name, MusicGroup.name " +
92       " FROM Person, ListensTo, MusicGroup " +
93       " WHERE MusicGroup.name IN ( 'Oasis', 'Beatles' ) " +
94       " AND Person.name = ListensTo.person_name " +
95       " AND ListensTo.music_group_name = MusicGroup.name " +
96       " ORDER BY MusicGroup.name, Person.name ");
97       System.out.println("All people that listen to either Beatles or Oasis:");
98       while (result.next()) {
99     System.out.print(" " + result.getString(1));
100     System.out.print(" listens to ");
101     System.out.println(result.getString(2));
102       }
103       System.out.println();
104
105       // Close the statement and the connection to end,
106
statement.close();
107       connection.close();
108
109     }
110     catch (SQLException e) {
111       System.out.println(
112     "An error occured\n" +
113     "The SQLException message is: " + e.getMessage());
114       return;
115     }
116
117   }
118
119 }
120
Popular Tags