-1

I have created a JSP page to register new users.In the registration form, there are 5 fields where the users enter their details.

After pressing the submit button they will be taken to another page where they have to fill another form which includes some other details of them. The data, the user enters in the first form is successfully inserted into the first table but, when they send the data in the second form it gives an error saying

"java.sql.SQLException: Field 'artist_id' doesn't have a default value".

Here the atrist_id is the primary key of the first table and it is the foreign key of the second table. Following is the signup and artist_info.

structures of the first table

structures of the second table

try {

    String address_1 = request.getParameter("address_1");
    String address_2 = request.getParameter("address_2");
    String city = request.getParameter("city");
    String postal_code = request.getParameter("postal_code");
    String phone_number = request.getParameter("phone_number");

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/painter?autoReconnect=true&useSSL=false", "root", "");
    Statement st = con.createStatement();

    //st.executeUpdate("insert into signup(uid,fname,lname,email,password,country)values('"+firstName+"','"+lastName+"','"+email+"','"+password+"','"+country+"')");
    st.executeUpdate("insert into artist_info(address_1,address_2,city,postal_code,phone_number)"
        + "values('" + address_1 + "','" + address_2 + "','" + city + "','" + postal_code + "','" + phone_number + "')");

How should I change this code to get the value of artist_id into the foreign key(artist_id) of the second table.Thank you.

  • Both images are the same. I'm guessing though that artist_id is in the signup table? Normally for doing an insert in two tables you first have to commit one, then get it's id, then commit the new one. Either that or you make the foreign key non-null so you can after all it's commited set it to the right value. – brightpants Jan 22 '20 at 06:45
  • i have put the correct image .please check again :) – Jeewan Thimira Wijewardhana Jan 22 '20 at 07:30

1 Answers1

0

I'm guessing it's due to the fact that if you set a field in a table to primary key, it can't be nulled. It seems you don't have a sequence generator, so you have to set the artist_id inside artist_info before trying to commit it.

So, the solutions here are:

  1. Get a sequence generator in your db for this field
  2. Don't forget setting the artist_id inside artist_info.
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
brightpants
  • 415
  • 1
  • 4
  • 28