Second Summer 2002


Lecture Notes Thirty-One: In which we look at embedded SQL.

Before we forget, the problem for today is:

Implement the following behaviour

burrowww.cs.indiana.edu% ./number
Hi there, are you ready?
I have chosen a number (between 1 and 100). 
You need to guess it in at most 10 tries.
Guess1> 50
Try lower.
Guess2> 25
Try higher.
Guess3> 27
Try higher.
Guess4> 36
Try lower.
Guess5> 30
Try lower.
Guess6> 28
Try higher.
Guess7> 29
Congratulations!You guessed in 7 attempts.
burrowww.cs.indiana.edu% 

The topic for today is: RDBMS and Java. JDBC.

What you need:

Here's how you connect, and create tables.

import java.sql.*; 

public class CreateTable { 
    public static void main(String[] args) {
        try { 
            Class.forName("org.gjt.mm.mysql.Driver"); 
        } catch (Exception e) { 
            System.out.println("Can't load JDBC Driver. Make sure classpath is correct."); 
        }
        String url = "jdbc:mysql://localhost/a348", 
            username = "a348", 
            password = "a348AG"; 
        Connection connection; 

        try {
            connection = DriverManager.getConnection(url, username, password); 

            Statement statement = connection.createStatement(); 
            ResultSet result; 
            System.out.println("Creating tables..."); 

            statement.executeUpdate(
              " create table dgerman_person ( "                  + 
              "   name      varchar(100) not null primary key, " + 
              "   age       int unsigned not null            , " + 
              "   lives_in  varchar(100) not null              " + 
              " ) " 
            ); 

            statement.close(); 
            connection.close(); 

        } catch (SQLException e) { 
            System.out.println("An SQLException occurred: " + e.getMessage()); 
        } catch (Exception e) {
            System.out.println("Exception: " + e); 
        }

    }
} 
Here's how you populate the tables with data.

import java.sql.*; 

public class InsertData { 
    public static void main(String[] args) {
        try { 
            Class.forName("org.gjt.mm.mysql.Driver"); 
        } catch (Exception e) { 
            System.out.println("Can't load JDBC Driver. Make sure classpath is correct."); 
        }
        String url = "jdbc:mysql://localhost/a348", 
            username = "a348", 
            password = "a348AG"; 
        Connection connection; 

        try {
            connection = DriverManager.getConnection(url, username, password); 

            Statement statement = connection.createStatement(); 
            ResultSet result; 
            System.out.println("Inserting data..."); 

            statement.executeUpdate(
              " insert into dgerman_person values              " + 
              "   ('David Beckham  ', 24, 'England'),          " + 
              "   ('Roger Milla    ', 25, 'Africa'),           " + 
              "   ('George Weah    ', 24, 'Africa'),           " + 
              "   ('Tony Meola     ', 25, 'USA'   ),           " + 
              "   ('Zinedine Zidane', 25, 'France')             " 

            ); 

            statement.close(); 
            connection.close(); 
            
        } catch (SQLException e) { 
            System.out.println("An SQLException occurred: " + e.getMessage()); 
        } catch (Exception e) {
            System.out.println("Exception: " + e); 
        }

    }
} 
Here's how you extract data from the database.

import java.sql.*; 

public class SelectData { 
    public static void main(String[] args) {
        try { 
            Class.forName("org.gjt.mm.mysql.Driver"); 
        } catch (Exception e) { 
            System.out.println("Can't load JDBC Driver. Make sure classpath is correct."); 
        }
        String url = "jdbc:mysql://localhost/a348", 
            username = "a348", 
            password = "a348AG"; 
        Connection connection; 

        try {
            connection = DriverManager.getConnection(url, username, password); 

            Statement statement = connection.createStatement(); 
            ResultSet result; 
            System.out.println("Querying database..."); 

            result = statement.executeQuery(
              " select name, age from dgerman_person    " + 
              "        where lives_in = 'Africa'        "   
            ); 

            while (result.next()) {
                System.out.println(" " + result.getString(1) + 
                                   ", " + result.getString(2)); 
            }

            statement.close(); 
            connection.close(); 

        } catch (SQLException e) { 
            System.out.println("An SQLException occurred: " + e.getMessage()); 
        } catch (Exception e) {
            System.out.println("Exception: " + e); 
        }

    }
}
We will run these programs and verify their operation from the mysql command prompt.


Last updated: Aug  8, 2002 by Adrian German for A348/A548