
hi i'm beginner 2 j2ee.. my question is...
when user enters his fname,lname,address,city from ui(html) it gets inserted into db...my problem is when user submit without entering i,e blank it also get inserted into db even though i have given not null for each elements in table....

Use javascript.
Here is the code:
1)register.html:
<html>
<script>
function validate(){
if(document.form.fname.value == ""){
alert( "Please enter first name." );
document.form.fname.focus();
return false;
}
if(document.form.lname.value == ""){
alert( "Please enter last name." );
document.form.lname.focus();
return false;
}
if(document.form.address.value == ""){
alert( "Please enter address." );
document.form.address.focus();
return false;
}
if(document.form.city.value == ""){
alert( "Please enter city." );
document.form.city.focus();
return false;
}
return true;
}
</script>
<form name="form" method="post" onsubmit="return validate();" action="http://localhost:8080/examples/jsp/insertdata.jsp">
<table>
<tr><td>Enter First Name</td><td><input type="text" name="fname"></td></tr>
<tr><td>Enter Last Name</td><td><input type="text" name="lname"></td></tr>
<tr><td>Enter Address</td><td><input type="text" name="address"></td></tr>
<tr><td>Enter City</td><td><input type="text" name="city"></td></tr>
<tr><td></td><td><input type="submit" value="Add" name="button"></td></tr>
</table>
</form>
</html>
2)insertdata.jsp:
<%@page import="java.sql.*,java.util.*"%>
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String address=request.getParameter("address");
String city=request.getParameter("city");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into student(firstname,lastname,address,city) values('"+fname+"','"+lname+"','"+address+"','"+city+"')");
out.println("Data is successfully inserted into database.");
}
catch(Exception e){
System.out.println(e);
}
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.