Monday, April 18, 2011

MVC, duet: Ubuntu, MySQL, Java, Netbeans, Tomcat, ZK, iReport (Part 2)

Making a connection to the database:1. Open the Netbeans2. In the "Services" window, right click on "Databases", select "New Connection",  then pop up, "New Database Connection" window.
3.
Fill existing items as you wish, for example: host = localhost, port = 3306, database = PFM, username = root, password = root. Check "Remember password" option.
4.
Press the "OK".

Creating a model for the database (results: dao-persistence-pfm.jar):

1.
Open the Netbeans
2. Create a new project by opening the menu "File"> "New Project ..." ("Ctrl + shift + n"), it will display the "New Project".
3.
In the "Categories", select "Java". In the "Projects", select "Java Class Library". Click the "Next>".
4. Name the project "PFM" and specify the storage location. Click the "Finish".
5.
In the window "Project" appears a new project with the name of the PFM. Right-click and select "Properties", then it will appear the window "Project Properties - dao-PFM-persistence."6. In the category "Sources", change "Sources / Binary Format" into the JDK version that the highest (this time JDK6).
7.
In the category "Libraries", add the following libraries by pressing the button "Add ...". Library Select "EclipseLink", "Persistence" and "MySQL JDBC Driver." Press the "OK".8. Right click on the "Sources Packages", select "New"> "Java Packages ...". Appear the window "New Java Packages", name with "dao", then click the "Finish".
9.
Under the "Source Packages" appears a new package "dao" which is grayed out, indicating that there is no content in the package. Right click on the package "dao", select:
a.
"New"> "Entity Classes from Database ...",
b.
(Alternatives) "New"> "Other ...", came the" New File ". In categories, select the "Persistence", then select "Entity Classes from Database", click the "Next>".10. Appear the window "New Entity Classes from Database". In the "Database Connection" select the connection that has been made, for example: "jdbc://localhost:3306/pfm". Click any item in the "Available Tables", thena. Click the "Add" for items that are selected ("ctrl + a" to select all)b. Click the "Add All" for all itemsin this case, press "Add All", then click the "Next>", then click the "Next>". On the "Collection Type" select "java.util.List". Finish by pressing the "Finish".
11.
In "Sources Packages" appears packages "META-INF" automatically, in which there is persistence configuration file for named "persistence.xml". In the package "dao" appear 3 new files result from the table, namely "Item.java", "Pengguna.java" and "Transaksi.java". In the package "dao" appears a new file the results of view, ie "VRekap.java".
12.
Try to open "Item.java":
a.
In that file there @ Id which must exist to indicate that it is a sign that under the primary key, in this case is the @ Column (name = "iditem"). That is the reason why on every table must have primary key and on any view be prepared a column for primary key. @GeneratedValue (strategy = GenerationType.IDENTITY) to create value "auto increment".
b.
In that file there are 4 named query, 3 query based on one column and 1 query show all. Request this name can be changed in accordance with the wishes, it can even add specific query as you wish. Better to add the query rather than change or remove it, who knows in the future is needed. For example, want all the displayed sort by namaitem, then the previous query "SELECT i FROM Item i" changed to "SELECT i FROM Item i ORDER BY i.namaitem", as well as other requests.c. On query there is a statement called "FROM Item i WHERE i.alurkas =: alurkas". "i" in "i.alurkas" derived from "item i", so if the "item a", then "a.alurkas", whatever we want to use the "i" or "a" or whatever, the important thing is consistent. ":alurkas" closely related "short private alurkas" and the column data type "alurkas" on the table "items" in the data base which incidentally has a data type tinyint (4), so that later the parameter "alurkas" must have a data type short. Save the file.
13.
Open "VRekap.java" contained an error. The mistake is "There is no ID defined for this entity hierarchy", the intention is not yet determined its ID. Based on the design table, which is used as ID to view "vrekap" is "idtransaksi", then place the cursor on the left of @Column(name="idtransaksi"), type the @Id and then press enter. Press "ctrl + shift + i" or right click and select "Fix Imports". Save the file.

notes:Limit, like, in and between discussed in the next section using the method of learning by doing.

No comments:

Post a Comment