The series so far has been as follows:
- Using Java web services to connect J2ME app to the internet: Part 1
- J2ME and Web Services:Part 2 – Configuring Apache Tomcat
- Creating a simple J2ME app: Part 3 of Java web services
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.
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 here to create the database.
- 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.