Wednesday, August 31, 2011

LookupDispatchAction - jsp

html:submit property="dynBtn"
bean:message bundle="resource" key="btn.form.add"
html:submit

LookupDispatchAction - Application Resource

btn.form.add=Add Button
btn.form.remove=Remove Button

LookupDispatchAction - Struts-Config

parameter="dynBtn"
name="DynamicCallActionForm"
path="/dynamicCall"
scope="session"
type="com.dynamicCall.DynamicCallAction">



LookupDispatchAction - Action Class

protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("btn.form.add", "addMethod");
map.put("btn.form.remove", "removeMethod");
return map;
}

get something

public List getDepartments(){
List list = new ArrayList();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "select dep_id,dep_name from department";

try{
ps = (PreparedStatement) con.prepareStatement(query);
rs = ps.executeQuery();

while(rs.next()){
Department d = new Department();
d.setDepId(rs.getInt(1));
d.setDepName(rs.getString(2));
list.add(d);
}
}catch(Exception e){
e.printStackTrace();
}

return list;
}

input something

public void addStudent(Student s) {
PreparedStatement pst = null;
String query = "insert into student values (?,?,?)";

try{
pst= (PreparedStatement) con.prepareStatement(query);
pst.setInt(1, s.getsId());
pst.setString(2, s.getsName());
pst.setInt(3, s.getDepId());

pst.execute();
}catch(Exception e){
System.out.println(e);
}
}

Tuesday, August 30, 2011

In Persistence

private Connection con;

public StudentPersistence(Connection con) {
this.con = con;
}

public void conClose() {
try {
this.con.close();
} catch (Exception e) {
System.out.println(e);
}
}