Tuesday, August 18, 2009

Setup MySQL Development in Eclipse

Using MySQL and Java? Check out an easier way: Yank

Setting up Eclipse as a MySQL development environment using the Database Development perspective is quite easy. Here you will find a step-by-step guide showing how to set up Eclipse for MySQL development. The tutorial shows how to get the right version of Eclipse, how to setup a MySQL database, how to get and use the Connector/J database driver, how to connect to a database within Eclipse, and how to create a table containing some sample data.

Step 1: Download and Install the Eclipse IDE for Java EE Developers.



Step 2: Download the Connector/J MySQL Database Driver.


Step 3: Download, Install, and start up MySQL. If you're developing on a Mac, see: Install MySQL on Mac OS X.

Step 4: Open up Eclipse and switch to the "Database Development" perspective (Window --> Open Perspective --> Other... --> Database Development perspective).




Step 5: Connect to your MySQL database.

In the "Data Source Explorer" view, right click on "Database Connections" and select "New...".



On the first page of the "New Connection Profile" Wizard, select the "MySQL" connection profile type and hit "Next >".

Click the circle with a plus sign next to the "Drivers" drop-down box.

On the "Name/Type" tab, select the MySQL JDBC Driver version 5.1 (or whatever version you downloaded). Just ignore the error in the wizard for now.

On the "Jar List" tab, add the driver .jar from the package that you downloaded and remove the default .jar that was added to the list. The error message in the wizard should now disappear. Click "OK".

On the second page of the "New Connection Profile" Wizard, edit the connection properties. In the "Database" and "URL" fields, change the word "database" to the actual name of the database you want to connect to. Change the URL, User name and Password appropriate to your circumstance. By default the MySQL database setup creates a root user with a blank password. Test the connection and hit "Finish".


Step 6: Create a table.

In the main Eclipse menu, select File --> New --> SQL File.



In the "New SQL File" wizard, select the folder where the SQL file should be stored. Create a new project if you don't have one yet. Give the file a name and hit "Finish".

In the SQL Editor view, write an SQL statement to add a table to the selected database. Highlight the text to be executed, right click, and choose "Execute Selected Text".


Step 7: Add data to the table.

In the "Data Source Explorer" view, dig down into the directory structure until you find your table in the "Tables" folder. Right click on it and choose Data --> Edit.



In the table editing view that appeared, add your data. Save to commit your changes.



Piece of cake!!

Uninstall MySQL on Mac OS X

Using MySQL and Java? Check out an easier way: Yank

Uninstalling MySQL on Mac OS X is very easy - you just have to delete some files. Here you will find instructions to delete both MySQL and the MySQL Startup Item, which you may have installed following the instructions found here: Install MySQL on Mac OS X. Be aware that doing this will delete all your MySQL databases and data.

Step 1: Uninstall MySQL. Fire up the Terminal and type in: cd /usr/local , hit enter, type in: ls , hit enter, and verify that there are two items called "mysql" and "mysql-5.1.37-osx10.5-x86_64" (or similar). These are the two items you want to delete. Type in: sudo rm mysql , hit enter, type in: sudo rm -rf mysql-5.1.37-osx10.5-x86_64 , and hit enter. You'll need to enter your password at some point when prompted.





Step 2: Uninstall the MySQL Startup Item. In Termainl type in: cd /Library/StartupItems , hit enter, type in: ls , hit enter, and verify that there is an item called "MySQLCOM". This is the item you want to delete. Type in: sudo rm -rf MySQLCOM , and hit enter. You'll need to enter your password at some point when prompted.

Piece of cake!!

See also: Install MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X
And also: Set MySQL Server System Variables on Mac OS X
And also: My Mac OS X MySQL Cheat Sheet

Monday, August 17, 2009

Install MySQL on Mac OS X

Using MySQL and Java? Check out an easier way: Yank

Update: I just verified that the 10.6 version works on Mountain Lion 10.8.

There are already several instructions out there on how to install MySQL on Mac OS X, but most of them are either outdated or more complicated than they need to be. Here you will find an easy tutorial showing how to install MySQL, start it up, access it as the default root user, create a database, delete the database and exit.

Step 1: Download MySQL. Go to the downloads page at mysql.com. Under the section "MySQL Community Server" click on the "Download" link. Find the particular version of MySQL that is appropriate for your OS and machine architecture. In my case, I downloaded the "Mac OS X 10.5 (x86, 64-bit)" version because I have Mac OS X 10.5 running on a MacBook with a 64-bit Intel processor. You want to download the DMG Archive, not the compressed TAR Archive.

Note: Use the 10.6 version for Snow Leopard, Lion, and Mountain Lion.





Step 2: Access the installers. Go the the directory where the .dmg file was downloaded to and mount the .dmg file by double clicking it. The .dmg contains the MySQL and MySQL Startup installer packages as well as the MySQL.prefPane.



Step 3: Install MySQL and the MySQL Startup Item. Double-clicking on mysql*.pkg icon starts an installer wizard that takes you through a few intuitive installation steps. Once you click through the wizard and accept a license agreement you should see the "install succeeded" page. Repeat for MySQLStartupItem.pkg and MySQL.prefPane.



Step 4: Verify Install. Fire up the Terminal and type in:
cd /usr/local
, hit enter, type in:
ls
, and verify that there are two items. The Mac OS X .pkg of MySQL installs itself into /usr/local/mysql-VERSION and /usr/local/mysql.



Step 5: Restart your computer. When the computer reboots, it will startup the database server allowing you to connect to it. You can also probably start MySQL without rebooting by starting it through the MySQL preference Pane under System Preferences although I haven't tried that. If you prefer the command line, the start and stop commands are listed at the bottom of this article.

Step 6: Use MySQL. Type in:
/usr/local/mysql/bin/mysql -u root -p
The last part of the command tells it you want to start it up as the root user and that you'd like to be prompted for a password. By default the root's password is blank, so when you are prompted for it, just hit enter. You are now using MySQL, and you can tell it what to do at the MySQL command prompt:
mysql>



Note about error 2002 people are reporting!


If after installing MySQL and trying to use MySQL as root user as in step 6, and you see this following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

, it's probably because MySQL is installed but not yet running. Make sure you've started MySQL as outlined in Step 5. To verify that it's running, open up Activity Monitor and under "All Processes", verify you see the process "mydqld".


If you installed the preference pane, you should also see there that MySQL is running.


Step 7: Create a database, delete it and exit MySQL. At the mysql> prompt, type in:
create database mynewdatabase;
Verify that it was created by typing in:
show databases;
You should see the database along with the default databases. Delete the database by typing in:
drop database mynewdatabase;
Verify that it was deleted by typing in:
show databases;
Exit MySQL by typing in:
exit;


Piece of Cake!!

In addition, here are the commands you need to start and stop MySQL:
sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop
sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart


See also: Uninstall MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X
And also: Set MySQL Server System Variables on Mac OS X
And also: My Mac OS X MySQL Cheat Sheet

Monday, August 10, 2009

Placing Two DIVs Side-by-Side using CSS

There is a little CSS trick needed when placing two HTML elements or DIVs side-by-side in order to prevent the two elements from jumping out of the containing DIV. In this simple demonstration there is an 'outer' DIV containing a 'header' DIV containing a 'title' DIV and a 'search' FORM. The FORM could just as well be a DIV for this to work. The title and search are forced left and right using 'float:left' and 'float:right' respectively. The trick is to add a third DIV after the search FORM inside the header DIV with a CSS 'clear:both' styling: <div style="clear:both;"></div>.

Before:



After:


HTML & CSS:


<html>
<head>
<style type="text/css">

#outer {
    width:600px;
    margin:0 auto;
    background:#BBBBBB;
    text-transform:uppercase;
    padding:10px;
}

#header {
    background:#0044FF;
    padding:5px 10px;
}

#title{
    background:#FF2244;
    padding:10px;
    float:left;
}

#search{
    background:#22CC22;
    padding:10px;
    float:right;
}

</style>
<body>
<div id='outer'>
    <div id='header'>
        <div id='title'>
            <p>Website Title</p>
        </div>
        <form id='search' method="get">
            <input type="text" name="searchText" />
            <input type="submit" value="Search" />
        </form> 
        <div style="clear:both;"></div>
    </div>
</div>
</body>
</html>


Piece of Cake!!