Sunday, November 7, 2010

Creating A Database In XAMPP

“This tutorial touches on creating a small database with xampp or lampp. I am using lampp in this case but what ever I do also applies to xampp.

With lampp, I will have to first start the server in linux.So I open the Terminal change access to root by typing the command

-sudo -s -H
after I start lampp with this command
-/opt/lampp/lampp start

For windows users,all you have to do is install xampp as a service.Open the xampp control panel and start apache and mysql.

After doing this.
-Open your browser and type http://localhost in navigation bar and press enter.
You should see and interface like this.



From here go to the tools menu and select phpMyAdmin.

This will take you to the Mysql database management system interface in xampp/lampp which looks like this



Now lets go through these steps.

-- Enter the database name Student_Information and click on create to start creating a database

*A prompt will appear alerting that database creation was successful

--Start creating your table by typing your table name in field,enter 3 in the columns field and click on GO.

--Enter details in various fields as seen in image and click on save


Thus, a name,index_number and course fields all of datatype VARCHAR.If by any means you don't understand what a VARCHAR is refer to the mysql manual.

After creating the table you will get an interface like this:




Great you have been able to create a table but it's left with one important thing thus our table does not have a primary key. From simple database concepts we get to know that a primary key should be unique.So we make the index number field our primary key since a every student has a unique index number.

Okay we do this by clicking the key image on the index number column.

We are now clear on creating our database and table.So our next step is to connect to our database from

netbeans and manipulation using JPA.


Sunday, September 19, 2010

Practical Approach to JPA


Hi guys,

In the next few weeks you will be introduced to Java EE(Java Enterprise Edition) with a simple a example such a student data capturing system .

Before I start I would like to make a point. The point is I am doing this for “My friends”,when I say “My friends” I mean people who want to so many things in software development but do not want to spend hours,days,month reading over 400 pages of a Java EE book or any other book. So if you are my friends this is the right place for you.

Even though this strategy might help a bit but my friends,I recommend you find a book to read after going through this example to get more insight on the Java EE platform because there is a lot to learn like my favourite building web applications using the Java Server Faces, Richfaces etc.

I recommend this cool book I 've been reading for some time now thus “Beginning Java EE 6 platform with glassfish from novice to professional”.Read it and you will never be the same again.

One Last point,this example is a Java EE application alright but it is done in the

Application-Managed Environment meaning we going to use a Java SE application with JPA thus the Java Persistence API in our example.

Looking at it from a technical angle the term “application-managed” means an application is responsible for explicitly obtaining an instance of EntityManager and managing its life cycle

.

Creating an application-managed entity manager is simple enough using a factory, but what differentiates application-managed from container-managed is how the factory is acquired.

A container-managed environment is when the application evolves in a servlet or an EJB container.

In a container-managed environment the concept of Application Servers like Glassfish,JBoss,Oracle As etc come into play but we are not going to delve much into this environment. I leave that for you to find out. My cool book can help you.

Okay I know “my friends” don't like a lot of literature so let's roll.

TOOLS

These are the tools we are going to use:

- Netbeans 6.9 IDE .Using any of the Earlier versions also works fine.

Note:Remember to have the Java Runtime installed.You can download it from www.sun.java.com. Download current Netbeans from www.netbeans.org.

- MySQL Database Management System. I am using Mysql that came with xampp.So it's better you get xampp installed and if you are a linux freak like I am install lampp or xampp for Linux.The same applies to Netbeans.


STARTING WITH NETBEANS

Though enterprise application rely mainly on databases.We will start with creating our application with netbeans before we creat our database.

So this how a Netbeans IDE looks like



Lets create a new project

  • Select File and Choose New Project.
  • Select java application from the list of options.
  • Give your project a name in this I chose StudentRecord.
  • Uncheck Create main class and Set as mainproject check boxes and click on finish


You should see an interface like this one




  • Next Right click on source packages
  • Choose new and select new package.Give your package a name in this case I used com.spomega.studentrecord.forms for my first package.
  • Right click on your package choose new and select Jframe form from the options provided
  • Name your form Mainform.

In this approach I am using the netbeans Drag n Drop feature to make things easier and faster but ideally it is better if you programme your swing in code instead of doing Drag n Drop. You can learn about GUI programming in java by reading JAVA HOW TO PROGRAM SIXTH EDITION chapters 11 and 22 knock your self out


So this is the interface you get after performing the above steps.



  • So..........let's do the drag n drop stuff now
  • Drag a panel from the pallete on to the Mainform.
  • Resize the panel to fit the size of the Jframe.
  • Rename the panel as backgroundPanel.
  • Drag a toolbar to the top of the panel.
  • Drag a toggle button on to the toolbar and Right click the toggle button and edit text to “Register Student”.

Continue adding the toggle button until your interface looks like this:



Drag another panel and place it just below the toolbar.Resize it to fill the remaining space under the toolbar.Rename the panel as contentpanel.

We are done with our Mainform for now.We will move to the content.

-Right click the package,select new and choose Jpanel Form.Name it studentregistrationpanel.

- Drag n drop various components onto panel till it looks like this:



Rename the textfields as jtfName,jtfIndexNumber,jftCourse respectively.Do the same for save and clear buttons in this case rename as btnsave and btnclear.

Now here comes the tricky part:

- Select the MainForm tab in the IDE.

- Right click on the Register Student button.

- Select Events>Action >ActionPerformed.

At this instance the interface switches to the source view:Add the following code


I hope you can see because i can.... Okay to be sure the code for the event handler is
contentpanel.removeAll();
contentpanel.setLayout(new BorderLayout());
contentpanel.add(new studentregisterationpanel());
contentpanel.validate();
contentpanel.repaint();

- After adding this code right click the source view and select Run.When program runs and the Mainform appears click on Register Student button. The studentregisterationpanel appears on the Mainform.Please if it does not appear check and correct errors in your code.

NB:And one thing about errors – don't give up or get frustrated when you get errors.In fact as a software developer it should make you happy because solving errors gives you experience.


- Perform the same steps above for Edit student like creating a new panel and naming it editstudentspanel with the same design as the studentregisterationpanel.After add the code add Run the application make sure when the Edit Student button is clicked the editstudentspanel is pasted on the Mainform.This should be easily done because of the example above.


So how was the small exercise,Guess it was cool or it didn't work?If so then make sure it does go over the example again and try it.

Now,let's move on to create our third and final panel which will show a table of students whose details have been

captured in the database.


· Create a new panel Form and name it viewdetailpanel

· From the pallete drag and drop an table from the Swing Controls Category on to the panel



The view should look like this


· After doing this select the Mainform and create an actionperformed event handler for the view registered students button.

· Add the code to the event just as we did for the two panels

The result of this exercise after running the application,should be a Mainform with navigation buttons on the toolbar .Any of the buttons clicked should show a new panel.


In a nutshell,your event handling code for the three navigation buttons looks like this:




Let me do a small explanation of the code:

-The first line contentpanel.removeAll() removes all the contents on a particular panel in this case the contentpanel.

- Next line sets the layout of the panel using the BorderLayout.For more information on Layout read about Layout Managers on Chapter 22 of Java How To Program Sixth Edition.

- The following line of code pastes or adds a component on to a panel in this case any our panels will do eg.viewdetailpanel

- contentpanel.validate() validates the components on the panel this operation is vital because the contents of the panel has been modified.

- The last line repaints the panel.

Congratulations Friend you have finshed with designing the interface for this application.Our Next stop will be an Introduction to MySQL using xampp or lampp


Followers