Spring Semester 2003


Lab Notes Twelve: JDBC, Content Syndication Reviewed, XSLT.

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: Apr 17, 2003 by Adrian German for A348/A548