简体   繁体   中英

java.lang.RuntimeException: java.sql.SQLException: Field 'UserName' doesn't have a default value

I am developing a web app.... in which there will be a list of users requesting registration ...Wen ever we click on accept all the details must be moved from register table to login table...

This is my servlet:

ClientApproveService approve=new ClientApproveService();
approve.clientApprove();

this is my service:

public class ClientApproveService {

    public void clientApprove() {
        ClientApproveDAO cad=new ClientApproveDAO();
        DataSource dataSource=new DataSource();
        cad.setDataSource(dataSource);
        cad.insertClient();

    }

}

And this is my DAO:

public class ClientApproveDAO {

        private DataSource dataSource;
        public void setDataSource(DataSource dataSource) {
       this.dataSource = dataSource;
    }
    Connection conn=null;
    PreparedStatement statement=null;
    ResultSet rs=null;


    public void insertClient() {    
        try{
            conn=dataSource.createConnection();
            PreparedStatement ps=conn.prepareStatement("insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select * from register ");
            ps.executeUpdate();
        }
        catch (SQLException e) {
            throw new RuntimeException(e);

    } finally {
            if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
    }


    }

}

This is my register table :

create table register(
id int(100) not null AUTO_INCREMENT primary key,
FirstName varchar(300) default null,
LastName varchar(300) default null,
Gender varchar(200) default null,
Category varchar(200) default null,
DateOfBirth varchar(200) default null,
Age int(3)default null,
Address varchar(1000) default null,
Country varchar(500) default null,
State varchar (500) default null,
city varchar(500)default null,
PinCode int(10)default null,
EmailId varchar(500)default null,
ContactNo varchar(20) default null,
MobileNo varchar(20) default null
);

And this is my login table:

create table login(
UserName varchar(100)not null,
PassWord varchar(100)not null,
id int(100) not null AUTO_INCREMENT primary key,
FirstName varchar(300) default null,
LastName varchar(300) default null,
Gender varchar(200) default null,
Category varchar(200) default null,
DateOfBirth varchar(200) default null,
Age int(3)default null,
Address varchar(1000) default null,
Country varchar(500) default null,
State varchar (500) default null,
city varchar(500)default null,
PinCode int(10)default null,
EmailId varchar(500)default null,
ContactNo varchar(20) default null,
MobileNo varchar(20) default null
)

i have to insert all data from register table to login table but i ve nt still auto generated username and password fields in login table... But i should be able to generate the rest ryt??? But when i run this it throws an exception

java.lang.RuntimeException: java.sql.SQLException: Field 'UserName' doesn't have a default value

Please someone help me fix this... Thanks in advance....

You are not giving UserName filed a vaule, and it does not have default. Try this:

insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo,UserName) 
select *, 'undefined' from register

Or, alternatively, you can ALTER the table, so userName field would have the default value:

ALTER TABLE login MODIFY userName varchar(100) NULL DEFAULT NULL;

You need to provide a username value since the column definition is "NOT NULL". You may first query the register table, generate a username, and then insert the new record to the login table.

在sql命令提示符下运行以下sql查询,并检查其是否正常运行

  insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select * from register 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM