Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write sophisticated code?

 
Haritha Gorijavolu
Greenhorn
Posts: 21
Debian Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone. There is this code I have written and it's very long(3 pages). It works fine, I have not detected any bugs. But the problem is I feel there is a way to write more sophisticated code. I am posting the code and anyone who has some free time to contribute, please examine the code and tell me where I can improve.
Thanks verymuch for your time. It will be a great help to me.

Background: This code is to be embedded in a JSP.
The code operates on 2 tables in the database. bookcopies and waitinglist.
The code is a module for online library management system(my semester project). We have book reservation facility in our system and the reservations are stored in waitinglist table. We have a bookcopies table which has the attributes: bookid,copynumber,borrower,duedate. I case the copy is not presently borrowed, the borrower and duedate will be null. Waitinglist has attributes like bookid,userid(user who reserved the book),listposition,activepassive,daycount.
activepassive is 1 when the waiter is active. activepassive is 0 when the waiter is passive.

If some thing is not understandable in my code, please ask and I shall reply back.
Here is the code




Thanks again for your valuble suggestions.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please examine the code and tell me where I can improve

Remove scriptlets from your JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No code in a JSP can be "sophisticated" -- it is no longer acceptable, indeed it is irresponsible, to put code in a JSP at this point. The first step in sophisticating your code is to refactor the code out of the JSP and into a Java class or classes.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some thoughts on refactoring your code,

- Remove scriptlets from JSP, there should be no java code in JSP, use JSP absolutely for presentation purpose. it just displays data provided by controller
- Move data access code to data access objects - DAO
- Move business logic related code to services
- No data access/business logic code in your servlets/controller
- You controller (or servlet if you are not using any framework) uses services and DAOs and provided data to view (JSP) for display
- Have your domain model and entities instead of writing database table oriented code.



 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
Stefan Evans
Bartender
Posts: 1691
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll echo the other comments about scriptlet code.
Java code belongs in a java class - and this is 99% java code.

Feedback on the code itself (ignoring the fact that it is in a JSP)
- This code is potentially open to SQL Injection attack. Instead of building up SQL using string concatenation, you should use a Prepared Statement.

eg


- Instead of opening a database connection directly in your code, you should use a JNDI Datasource to get a connection from a connection pool

- I would not recommend using the JDBC-ODBC bridge driver. Use a proper database (eg Oracle, MSSql, MySQL) with a Type 4 JDBC connector.

- This code appears to always execute every single query in line. I would suggest breaking this up into methods, one action per method. You have already broken them up by comments, so extracting them into methods should be trivial.

- Often you are performing 2-3 seperate queries on seperate tables, where a better approach would be to use a single query and join the tables in the query.

For instance:
SELECT DISTINCT BOOKID FROM USER.WAITINGLIST
SELECT TITLE,EDITION,AVAILABLECOPIES FROM USER.BOOKS WHERE BOOKID=

Could be written as one query:


- You don't seem to be releasing your database connection, though you seem pretty good about closing resultset/statement. My recommendation would be that you always release these resources in a finally block after a try/catch, so that they do get closed in the event of an exception.


 
Haritha Gorijavolu
Greenhorn
Posts: 21
Debian Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all for those useful responses. And, any more suggestions are welcome. Coderanch rocks!!!

 
Haritha Gorijavolu
Greenhorn
Posts: 21
Debian Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move business logic related code to services


So, I have to use EJB as services?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not at all.
 
Haritha Gorijavolu
Greenhorn
Posts: 21
Debian Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use "web services" as services?Can any one suggest some books that would help me build web services through J2EE? I am a beginner.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic