how to insert data in foreign key using jsp
-
<%--
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
-
<%--
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
-
<%--
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
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
-
<%--
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
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
-
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.
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.
-
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
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.
-
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.
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.
-
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
yes you are absolutly right, but how i can do it in oracle and mysql?
-
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.
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[^]
-
yes you are absolutly right, but how i can do it in oracle and mysql?
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