3
\$\begingroup\$

I will be creating a web service as part of a group project for university, which will be developed using Java within the Eclipse IDE.

Having no prior experience using web services, I managed to find some tutorials that were useful, but struggled to find enough database code examples which suited my needs (though, perhaps I was looking in the wrong places).

My team member has failed to produce even the slightest example of a web service client, so I have taken it upon myself to develop both the web service and client.

I would like to know whether or not my web service example method is suitable for client-side implementation: i.e - Do I have to change the method in order for the client to execute the method or is it acceptable as is?

(I have been creating basic web services using the axis2 framework and running them on the Apache Tomcat server. However, I am open to any suggestions or recommendations in regards to alternative servers and frameworks.)

/**
* Adds a new product to the database
* 
* @param name -- name of the game product (e.g - call of duty, grand theft auto)
* @param genre -- genre of the game product (e.g - action, adventure)
* @param platform -- platform of the game product (e.g - playstation 4, xbox one)
* @param developer -- developer of the game product (e.g - sledgehammer games, rockstar north)
* @param publisher -- publisher of the game product (e.g - activision, rockstar games)
* @param ageRating -- age rating of the game product (based on the PEGI rating system)
* @param price -- price of the game product in pounds (£)
* @param quantity -- quantity of the game products 
* @param description -- brief description of the game product 
*/
public void addProduct(String name, String genre, String platform, String developer, String publisher,
int ageRating, double price, int quantity, String description)
{
try
{
// 1. Connects to the database
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Products", "root", "root");

// 2. Executes SQL query
String SqlString = "INSERT INTO Games(Game_Name, Genre, Platform, Developer, Publisher, Age_Rating, Price, Quantity, Description) VALUES(?,?,?,?,?,?,?,?,?)";

PreparedStatement prepStatement = connection.prepareStatement(SqlString);
prepStatement.setString(1, name);
prepStatement.setString(2, genre);
prepStatement.setString(3, platform);
prepStatement.setString(4, developer);
prepStatement.setString(5, publisher);
prepStatement.setInt(6, ageRating);
prepStatement.setDouble(7, price);
prepStatement.setInt(8, quantity);
prepStatement.setString(9, description);    

// 3. Updates the database using the SQL query
prepStatement.executeUpdate();

// 4. Closes database connection
connection.close();
}
catch(Exception e)
{
System.out.println("Problems were encountered when trying to insert new records to the database. (Ref. to SQL statements)");
}
}
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Looked briefly into your program, here are few points:

I would like to know whether or not my web service example method is suitable for client-side implementation

Web Client implementation doesn't need the actual implementation, if the service is hit able, it's client can surely be written.

addProduct(String name, String genre, String platform, String developer, String publisher,
int ageRating, double price, int quantity, String description)

Why not create a Product bean and pass it instead of so many params?

(I have been creating basic web services using the axis2 framework and running them on the Apache Tomcat server. However, I am open to any suggestions or recommendations in regards to alternative servers and frameworks.)

This seems fine but from the learning perspective, give Jersey a try.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your feedback. I will admit, the reason that I have used so many parameters is because I have no experience creating JavaBean classes, or using getters and setters effectively. However, I will take the Product bean suggestion and optimise my code accordingly. Thanks again. (Edit: I would vote-up the answer, but I do not have enough reputation, sorry) \$\endgroup\$ Commented Nov 20, 2014 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.