SIMS 255: Lab 14: Database Tutorial

Wednesday, November 27, 2002

Connecting to Your Database on Dream

To connect to your database from the Unix command line:
  1. Use SecureCRT or some other program to login to dream.sims.berkeley.edu

  2. At the 'Dream%' prompt, type:

    mysql

    And press enter.

  3. Type your password (same as your login name) and press enter.

  4. At the 'mysql>' prompt, type (substituting your login name for 'loginname' below):

    use loginname

    And press enter.

  5. To display a list of the tables that currently exist in your database, type (NOTE the semicolon at the end of the line!):

    show tables;

    And press enter.

  6. To display the fields that exist in the 'mytable' table, type:

    describe mytable;

    And press enter.

  7. To display the records that exist in the 'mytable' table, type:

    select * from mytable;

    And press enter.

  8. To exit mysql, type:

    quit

  9. Information on using mysql at SIMS can be found here. Complete mysql documentation can be found here.

Connecting to Your Database Using Java

The Java file from Assignment 9 provides code that enables you to use Java to connect to a database. To run the code, you'll need to include a jar file in the classpath, because it contains the mysql driver.

You'll also need to modify the TestDB.java file slightly. In the main() method, you'll need to change the 'dblogin' variable to your own username. Change this line so that your username is within the quotes.

String dblogin = "yourlogin";

Following are instructions for including the jar file and running the code from the Unix command line, TextPad, and Eclipse.

Unix Command Line

  1. Copy the TestDB.java and mysql.jar files to your H drive, wherever you keep your Java projects.

  2. Login to Dream, and navigate to the location where you put the Java and JAR files.

  3. To compile the Java file, at the 'Dream%' prompt, type:

    javac TestDB.java

    And press enter.

  4. To run the Java file and include the JAR file, at the 'Dream%' prompt, type:

    java -classpath .:mysql.jar TestDB

    (Note that the classpath separator in windows is ";" but in Unix is ":")

  5. You can now enter SQL commands in the Java console.

TextPad

  1. Copy the TestDB.java and mysql.jar files to a directory of your choice. (Put the two files in the same directory.)

  2. Launch TextPad.

  3. Open TestDB.java.

  4. Select Configure > Preferences > Tools > Run Java Application, and make sure that Prompt for parameters is checked, and Capture output is unchecked.

  5. Select Tools > Compile Java.

  6. Select Tools > Run Java Application. At the prompt, type

    -classpath .;mysql.jar TestDB

    (Note that the classpath separator in windows is ";" but in Unix is ":")

  7. You can now enter SQL commands in the Java console.

Eclipse

  1. Copy the TestDB.java and mysql.jar files to the root of your H drive (don't copy them into the Eclipse\workspace directory).

  2. Launch Eclipse.

  3. Create a new Java project called TestDB.

  4. Right-click on the TestDB project, choose Import... > File System. Click Next, then click Browse, navigate to the H drive, and click OK. Check the TestDB.java file, and click Finish.

  5. To include the jar file:
    1. Right-click on the TestDB project.
    2. Choose Properties from the context-sensitive menu.
    3. Click on Java Build Path.
    4. Click on the Libraries Tab.
    5. Click the Add External JARs... button.
    6. Navigate to the H drive, select mysql.jar, and click Open.
    7. Click OK to close the Properties window.

  6. Open the TestDB.java file.

  7. Choose Run > Run As > Java Application.

  8. You can now enter SQL commands in the Java console.


LZ -- last modified 11/24/02