MySQL Documentation

Connecting to the MySQL data base manager (applies to v 3.23.54)

On the client side, there needs to be a MySQL driver file (xxxxx.jar) in the CLASSPATH. I am using mysql-connector-java-3.0.11-stable.jar obtained from http://www.mysql.com/downloads/api-jdbc-stable.html. This particular one is needed for MySQL vs. 3-4 and sun java v 1.4. In what follows it is assumed MySQL v 3.23 is already installed. The example that follows is taken from from experiments on gauss.ececs.uc.edu as the server machine and my laptop as the client machine. Hence there are numerous references to franco. These should be changed accordingly for other users. A database server is started, a database created, and data items added using the steps below.

  1. Start the dbms server. Use the following command as user root on a Linux machine:

       sudo service mysqld start
    

    The server is now listening.

  2. A Database must be created. Use the following command on the machine that will be running the server.

       mysql -u franco -h localhost
       mysql> create database franco_db 
    

  3. Grant permission to access the database.

          mysql> grant all privileges on franco_db.* to franco@laptop.ececs.uc.edu
    

  4. The Driver classes must be visible to the client Java code. On the client machine, suppose all the code is to be run from the current directory. We recommend renaming the jar file to connector.jar. Put connector.jar somewhere accessible, say /usr/lib/java on the client's machine. Before running any Java code execute the following:

          export CLASSPATH=$CLASSPATH:/usr/lib/java/connector.jar
    

    This is done a little differently, but with the same effect, in this Makefile.

  5. Connect to franco_db. To connect to the franco_db database on host gauss.ececs.uc.edu use:

          String url = "jdbc:mysql://gauss.ececs.uc.edu/franco_db";
          ...
          try {
             Class.forName("com.mysql.jdbc.Driver");
          }
          ...
          con = DriverManager.getConnection(url, "franco", "");
    

    See, for example, CreateCoffees.java. Running that class file adds COFFEES to the franco_db database. The same holds for CreateSuppliers. You can run the code as follows:

         java -Djdbc.drivers=com.mysql.jdbc.Driver CreateCoffees gauss.ececs.uc.edu
    

    But in the given source code the Driver is specified as

         Class.forName("com.mysql.jdbc.Driver");
    

    so the -D option is not necessary. Use the following Java line to see verbose messages (on the console) while connecting:

         DriverManager.setLogWriter(new printWriter(System.out));
    

    Do the same for CreateSuppliers.java. After InsertCoffees and InsertSuppliers compile and run PrintColumns.java to see the new table. Display the results using OutputApplet.java.

  6. Procedure for tunneling to gauss.

  7. Using this Makefile to compile and run the code, do the following: