You are on page 1of 2

Creating the Login: Java web services part 4

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;

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;

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 to create the database.

You might also like