This Application illustrates how to create database connectivity with MySQL in visual web jsf application using java persistence api.
In this application, we are going to create a database connection with mysql step-by-step. For your help we provide you all screen shots one by one. Follow all the instructions and screen shots, finally you will find the table data on your browser.
1. Creating database table
Right click on Database and create New Connection...
Click OK. You will find new connection visible on the left side in Databases. Expand and right click on Table and click on Create Table.
It will ask you to enter user name and password to connect to the database.
Click OK. Now put table name, column name, data type, size etc. to create table in Create Table window. Click Add column to add new column.
If you want to populate the data in the table, right click on Table and select Execute Command. A command window will open on the right side. Write the query to insert the data in the table as shown below. Execute select query to see the data in the table.
2. Creating Project
Click on File > New Project. Select Java in the Categories section and Java Application in the Projects section as shown below in the screen shot.
Enter your project name, select Create Main Class and click Finish.
Click on File > New Project. Select Web in the Categories section and Web Application in the Projects section as shown below in the screen shot.
Enter the project name and click Next.
Select the server from the list and click Next.
Select Visual Web JavaServer Faces and click Finish.
3. Linking The Project Together
Select Your Java Application then .jar file appear in Project JAR Files Section then click Add Project JAR Files.
The Selected JAR file shown in the Compile-time Libraries section as below screen shot and then click OK.
4. Binding To The DataBase Table
2. The New Entity Classes from Database Tables window will displays. If the
Database Connection field is blank, then select the test database
from the pull-down list. The Available Tables column displays all the tables in the test database, including the users table. Select the users
table and click Add to move users to the Selected Tables column. Click Next after moving the users table.
3. The Entity Classes dialog displays. The IDE displays
the database table name users, and suggests a class name of Users.
(Double click this class name to change it.) click Finish.
5. Creating a Persistence Unit
1. In the Create Persistence Unit window, set the
Persistence Unit Name to VisualModelAppPU. Leave the other fields with the
values then Click Create
Then click Finish in the Entity Classes screen to complete the operation and create the VisualModelAppPU persistence unit.
2. To verify that the persistence unit, whether it is
created correctly or not, expand the VisualModelApp Source Packages >
META-INF and double click on persistance.xml.
3. Click the XML tab to see the complete XML coding. The
properties in the file should correctly reflect the database name, its url,
and password, along with the package and class name (visualmodelapp.Users). The transaction type is RESOURCE_LOCAL
and the provider is oracle.toplink.essentials.PersistenceProvider.
6. Creating The Entity Controller Class
Expand the VisualModelApp > Source Packages > visualmodelapp. Right click the visualmodelapp node and select New > Java Class. In the New Java Class dialog, set the class name to UserController (leave the location as Source Packages and the package name as visualmodelapp). Note that there are already two classes in the package: Main.java and Users.java. Click Finish.
You should see the skeleton source code for this new class in the Edit window. Add the following code to the class:
Source Code of UserController.java
Use the Fix Imports function to import the required classes and packages. (Right click in the source editor window and select Fix Imports from the pop-up menu.) OR import these three package in your UserController.java class:
7. Binding The Entity Bean To The Table Component
1. Creating a property in the session bean
usersto SessionBean1. Type in the following line of code:
private Users users;
usersproperty. Right click the line of code you typed in and select the action Insert Code. Choose Getter and Setter from the popup menu. Then, select
You can Copy and Paste This Code in the SeessionBean1.java Class
8. Binding The Property To The Table Component
users(from SessionBean1) as the binding array. (Note that if you do not see the
usersproperty displayed in the drop down list, right click in the Design window and click the Refresh option in the context menu. Or simply click the Refresh button in the toolbar. If you still do not see the users property displayed in the drop down list, then close and reopen the VisualWebApp project.) Click OK.
The Table Data Display on the Browser as Below:
You can add more functionality in your table by making changes in table layout available in design window of Page1.jsp right click on table.
Posted on: June 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles