1 2 11 12 import java.sql.*; 13 14 public class SimpleApplicationDemo { 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"; 39 40 String username = "user"; 43 String password = "pass1212"; 44 45 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 System.out.println(); 59 60 try { 61 Statement statement = connection.createStatement(); 63 ResultSet result; 64 65 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 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 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 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 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 |