views:

892

answers:

5

Hello

I just started to learning Eclipse with Glassfish server. I was looking around how can I make simple database web application, but can't figure out yet. I downloaded the Glassfish bundle for Eclipse. I need to create simple database ( perhaps one table ), and connect the database with simple web application. How to do that in Eclipse , can you give me some step by step link how can I do this Thank you

+4  A: 

For the database you could do worse than use JavaDb, which comes as standard with Java 6. Check out the JDBC tutorial for details on interfacing Java to a database.

Brian Agnew
+2  A: 

At this point, you have a webapp server and an IDE. Both of which are designed to be semi-agnostic when it comes to databases. The next choice is how you want to interact with the database. Then you should be able to find more information and examples on how to start constructing your webapp.

I'd suggest going with spring and stripes (http://www.springsource.org/ and http://www.stripesframework.org/display/stripes/Home respectively), but that's a personal choice.

Quotidian
+3  A: 

First get the terminology and the software right: Glassfish is a Java EE application server supporting under each JSP/Servlet. Eclipse is an IDE, just a development tool. None of them both are databases.

Thus, you actually need to install a separate database server next to them. There are five major DB vendors known at the world: MySQL, PostgreSQL, DB2, Microsoft SQL and Oracle. Just choose the one you like and/or can afford. On the other hand, you can also choose an embedded database, like JDK's built-in JavaDB.

Once done that, you need a JDBC driver to access either of them using Java code. A JDBC driver is basically a concrete implementation of the abstract JDBC API. Usually such a JDBC driver is already supplied by the DB vendor in question. Either included in the DB installation, or separately downloadable from their homepage. Just google "[dbvendorname] jdbc driver download" to get a download link/button.

In Eclipse, as being a development tool which already provides the Data Tools Platform (DTP) out of the box, you can manage databases and tables through the Data Source Explorer view (Window > Show View) and/or the Database Developer perspective.

Then you can just create Java classes which makes use of JDBC to execute the SQL. To learn about JDBC, consult Sun's JDBC tutorial. To go a step further, learn the DAO pattern how to abstract the one and other nicely away in a data access layer. Note: you still have to learn SQL separately. It is not part of JDBC, the JDBC is rougly said just a Java tool which enables executing the SQL language using Java code.

Once done that, you can just instantiate such a DAO class in a servlet class and use it during preprocessing and/or postprocessing the HTTP request. You normally don't want to do that in a JSP file. If you for example want to load a list from the database for display in a table, use servlet's doGet() method in combination with RequestDispatcher#forward() which forwards the request to a JSP for display. If you for example want to store a newly registered user in the database, use a <form method="post"> and the servlet's doPost() method for this.

That said, you normally don't want to create/alter tables using Java/JDBC. You normally do that only once during application setup/installation and then use it forever. Use JDBC to select/insert/update the data in existing tables.

BalusC
JavaDB Apache Derby can also be used as a standalone network database, just like to the other more well known ones you mentioned. Like you said it can also be embedded.
Stanley kelly
+3  A: 

If you decide to use Apache Derby as your database, a version of it called JavaDB comes with jee6 + Glassfish bundle, you can read this tutorial about how to connect to it and create tables from within Eclipse. You need to download the Apache Derby plugin.

This has nothing to do with Glassfish, however the example shows you how to connect to it from a normal Java desktop application using JDBC.

Stanley kelly
+3  A: 

This Tip Of The Day seems to have the info you are looking for....

vkraemer