Reputation: 336
I am using below code to insert into a table using JSP:
<%@ page import="java.sql.*" %>
<%@ page language="java" import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>conn</head>
<body>
<form action = "" method = "POST">
id : <input type = "text" name = "eid">
name: <input type="text" name="first_name">
<input type="submit" value="Submit" />
</form>
<%
String data1 = request.getParameter("eid");
String data = request.getParameter("first_name");
%>
<%
Connection conn = null;
Statement stmt = null;
ResultSet result = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","Password");
PreparedStatement ps = null;
String query = "insert into employee(eid,ename) values(?,?)";
ps = conn.prepareStatement(query);
ps.setString(1,data1);
ps.setString(2,data);
ps.executeUpdate();
%>
</body>
</html>
I am getting error "ORA-01400: cannot insert NULL into ("SYSTEM"."EMPLOYEE"."EID")"
I am getting this error even before the form appears. Why the insertion is happening before I click the submit button.
Thanks!!!!
Upvotes: 0
Views: 854
Reputation: 74
If you want to allow your JSP to be able to post to itself and process requests I would probably do the following:
<%
String eid = request.getParameter("eid");
String first_name = request.getParameter("first_name");
if(eid != null && first_name != null){
Connection conn = null;
Statement stmt = null;
ResultSet result = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
try{
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","Password");
PreparedStatement ps = null;
String query = "insert into employee(eid,ename) values(?,?)";
ps = conn.prepareStatement(query);
ps.setString(1,eid);
ps.setString(2,first_name);
ps.executeUpdate();
}catch(SQLException sqle){
//log sql exception
} finally {
if(conn != null){
try{
conn.close();
}catch(SQLException sqle){
//log exception
}
}
}
%>
Remember that with JSP all of the work is done server side so if there is a logical mistake in your code most it will happen before your page will get fully rendered. If you want to do a query to the database then you should make sure that you sent the necessary information over. You can get that information by accessing it through your JSP page's request object. Next I put an if statement around your code to make sure that these two values are populated. Finally I placed your code in a try/catch/finally structure in order to make sure that the database connection is closed. This last bit probably isn't that big of a deal for you (I take it your doing this for some class or just to learn) but it could case problems if you're running a server for a long time and you forget to close database connections.
Upvotes: 1
Reputation: 1242
If you want to work just with JSP :
you can put all the code between <% %>
in another jsp page where you pass data to it
But it's not a good practise , it's recommended to work with servlet
that get data from jsp
and pass it to bean , also in the bean
you put your code to connect with database ...
Upvotes: 2
Reputation: 12748
When the page loads, each block inside <% %> is executed on server side and result is passed to the page. Thus, you get the said behavior.
Your blocks <% %> are related to the fom in no way.
Upvotes: 1