Search code examples
javamysqlsqlstruts2mysql-error-1064

I am receiving an error which says column "address" can not be null


I am trying to run a query but whenever I run I get an error saying that "column address cannot be null".

here is my code.

package com.rajesh.action;
import java.sql.*;
public class AccountDao {


public static int  save(Actionraju r){
int status =0;
Connection con=GetCon.getCon();
PreparedStatement ps;
try {
ps = con.prepareStatement("Insert into account (accountno, username, password,   repassword, amount, address, phone) values (?,?,?,?,?,?,?)");

ps.setInt(1,r.getAccountno()); 
ps.setString(2,r.getUsername());
ps.setString(3,r.getPassword());
ps.setString(4,r.getRepassword());
ps.setDouble(5,r.getAmount());
ps.setString(6,r.getAddress1());
ps.setDouble(7,r.getPhone());
status=ps.executeUpdate();
}
catch(Exception e){
e.printStackTrace();}
return status;

}

}

my Actionraju.java looks like this.

package com.rajesh.action;
public class Actionraju  {
private int accountno;

private String username;
private String password;
private String repassword;
private Double amount;
private String address;
private Double phone;



public int getAccountno() {
return accountno;
}


public void setAccountno(int accountno) {
this.accountno = accountno;
}


public Double getAmount() {
return amount;
}


public void setAmount(Double amount) {
this.amount = amount;
}


public Double getPhone() {
return phone;
}


public void setPhone(Double phone) {
this.phone = phone;
}


public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getRepassword() {
return repassword;
}


public void setRepassword(String repassword) {
this.repassword = repassword;
}




public String getAddress() {
return address;
}


public void setAddress(String address) {
this.address = address;
}


public String execute(){

int i = AccountDao.save(this);
if(i>0){
return "success";
}
return "error";

}

}

In my mysql database I have 7 columns, namely "accountno", "username", "password", repassword", "amount", "address", "phone". For all these 7 columns I checked "not null" box and here my "accountno" is the primary key.

please anyone help me out of this. I am a newbie.


Solution

  • We don't have enough information to be sure we're helping you. But here's some things you can do to debug. Do a System.out.println(r.getAddress1()). If it prints null, that's your problem. The way you created your table says address can't be null, but you're passing in a null value in your code. Either make the table less restrictive, or make sure you pass in a value.

    Also, change your insert sql so it says something like this: Insert into account (accountno, username, ...) values(?,?,?,?,?,?,?) That way, you know you're setting the correct column for the address field. Perhaps you think that 6 means the address field, but it's really using 2 for the address field. If you write your sql to include the column names, you can be sure you're doing what you intend to be doing.