943,632 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 875
  • Java RSS
Jan 12th, 2009
0

Java and Web Services

Expand Post »
I know I should probably post this in Web Services of Web Development, but this may be beneficial to others that are trying to lean something about it too.

I'm getting lot of strange behaviour from this assignment that at this point I'm willing to throw it away and never work on it.
I have class that run various queries with database. With use of Axis2 plug-in I was able to generate WSDL (This has some red sections which I'm not able to work out). Here is small section of two methods out of it to demonstrate some errors I'm getting
Java Syntax (Toggle Plain Text)
  1. public Employee logEmployee(String usr, String pass) {
  2. System.out.println("Username = "+ usr+" Password = "+pass);
  3. Employee e = new Employee();
  4. Connection conn = cm.getConnection();
  5. if (conn != null) {
  6. ResultSet rs = null;
  7. PreparedStatement preparedStatement = null;
  8. try {
  9. String strQuery =
  10. "SELECT employee_id, branch_id, password, firstName, lastName "
  11. + "FROM employee WHERE employee_id=? AND password=?";
  12.  
  13. preparedStatement = conn.prepareStatement(strQuery);
  14. preparedStatement.setString(1, usr);
  15. preparedStatement.setString(2, pass);
  16. rs = preparedStatement.executeQuery();
  17.  
  18. while (rs.next()) {
  19. e.setEmployeeID(rs.getString("employee_id"));
  20. e.setBranchID(rs.getString("branch_id"));
  21. e.setPassword(rs.getString("password"));
  22. e.setFirstName(rs.getString("firstName"));
  23. e.setLastName(rs.getString("lastName"));
  24. }
  25. }
  26. catch (SQLException ex) {
  27. ex.printStackTrace();
  28. }
  29. finally {
  30. try {
  31. rs.close();
  32. preparedStatement.close();
  33. }
  34. catch (SQLException sqle) {
  35. sqle.printStackTrace();
  36. }
  37. catch (NullPointerException npe) {
  38. npe.printStackTrace();
  39. }
  40. cm.putConnection(conn);
  41. }
  42. }
  43. return e;
  44. }
  45.  
  46. public ArrayList<Employee> viewEmployees() {
  47. ArrayList<Employee> employee = new ArrayList<Employee>();
  48. Connection conn = cm.getConnection();
  49. if (conn != null) {
  50. ResultSet rs = null;
  51. PreparedStatement preparedStatement = null;
  52. Employee emp;
  53. try {
  54. String strQuery =
  55. "SELECT employee_id, branch_id, firstName, lastName "
  56. + "FROM employee ORDER BY lastName,firstName";
  57.  
  58. preparedStatement = conn.prepareStatement(strQuery);
  59. rs = preparedStatement.executeQuery();
  60.  
  61. while (rs.next()) {
  62. emp = new Employee();
  63. emp.setEmployeeID(rs.getString("employee_id"));
  64. emp.setBranchID(rs.getString("branch_id"));
  65. emp.setFirstName(rs.getString("firstName"));
  66. emp.setLastName(rs.getString("lastName"));
  67. employee.add(emp);
  68. }
  69. }
  70. catch (SQLException ex) {
  71. ex.printStackTrace();
  72. }
  73. finally {
  74. try {
  75. rs.close();
  76. preparedStatement.close();
  77. }
  78. catch (SQLException sqle) {
  79. sqle.printStackTrace();
  80. }
  81. catch (NullPointerException npe) {
  82. npe.printStackTrace();
  83. }
  84. cm.putConnection(conn);
  85. }
  86. }
  87. return employee;
  88. }

I was able to use this WSDL to create client side and get automated files Service, ServiceLocator, _PortType, SoapBindingStub.
To communicate with service I use following code
Java Syntax (Toggle Plain Text)
  1. try{
  2. EmployeeQueriesServiceLocator locator = new EmployeeQueriesServiceLocator();
  3. EmployeeQueries_PortType service = locator.getEmployeeQueries();
  4.  
  5. //execution of any service method here
  6. }
  7. catch(ServiceException ex){
  8. ex.printStackTrace();
  9. }
  10. catch(RemoteException re){
  11. re.printStackTrace();
  12. }
However for example when I'm trying to log in through logEmployee(String usr, String pass) method server side receive only user name string and password is lost. (I found solution to this in sending these strings in array)
Secondly when I tried to retrieve list of all employees through viewEmployees() method that is supposed to return ArrayList<Employee> I'm getting warning or error that found return is actually Object[].
Can somebody shed some light into this darkness?
Similar Threads
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Jan 12th, 2009
0

Re: Java and Web Services

first issue sounds really strange, the system out doesn't print pass?
with the second, are you importing Employee?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jan 13th, 2009
0

Re: Java and Web Services

Yes of course I do import Employee
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Jan 13th, 2009
0

Re: Java and Web Services

A piece of advice: use a debugger, given that we have no way of reproducing the scenario and the code posted so far looks OK.

BTW, why are you catching a NullPointerException ? Catching unchecked exceptions without any kind of processing is *always* a bad practice and screams of a pre-condition check. Instead put null checks before closing the database resources [like result sets and statements].
Java Syntax (Toggle Plain Text)
  1. Connection conn = null;
  2. Statement stmt = null;
  3. try {
  4. try {
  5. // do something
  6. } finally {
  7. if(stmt != null) stmt.close();
  8. if(conn != null) conn.close();
  9. }
  10. } catch(Exception e) {
  11. e.printStackTrace();
  12. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 13th, 2009
0

Re: Java and Web Services

Yeah NullPointerException is one of many small things I need to battle through as many of the things I pick up either from IDE suggestions (which is not always best to do) or following other people code and often miss the reason of the use. Hopefully with all this technical reading I'm doing I will loose some bad habits

Right now I'm "cracking" through Service-Oriented Architecture with Java Web Services by Mark Hansen and found nice quote
Quote ...
Unfortunately, if you are like me, you may have found the Java Web Services learning curve a little steep. It seems that lots of powerful and complex machinery is required just to deploy a Java class as a Web service or create a simple client to consume such services. Sure, you can get the simple "Hello World" application from the Java EE 5 tutorial to work. However, when you need to deploy your purchase ordering system, things suddenly seem to get much more complicated. Either the WSDL you start with gets compiled into myriad bizarre classes that have to be manually wrapped and mapped into your real purchasing system, or, if you start from your Java classes, the WSDL that gets produced doesn't turn out the way you need it to. If you have been frustrated by problems like these, I sympathize with you.
This assignment is already submitted, it is only my stubbornness that push now to find out why it did not worked in first place and in doing so learn something new and maybe help somebody in the future with insufficient resources and support from tutor/teacher
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help!
Next Thread in Java Forum Timeline: Quick Search(Finding Duplicate Records)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC