/* * TestMySQL.java * * * History: * * When Who What * ============================================================================== * 2001-07-23 Marc Liyanage First version, based on TestPostgreSQL.java * * * License: * * Copyright abandoned 2001 by Marc Liyanage * Do with this whatever you want. * */ import java.sql.*; /** * The TestMySQL class shows how to access the MySQL * DB server on Mac OS X using the JDBC interface. * It assumes the installation has been performed according * to the instructions at http://www.entropy.ch/software/macosx/mysql/. * * It further assumes that the MM.MySQL JDBC driver jar file from * http://mmmysql.sourceforge.net * has been installed in /usr/local/lib/mysql/ * * * You compile it like this: * * % javac TestMySQL.java * * Run the program like this, make sure to adjust the classpath * so it matches the version of the mm.mysql jar file you * installed: * * % java -classpath /usr/local/lib/mysql/mm.mysql-2.0.4-bin.jar:. TestMySQL * * You should see the current date as returned by the DB server: * * 2001-07-23 23:03:57 * * * @author Marc Liyanage * @version 1.0 */ public class TestMySQL { public static void main(String argv[]) throws Exception { // Load the driver class // Class.forName("org.gjt.mm.mysql.Driver"); // Try to connect to the DB server. // We tell JDBC to use the "mysql" driver // and to connect to the "test" database // which should always exist in MySQL. // // We use the username "" and no // password to connect. This should always // work for the "test" database. // Connection conn = DriverManager.getConnection( "jdbc:mysql:///test", "", "" ); // Set up and run a query that fetches // the current date using the "now()" SQL function. // Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("SELECT now();"); // Iterate through the rows of the result set // (obviously only one row in this example) and // print each one. // while (rset.next()) { System.out.println(rset.getString(1)); } // Close result set, statement and DB connection // rset.close(); stmt.close(); conn.close(); } }