Installation of MySQL
Castor supports all the well-known high-end databases like Oracle, DB2, MS
SQL Server, but also Open Source databases like MySQL and Postgres. Here's the complete list of
supported database systems. For all the examples in this article I've used
MySQL, and for those of you who don't already have a database running on your
computer I'll show you how to get one.
You're always told that software is easy to install, right? This time it's
true! MySQL is really simple to install and use. To use MySQL we need three
software components:
- the MySQL server
- a GUI client to administer the MySQL server
- a Java JDBC driver
You'll find them all on the MySQL download page: http://www.mysql.com/downloads/index.html.
The MySQL server: The production release is currently 3.23, but there
is a "gamma" release called 4.0 which is recommended for new development, so
let's pick that one. I used version 4.0.10 for this article. It's quite a bit to
download, more than 20M for my Windows98 computer. On Windows you unzip the
files, run the setup.exe, install in c:\mysql, and you're ready to go.
There's a very comprehensive manual in the download, but why not start the
server right away? Open a command prompt, go to the mysql\bin directory and type
"mysqld", and the server is running. Verify this by typing "mysql" and you're in
the server administrator. Now type "show databases;"--remember the semicolon!
This will list the two databases--mysql and test--which come with
the download. Enter "use test", and then create a new table called "media" in
the "test" database:
create table media (id int primary key, type char(10) not null);
Enter "exit" to leave the administrator.
The GUI client: Several clients exist. On the MySQL download page
you'll find a link to "MySQL Control Center", which is a client that'll give you
all the essential features you'd need to administer MySQL. Installation is
straightforward. I used version 0.8.10 when preparing this article.
You should be able to see the new "media" table in the "test" database.
The JDBC driver: Also on the download page you'll find "MySQL
Connector/J", which is the official JDBC driver. Unzip the file you've
downloaded and place the jar-file (there's only one) in your classpath. To see
if your setup is correct try to compile and run this simple program that'll show
the names of all tables in the "test" data base:
package hansen.playground;
import java.sql.*;
public class TestMySQL {
public static void main(String[] args) {
Connection connection = null;
try {
// Load the MySQL JDBC driver
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName);
// Create a connection to the "test" database
String serverName = "localhost";
String mydatabase = "test";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root"; // default
String password = ""; // default
connection = DriverManager.getConnection(url, username, password);
// Get the database metadata
DatabaseMetaData dbmd = connection.getMetaData();
// Specify the type of object; in this case we want tables
String[] types = {"TABLE"};
ResultSet resultSet = dbmd.getTables(null, null, "%", types);
// Get the table names
while (resultSet.next()) {
// Get the table name
String tableName = resultSet.getString(3);
System.out.println("Table: " + tableName);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- Listing 1: Testprogram for MySQL -
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|