こんにちは、Java Struts の初心者です。単純な登録フォームを作成していますが、フォームを送信した後、null がデータベース テーブルに挿入されます。フォーム クラス名を RegistrationForm.java として作成しました。
public class RegistrationAction extends Action {
private Connection con = null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
RegistrationForm objReg = new RegistrationForm();
String userName = (String) objReg.getTxtusername();
String userPass = (String) objReg.getTxtpassword();
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///test", "root", "");
if (!con.isClosed()) {
PreparedStatement ps = (PreparedStatement) con.prepareStatement("INSERT >INTO tblUserdetails(fldUsername,fldPassword) VALUES (?,?) ");
ps.setString(1, userName);
ps.setString(2, userPass);
ps.execute();
} else {
errors.add("SQL", new ActionMessage("error.SQLConnectivity"));
}
return mapping.findForward(SUCCESS);
}
}
package com.vaannila.form;
import org.apache.struts.action.ActionForm;
public class RegistrationForm extends ActionForm {
private String txtusername;
private String txtpassword;
private String txtrepassword;
private String txtemail;
public String getTxtusername() {
return txtusername;
}
public void setTxtusername(String txtusername) {
this.txtusername = txtusername;
}
public String getTxtpassword() {
return txtpassword;
}
public void setTxtpassword(String txtpassword) {
this.txtpassword = txtpassword;
}
}