1

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....

0

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

5 Comments

i tried this and i got an exception like :java.lang.RuntimeException: java.sql.SQLException: Column count doesn't match value count at row 1
hey its working but i ve two ppl registered for the post and am clicking accept for one user but both the users are getting added to the login table i want only that person to be added... i think i ve to fetch the id when i click accept can u guide me on that??
'insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo,UserName) select *, 'undefined' from register' I tried this
great. Now, when this problem is solved, you probably need to post another question, and fully desribe it, because right now, I'm not following you.
hey and one more thing even yo alter worked... I think for my project alter is more suitable at this stage so thanks a ton..... and ya surely ll post a new question..... thank u
3

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.

Comments

-1

Run the following sql query, in sql command prompt and check whether it's working or not

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.