This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes the developer made JDBC calls in every bean.what to change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "the developer made JDBC calls in every bean.what to change" Watch "the developer made JDBC calls in every bean.what to change" New topic
Author

the developer made JDBC calls in every bean.what to change

prasanna pati
Ranch Hand

Joined: Jan 29, 2005
Posts: 46
Basically I am a tester for J2ee application ( struts ). I have got a developed project in which the developer made Jdbc calls in his/her every beans validate method . He made atleast 30 beans and almost every bean is similar somewhat similar to this : ( see below )

these are all he/she made in ActionForms . So what should I suggest him/her ?

now see the actionForms >>> ( I am pasting only 1 example )




package beanpack;
import javax.servlet.http.HttpServlet;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;

import javax.servlet.http.HttpServlet;

public class Loginbean extends HttpServlet
{

private String name, pass;

public Loginbean() {
name = null;
pass = null;
}

public Loginbean(String nm, String pwd) {
this.name = nm;
this.pass = pwd;

}

public boolean validation() {


try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc:MyDSN");
Statement st = con.createStatement();
String stp = new String("select * from kenji where username='"
+ getName() + "' and password='" + getPass() + "'");
ResultSet rest = st.executeQuery(stp);

System.out.println("... " + stp);
if (rest.next())
return true;

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return false;

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPass() {
return pass;
}

public void setPass(String pass) {
this.pass = pass;
}

}


-------------------------------------------------------------

firstly he made everything hardcoded , next no-internal-comments .tell some other stuffs and what should I suggest him?
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Considering that it's not just the form beans, which you have to review, I'm sure reading up on one of the "Struts Best Practices" results will give you a lot of points.

Also, making JDBC calls from the form bean is a total NO-NO. It doesn't make sense to adopt a framework and then go about violating one of its fundamental requirements (that of separation of layers).

[ March 18, 2008: Message edited by: Anirvan Majumdar ]
[ March 18, 2008: Message edited by: Anirvan Majumdar ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd also suggest that you tell the developer to read up on the Data Access Object and Transfer Object patterns. These patterns are very useful in separating the layers in a database application.
[ March 18, 2008: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I would have to know how this class fit into the big picture to comment too much. What you are showing is not an "ActionForm". I do not understand what it is. It extends HttpServlet but it sure does not look like a servlet to me.

- Brent
 
Don't get me started about those stupid light bulbs.
 
subject: the developer made JDBC calls in every bean.what to change
 
Similar Threads
I am getting clone() has protected access in java.lang.Object
Struts Problem with DAO..
Null populated in parent entity
K&B Chapter 1 Question 5
Simple Login faces