Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to insert data in foreign key using jsp

how to insert data in foreign key using jsp

Scheduled Pinned Locked Moved Database
javadatabasehtmlmysqlcom
10 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Faisal_Abdullah
    wrote on last edited by
    #1

    <%--
    Document : check
    Created on : Mar 10, 2014, 7:52:19 PM
    Author : Sahara
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Student Information</title>
    </head>
    <body>
    <h3><%
    String nameHeader = request.getParameter("name");
    out.print(nameHeader);
    %></h3>
    <%@page import = "java.sql.*" %>
    <%@page import = "java.io.*" %>
    <%@page import = "com.mysql.jdbc.Driver" %>
    <?xml version="1.0" ?>
    <%
    String name = request.getParameter("name");
    String subject = request.getParameter("subject");
    String roll = request.getParameter("roll");
    // int roll = Integer.parseInt((String)request.getAttribute("roll"));
    String Student = "INSERT INTO student(roll,name) VALUES('"+roll+"','"+name+"')";
    String Subject = "INSERT INTO subject(name) VALUES('"+subject+"')";
    try{
    Connection connection = null;
    Statement statement = null;
    ResultSet result = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/normalization","root","");
    statement = connection.createStatement();
    statement.executeUpdate(Student);
    statement.executeUpdate(Subject);
    }catch(SQLException e){
    out.println("Error: Connection To Database...");
    out.println("Error:"+e);
    }
    %>
    </body>
    </html>

    Error:java.sql.SQLException: Field 'idfk' doesn't have a default value it is the error which i am facing it from last 3 months. I have created the tables fully correct

    Faisal Abdullah

    _ M Richard DeemingR 3 Replies Last reply
    0
    • F Faisal_Abdullah

      <%--
      Document : check
      Created on : Mar 10, 2014, 7:52:19 PM
      Author : Sahara
      --%>

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Student Information</title>
      </head>
      <body>
      <h3><%
      String nameHeader = request.getParameter("name");
      out.print(nameHeader);
      %></h3>
      <%@page import = "java.sql.*" %>
      <%@page import = "java.io.*" %>
      <%@page import = "com.mysql.jdbc.Driver" %>
      <?xml version="1.0" ?>
      <%
      String name = request.getParameter("name");
      String subject = request.getParameter("subject");
      String roll = request.getParameter("roll");
      // int roll = Integer.parseInt((String)request.getAttribute("roll"));
      String Student = "INSERT INTO student(roll,name) VALUES('"+roll+"','"+name+"')";
      String Subject = "INSERT INTO subject(name) VALUES('"+subject+"')";
      try{
      Connection connection = null;
      Statement statement = null;
      ResultSet result = null;
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/normalization","root","");
      statement = connection.createStatement();
      statement.executeUpdate(Student);
      statement.executeUpdate(Subject);
      }catch(SQLException e){
      out.println("Error: Connection To Database...");
      out.println("Error:"+e);
      }
      %>
      </body>
      </html>

      Error:java.sql.SQLException: Field 'idfk' doesn't have a default value it is the error which i am facing it from last 3 months. I have created the tables fully correct

      Faisal Abdullah

      _ Offline
      _ Offline
      _AK_
      wrote on last edited by
      #2

      Faisal_Abdullah wrote:

      Error:java.sql.SQLException: Field 'idfk' doesn't have a default value

      It looks like you have a column which requires data and you are not providing it. Which table has the column "dfk"?

      .AK.

      F 1 Reply Last reply
      0
      • F Faisal_Abdullah

        <%--
        Document : check
        Created on : Mar 10, 2014, 7:52:19 PM
        Author : Sahara
        --%>

        <%@page contentType="text/html" pageEncoding="UTF-8"%>
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Student Information</title>
        </head>
        <body>
        <h3><%
        String nameHeader = request.getParameter("name");
        out.print(nameHeader);
        %></h3>
        <%@page import = "java.sql.*" %>
        <%@page import = "java.io.*" %>
        <%@page import = "com.mysql.jdbc.Driver" %>
        <?xml version="1.0" ?>
        <%
        String name = request.getParameter("name");
        String subject = request.getParameter("subject");
        String roll = request.getParameter("roll");
        // int roll = Integer.parseInt((String)request.getAttribute("roll"));
        String Student = "INSERT INTO student(roll,name) VALUES('"+roll+"','"+name+"')";
        String Subject = "INSERT INTO subject(name) VALUES('"+subject+"')";
        try{
        Connection connection = null;
        Statement statement = null;
        ResultSet result = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/normalization","root","");
        statement = connection.createStatement();
        statement.executeUpdate(Student);
        statement.executeUpdate(Subject);
        }catch(SQLException e){
        out.println("Error: Connection To Database...");
        out.println("Error:"+e);
        }
        %>
        </body>
        </html>

        Error:java.sql.SQLException: Field 'idfk' doesn't have a default value it is the error which i am facing it from last 3 months. I have created the tables fully correct

        Faisal Abdullah

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        By the error I suspect you need to set the ID field in Student to an IDENTITY (assuming sql server). You seem to be inserting a new subject for every student. I suspect your table design is crap and needs to be redone.

        Never underestimate the power of human stupidity RAH

        F 2 Replies Last reply
        0
        • F Faisal_Abdullah

          <%--
          Document : check
          Created on : Mar 10, 2014, 7:52:19 PM
          Author : Sahara
          --%>

          <%@page contentType="text/html" pageEncoding="UTF-8"%>
          <!DOCTYPE html>
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>Student Information</title>
          </head>
          <body>
          <h3><%
          String nameHeader = request.getParameter("name");
          out.print(nameHeader);
          %></h3>
          <%@page import = "java.sql.*" %>
          <%@page import = "java.io.*" %>
          <%@page import = "com.mysql.jdbc.Driver" %>
          <?xml version="1.0" ?>
          <%
          String name = request.getParameter("name");
          String subject = request.getParameter("subject");
          String roll = request.getParameter("roll");
          // int roll = Integer.parseInt((String)request.getAttribute("roll"));
          String Student = "INSERT INTO student(roll,name) VALUES('"+roll+"','"+name+"')";
          String Subject = "INSERT INTO subject(name) VALUES('"+subject+"')";
          try{
          Connection connection = null;
          Statement statement = null;
          ResultSet result = null;
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/normalization","root","");
          statement = connection.createStatement();
          statement.executeUpdate(Student);
          statement.executeUpdate(Subject);
          }catch(SQLException e){
          out.println("Error: Connection To Database...");
          out.println("Error:"+e);
          }
          %>
          </body>
          </html>

          Error:java.sql.SQLException: Field 'idfk' doesn't have a default value it is the error which i am facing it from last 3 months. I have created the tables fully correct

          Faisal Abdullah

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Your code is subject to SQL Injection[^]. You should fix that ASAP.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • _ _AK_

            Faisal_Abdullah wrote:

            Error:java.sql.SQLException: Field 'idfk' doesn't have a default value

            It looks like you have a column which requires data and you are not providing it. Which table has the column "dfk"?

            .AK.

            F Offline
            F Offline
            Faisal_Abdullah
            wrote on last edited by
            #5

            dear i have two tables one is student and second is subject. the student is having columns: id(primary key), roll, name. and the subject table is having columns: id(primary key), idfk(foreign key), name. The problem is that which i wrote query in my code it is fully correct because the student table is being update, but the subject table is not being update due to foreign key of the student which is idfk. So my question is that. which query should i write in my jsp code, which when execute and it should update the table without any error.

            _ J 2 Replies Last reply
            0
            • M Mycroft Holmes

              By the error I suspect you need to set the ID field in Student to an IDENTITY (assuming sql server). You seem to be inserting a new subject for every student. I suspect your table design is crap and needs to be redone.

              Never underestimate the power of human stupidity RAH

              F Offline
              F Offline
              Faisal_Abdullah
              wrote on last edited by
              #6

              dear friend i am using mysql and oracle. would you create the tables and send me their code here. Please because i am searching this problem solution from last 3 months. Please i humbly request you help me.

              1 Reply Last reply
              0
              • F Faisal_Abdullah

                dear i have two tables one is student and second is subject. the student is having columns: id(primary key), roll, name. and the subject table is having columns: id(primary key), idfk(foreign key), name. The problem is that which i wrote query in my code it is fully correct because the student table is being update, but the subject table is not being update due to foreign key of the student which is idfk. So my question is that. which query should i write in my jsp code, which when execute and it should update the table without any error.

                _ Offline
                _ Offline
                _AK_
                wrote on last edited by
                #7

                Faisal_Abdullah wrote:

                the student is having columns: id(primary key), roll, name. and the subject table is having columns: id(primary key), idfk(foreign key), name

                What is the parent for "idfk(foreign key)" key. You need to include this ID in your INSERT query if something like below:

                "INSERT INTO subject(idfk, name) VALUES('"+value+"','"+subject+"')";

                .AK.

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  By the error I suspect you need to set the ID field in Student to an IDENTITY (assuming sql server). You seem to be inserting a new subject for every student. I suspect your table design is crap and needs to be redone.

                  Never underestimate the power of human stupidity RAH

                  F Offline
                  F Offline
                  Faisal_Abdullah
                  wrote on last edited by
                  #8

                  yes you are absolutly right, but how i can do it in oracle and mysql?

                  M 1 Reply Last reply
                  0
                  • F Faisal_Abdullah

                    dear i have two tables one is student and second is subject. the student is having columns: id(primary key), roll, name. and the subject table is having columns: id(primary key), idfk(foreign key), name. The problem is that which i wrote query in my code it is fully correct because the student table is being update, but the subject table is not being update due to foreign key of the student which is idfk. So my question is that. which query should i write in my jsp code, which when execute and it should update the table without any error.

                    J Offline
                    J Offline
                    Jorgen Andersson
                    wrote on last edited by
                    #9

                    If you're using an AUTO_INCREMENT column in MySQL you can retrieve that value using Last_Insert_ID[^] and then use that ID as fkid in the insert to the subject table.

                    Wrong is evil and must be defeated. - Jeff Ello[^]

                    1 Reply Last reply
                    0
                    • F Faisal_Abdullah

                      yes you are absolutly right, but how i can do it in oracle and mysql?

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #10

                      Faisal_Abdullah wrote:

                      yes you are absolutly right, but how i can do it in oracle and mysql?

                      Get a book, follow some examples, learn, no one here is going to do your work for you.

                      Never underestimate the power of human stupidity RAH

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups