center of tech

TOTD #97 showed how to install GlassFish Tools Bundle for Eclipse 1.1. Basically there are two options - either install Eclipse 3.4.2 with WTP and pre-bundled/configured with GlassFish v2/v3, MySQL JDBC driver and other features. Or if you are using Eclipse 3.5, then you can install the plug-in separately and get most of the functionality.
TOTD #98 showed how to create a simple Metro/JAX-WS compliant Web service using that bundle and deploy on GlassFish.
This Tip Of The Day (TOTD) shows how to create a simple Java EE 6 application that reads data from a MySQL database using JPA 2.0 and Servlet 3.0 and display the results. A more formal support of Java EE 6/Servlet 3.0 is coming but in the meanwhile the approach mentioned below will work.
Lets get started!



This blog uses MySQL sample database sakila. So please download and install the sample database before proceeding further.

Notice the "Drivers" indicate that the JDBC driver is pre-bundled so there is no extra configuration required. If you are using a stand-alone Eclipse bunde and installing the plugin separately, then you need to configure the MySQL JDBC driver explictily.
The URL indicates the application is connecting to the sakila database. Click on "Test Connection" to test connection with the database and see the output as:

and click on "Finish" to complete. The expanded database in the explorer looks like:

The expanded view shows all the tables in the database.

Only the project name needs to be specified and everything else is default. Notice the target runtime indicates that this is a Java EE 6 application. Click on "Finish".


Make sure to disable "orm.xml" since we are generating a standard Java EE 6 web application. Choose "sakila" as the database. Click on "OK" and again on "OK" to complete the dialog.


and click on "Next >". If no values are shown in the schema drop-down, then click on "Reconnect …".

and click on "Finish". The "film" and "language" table are related so it would be nice if all the related tables can be identified and picked accordingly.
Anyway this generates "model.Film" and "model.Language" classes and "persistence.xml" as shown below:

Also notice that "web.xml" and "sun-web.xml" have been explicitly removed since they are not required by a Java EE 6 application.

and click on "Finish". This class will be our Servlet client.
@WebServlet(urlPatterns="/ServletClient")
public class ServletClient extends HttpServlet {
@PersistenceUnit
EntityManagerFactory factory;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletOutputStream out = resp.getOutputStream();
List list = factory.createEntityManager().createQuery("select f from Film f where f.title like 'GL%';").getResultList();
out.println("<html><table>");
for (Object film : list) {
out.print("<tr><td>" + ((Film)film).getTitle() + "</tr></td>");
}
out.println("</table></html>");
}
}
and the imports as:
import java.io.IOException; import java.util.List; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.Film;
Basically, this is a Servlet 3.0 specification compliant Servlet that uses @WebServlet annotation. It uses @PersistenceUnit to inject the generated JPA Persistence Unit which is then used to query the database. The database query return all the movies whose title start with "GL" and the response is displayed in an HTML formatted table.

and click on "Finish". The output at "http://localhost:8080/HelloJPA/ServletClient" looks like:

Simple, easy and clean!
How are you using Eclipse and GlassFish - the consolidated bundle or standalone Eclipse + GlassFish plugin ?
Download GlassFish Tools Bundle for Eclipse now.
Technorati: glassfish eclipse mysql jpa database
Source/Kaynak : http://blogs.sun.com/arungupta/entry/totd_99_creating_a_java