can't insert data into database [message #492448] |
Mon, 31 January 2011 05:04 |
|
prabu1990
Messages: 1 Registered: January 2011 Location: chennai
|
Junior Member |
|
|
I am new in netbeans.I have developed online application using netbeans as frontend,oracle database10g as backend.Some records are not inserted into database.So,kindly send the solution here i attached my code.
[jspformcode]
<form action="validation.jsp" method="POST">
Name:<input type="text" name="name" value="" size="20" />
<br>
Mobile:<input type="text" name="mobile" value="" size="20" />
<br>
Email:<input type="text" name="email" value="" size="20" />
<br>
<input type="submit" value="Submit" />
<br>
<input type="reset" value="Reset" />
<br>
[validation jsp]
<%@ page import="java.util.*" %>
<jsp:useBean id="formHandler" class="Model.formbean" scope="request">
<jsp:setProperty name="formHandler" property="*"/>
</jsp:useBean>
<%
if (formHandler.validation())
{
%>
<jsp:forward page="jservlet"/>
<%
}
else
{
%>
<jsp:forward page="finalerror.jsp"/>
<%
}
%>
[/validation jsp]
[formbean]
package Model;
import java.util.*;
/**
*
* @author knuuser
*/
public class formbean
{
private boolean retu=false;
private String name;
private String mobile;
private String email;
private Hashtable errors;
public boolean validation()
{
boolean aname=validationcode.isValidname(name);
boolean amobile=validationcode.isValidmobile(mobile);
boolean aemail=validationcode.isEmailValid(email);
if(aname==false)
{
errors.put("name","Please Enter Valid Name ");
}
if(amobile==false)
{
errors.put("mobile","Enter the correct mobile number");
}
if(aemail==false)
{
errors.put("email","Please enter A Valid EmailId");
}
if(aname==true&amobile==true&aemail==true)
{
retu=true;
}
return retu;
}
public formbean()
{
name="";
mobile="";
email="";
errors = new Hashtable();
}
public String getname()
{
return name;
}
public void setname(String nm)
{
name=nm;
}
public String getmobile()
{
return mobile;
}
public void setmobile(String mbl)
{
mobile=mbl;
}
public String getemail()
{
return email;
}
public void setemail(String eml)
{
email=eml;
}
public String getErrorMsg(String s)
{
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}
public void setErrors(String key, String msg)
{
errors.put(key,msg);
}
}
[/formbean]
[validationcode]
package Model;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author knuuser
*/
public class validationcode
{
public static boolean isValidname(String name)
{
boolean isValid=false;
String expression="^[a-zA-Z]+$";
CharSequence inputStr=name;
Pattern pattern=Pattern.compile(expression);
Matcher matcher=pattern.matcher(inputStr);
if(matcher.matches())
{
isValid=true;
}
return isValid;
}
public static boolean isValidmobile(String mobile)
{
boolean isValid=false;
String expression ="^([7-9]{1})([0-9]{1})([0-9]{8})$";
CharSequence inputStr=mobile;
Pattern pattern=Pattern.compile(expression);
Matcher matcher=pattern.matcher(inputStr);
if(matcher.matches()) {
isValid=true;
}
return isValid;
}
public static boolean isEmailValid(String email)
{
boolean isValid=false;
String expression="^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr=email;
Pattern pattern=Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(inputStr);
if(matcher.matches()) {
isValid=true;
}
return isValid;
}
}
[/validationcode]
[jservlet]
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class jservlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection con;
try
{
String name=request.getParameter("name");
String mobile=request.getParameter("mobile");
String email=request.getParameter("email");
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@10.10.10.10:1521:ORADB", "empmgt", "empmgt");
Statement st=con.createStatement();
PreparedStatement ps=con.prepareStatement("insert into svpjs Values(?,?,?)");
ps.setString(1,name);
ps.setString(2,mobile);
ps.setString(3,email);
ps.executeUpdate();
out.println("Details Entered Sucessfully");
}
catch (SQLException ex)
{
ex.printStackTrace();
}
finally
{
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
[/jservlet]
Regards
Vasanth
|
|
|