import java.sql.*;
import java.net.URL;

// Tecfa Note:
// Kevin Boone, August 98
// Example COPIED from http://www.cs.mdx.ac.uk/staffpages/kevin/JdbcTest-java.html
// This is copyrighted stuff, but ok to use
// Please look at the site and DON'T copy from here

class JdbcTest
{
    public static void main (String args[])
    {
	// Because I'm lazy, I enclose the entire program in 
	//  a `try' block, so that Java will automatically
	//  stop the program and print an error message
	//  if an error occurs. 
	try
	    {
		// This next line loads the class `gweMysqlDriver'
		// This is the vendor-specific JDBC driver for the
		//  database server I use. This line will throw
		//  `ClassNotFound' exception if the driver class
		//  does not exist.
		Class.forName ("gwe.sql.gweMysqlDriver");

		// Database URLs (i.e., addresses) have a very
		//  ugly format. It is: 
		//  "jdbc:type://server_address:port/database_name"
		//  'localhost' means 'on this computer'
		//  'cd' is the name of the database
		//  3333 is the default port number for this server
		String url = "jdbc:mysql://localhost:3333/cd";

		// Establish a connection to the database at the
		//  URL (address) given above
		// We ahve to give a username and password here
		//  I use the id `guest' for general read-only access
		Connection connection = DriverManager.getConnection
		    (url, "guest", "guest");
                                  
		// The following is the SQL query to execute
		String query = 
		    "select shorttitle from kevin_cd "
		    + "where shorttitle like \"%bach%\"";

		// If the query is badly formed, the program will
		//  not get to this line. But even if it does,
		//  there may be no data to display.
		// Loop through the results returned by the query,
		//  printing the first field from each. There is
		//  only one field `shorttitle' in the query, so
		//  we must ask for `getString(1)'. Note that
		//  field numbers start at ONE not zero like arrays.
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery (query);
		while (resultSet.next())
		    {
			System.out.println (resultSet.getString(1));
		    }
	    } 
	catch (Exception e)
	    {
		System.out.println(e.toString());
	    }
    }
}
