javax.servlet.ServletException: General error message from server: "Field
'firstname' doesn't have a default value"
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.sql.SQLException: General error message from server: "Field 'firstname'
doesn't have a default value"
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1371)
org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:168)
com.stocks.user.sql.DBSQL_RegistrationForm.execute(DBSQL_RegistrationForm.java:38)
myproject1.com.RegistrationAction.execute(RegistrationAction.java:38)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
DBSQL_RegistrationForm.java:
package com.stocks.user.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
public class DBSQL_RegistrationForm {
private static final String QUERY1 = "INSERT INTO all_users(UserName, Password)
VALUES(?,?);";
private static final String QUERY2 = "CREATE TABLE ?_user_business(CompanyName
VARCHAR(20), SharesHeld INT, ShareBuyingPrice FLOAT);";
private static final String QUERY3 = "CREATE TABLE ?_user_info(UserName
VARCHAR(20), FName VARCHAR(20), LName VARCHAR(20), Email VARCHAR(35), Address
VARCHAR(30), Phone VARCHAR(11));";
private static final String QUERY4 = "INSERT INTO ?_user_info
VALUES(?,?,?,?,?,?); ";
private static final String QUERY5 = "CREATE TABLE ?_SingleRecord (Balance
FLOAT)";
public boolean execute(String fname, String lname, String email, String
address, String phone, String UserName, String Password, DataSource ds) throws
SQLException {
Connection conn = null;
conn = ds.getConnection();
int AccountNo = 0;
//check whether user already exists or not...
String QUERY = "SELECT UserName FROM all_users WHERE UserName = ? ";
PreparedStatement pstmt = conn.prepareStatement(QUERY);
pstmt.setString(1, UserName);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
return false; //user already exists...
else
{
pstmt = conn.prepareStatement(QUERY1);
pstmt.setString(1,UserName);
pstmt.setString(2,Password);
pstmt.addBatch();
pstmt.execute();
pstmt = conn.prepareStatement("SELECT AccountNo FROM all_users WHERE UserName =
?");
pstmt.setString(1,UserName);
rs = pstmt.executeQuery();
if(rs.next())
AccountNo = rs.getInt(1);
pstmt = conn.prepareStatement(QUERY2);
pstmt.setInt(1,AccountNo);
pstmt.execute();
pstmt = conn.prepareStatement(QUERY3);
pstmt.setInt(1,AccountNo);
pstmt.execute();
pstmt = conn.prepareStatement(QUERY4);
pstmt.setInt(1,AccountNo);
pstmt.setString(2, UserName);
pstmt.setString(3, fname);
pstmt.setString(4, lname);
pstmt.setString(5, email);
pstmt.setString(6, address);
pstmt.setString(7, phone);
pstmt.addBatch();
pstmt.execute();
pstmt = conn.prepareStatement(QUERY5);
pstmt.setInt(1,AccountNo);
pstmt.execute();
pstmt.close();
conn.close();
return true;
}
}
}
RegistrationAction.java :
package myproject1.com;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.stocks.user.sql.DBSQL_RegistrationForm;
public class RegistrationAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
RegistrationForm myform =(RegistrationForm)form;
String password= myform.getPassword();
String confirmpass= myform.getConfirmpass();
if(password.equals(confirmpass))
{ String firstname= myform.getFirstName();
String lastname= myform.getLastName();
String addr= myform.getAddr();
String username= myform.getUsername();
String phone=myform.getPhone();
String email=myform.getEmail();
// int accountno = myform.getAccountno();
DataSource ds = getDataSource(request);
if((new
DBSQL_RegistrationForm()).execute(firstname,lastname,email,addr,phone,username,password,ds))
return (mapping.findForward("success"));
else
return (mapping.findForward("failure"));
}
else
//failure
return (mapping.findForward("failure"));
}
}
RegistrationForm.java
package myproject1.com;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.*;
public final class RegistrationForm extends ActionForm {
private String firstName = null;
private String lastName = null;
private String addr = null;
private String username = null;
private String password = null;
private String confirmpass = null;
private int accountno;
private String phone = null;
private String email = null;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getConfirmpass() {
return confirmpass;
}
public void setConfirmpass(String confirmpass) {
this.confirmpass = confirmpass;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
username=null;
password=null;
confirmpass=null;
firstName = null;
lastName = null;
addr = null;
phone = null;
email = null;
}
public int getAccountno() {
return accountno;
}
public void setAccountno(int accountno) {
this.accountno = accountno;
}
public ActionErrors validate(
ActionMapping mapping, HttpServletRequest request ) {
ActionErrors errors = new ActionErrors();
if( getFirstName() == null || getFirstName().length() < 1 ) {
errors.add("firstName",new ActionMessage("error.firstName.required"));
}
if( getLastName() == null || getLastName().length() < 1 ) {
errors.add("lastName",new ActionMessage("error.lastName.required"));
}
if( getAddr() == null || getAddr().length() < 1 ) {
errors.add("addr",new ActionMessage("error.addr.required"));
}
if( getUsername() == null || getUsername().length() < 1 ) {
errors.add("uername",new ActionMessage("error.username.required"));
}
if( getPassword() == null || getPassword().length() < 1 ) {
errors.add("password",new ActionMessage("error.password.required"));
}
if( getConfirmpass() == null || getConfirmpass().length() < 1 ) {
errors.add("confirmpass",new
ActionMessage("error.confirmpass.required"));
}
if( getPhone() == null || getPhone().length() < 1 ) {
errors.add("phone",new ActionMessage("error.phone.required"));
}
if( getEmail() == null || getEmail().length() < 1 ) {
errors.add("email",new ActionMessage("error.email.required"));
}
return errors;
}
}
---------------------------------
Yahoo! India Answers: Share what you know. Learn something new. Click here
Send instant messages to your online friends - NOW