In this part we'll create a database named 'test' and then work with this database using eclipse. So, let's start. Before you can connect to MySQL Server through Eclipse, you need to download mysql-connector-java-5.1.18.jar and include it into your project libraries using Build Path. Download the jar file from http://www.mysql.de/downloads/connector/j/ as in the figure:
In your project explorer Eclipse right-click on your project and click on Build Path -> Configure Build Path. In the Libraries tab click on Add External JARs and then browse to your directory where you have downloaded the jar file. Your window would look like the following; click Ok.
Now we'll create our test database. Open MySQL Command Line Client as shown in the previous part and give the command create database test. This will create a database named test as in the figure below:
In the lower panel of eclipse you could find Data Source Explorer as in the figure below:
Drag this tab and drop it in the left panel besides Project Explorer tab as shown in the following figure so that you can have both views on left panel this will make things easy to use in future.
If you don't see Data Source Explorer here, then in eclipse menubar, click on Window -> Open Perspective -> other -> Database Development as in the figure below; this will open Data Source Explorer in left panel. To switch back to your Project Explorer, click on Window -> Open Perspective -> other -> Java EE (default).
In the Data Source Explorer right-click on Database Connections and click New.
In Connection Profile window select MySQL and in the name field write Test MySQL; click Next.
On the upper right corner click on New Driver Definition button.
In New Driver Definition window, select MySQL JDBC Driver MySQL 5.1 and then click JAR Listtab.
In JAR List tab, select the default driver and click on Edit JAR/Zip... button.
Select the jar file which you have downloaded above, click Open and then click Ok.
In the General tab under Properties, change the database name in the Database and URL field to your database name as in the figure. In our case it is test. Give your password which you have created in the previous part. Check Save password, and click on Test Connection and you will see ping successful message. Click Finish.
To create tables in database using eclipse, select your database test, then open the scrapbook by clicking on the button placed in the right corner of Data Source Explorer as in the figure:
In the scrapbook write the following query to create a table customer.
Right-click in the scrapbook and click Execute All.
You will see the see the success message in the SQL Result tab.
You can view your tables in In the Data Source Explorer like this.
Insert some rows in the table by right-clicking on your table and then clicking Data -> Edit
We now move on to real stuff. In the next step we'll use this database and create entity class using JPA 2.0 and Eclipselink. So, click Next.
No comments:
Post a Comment