Creating the Login: Java web services part 4


The series so far has been as follows:

In the previous tutorial we created a simple J2ME application that allows a user to login into a system. Today we are going to do the actual logic that enables a person to login and so we will not be touching on any J2ME code but rather the Java we all know.

The assumption in this tutorial is that you know how to create a Java application that connects to the database. Before we continue make sure you have the following;

  • Installed MySQL database on your machine
  • Obtained a copy of the MySQL connector (mysql-connector-java-3.0.11-stable-bin or any other version
  • Created a database in MySQL with a table which will hold your user details. I will provide the SQL necessary to create the database and the table.

Steps

1.       Fire up Eclipse and then create a new project. We are going to create a “Dynamic Web Project”. Make sure the “Target Runtime” points to the version of Tomcat that you installed on your system. A screenshot of mine is shown below;

Dynamic web project configuration on Eclipse

Dynamic web project configuration on Eclipse

2.       Next create a new package that will have all our code in it. This practice is encouraged as it reduces the chances of conflicts in naming that might be encountered when the default package is used.

3.       Now create a Java class and give it a name of your choice. This class should reside in the package you created in the above step. I named mine Login.java and the package name is called webservices.

4.       Before we actually do the code to login let us first create the database part. Create another package and in it create another Java class. I have named my class as DbManager.java and the package is called database.

5.       Next we need to add the MySQL connector jar to our project library. First copy it to the root of your project folder. In Eclipse you then right click on your project and choose “Properties” and then choose “Java Build Path” and then select the “Libraries” tab. If you followed the above steps correctly you should have something similar to the screenshot below;

Adding MySQL connector to your project library

Adding MySQL connector to your project library

6.       Next click on the “Add External JARs” tab and browse to the location of MySQL connector jar file and add it to your project. It should now be shown under “Libraries” tab on your Project Explorer.

7.       Since this is not a tutorial on how to connect to the database I am not going to go through the code line by line but rather just provide the code and highlight the important parts. In the code provided please note the following;

  • The code for login and database connection has been separated. This a good practice.
  • To access the methods found in DbManager.java we have imported the class using this line: “import database.DbManager.java;” and then created an instance of that class using this line “DbManager manager

8.       All you need to do now is go to phpMyAdmin and click on the SQL tab and paste the SQL code provided here to create the database.

Downloads

  • Login.java – in word format or in pdf
  • DbManager.java – in word format or in pdf
  • The whole tutorial -in word format or in pdf

That will be all for today. In the next tutorial we are going to create and publish the Java web services.

Advertisements

4 thoughts on “Creating the Login: Java web services part 4

  1. Pingback: Tweets that mention Creating the Login: Java web services part 4 « KiruinC -- Topsy.com

  2. Hey.. Thankx for the tuts. Could you be having a J2ME app/code thats has a chat conversation in it. Or comments thread in it? It might be of help to me. Kindly let me know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s