im a beginner in java servlet my problem is i want to generate a account no . eachtime there would be a new account on my table.but the trigger is not workin pls help the othe values like fname lname username and pword etc are workin fine the servlet is ok but the trigger for creating the accno is not workin properly
CREATE OR REPLACE TRIGGER T1 BEFORE INSERT ON UINFO
FOR EACH ROW
BEGIN
set new.UACCNO="jith"||to_char(SQ_1.nextval,'FM0009');
END;
/
//this is my servlet prog
String s1=hreq.getParameter("fname");
String s2=hreq.getParameter("lname");
String s3=hreq.getParameter("uacc");
String s4=hreq.getParameter("uname");
String s5=hreq.getParameter("pword");
String s6=hreq.getParameter("pword2");
String s7[]=hreq.getParameterValues("select");
String s8=hreq.getParameter("uans");
String s9=hreq.getParameter("ueid");
PrintWriter pw=hres.getWriter();
//if passwords match
if(s6.equals(s5))
{
PreparedStatement pstmt=con.prepareStatement("insert into uinfo(fname,lname,uacc,uname,pword,uques,uans,ueid) values(?,?,?,?,?,?,?,?)");
pstmt.setString(1,s1);
pstmt.setString(2,s2);
pstmt.setString(3,s3);
pstmt.setString(4,s4);
pstmt.setString(5,s5);
for(int i=0;i<s7.length;i++)
{
if(s7[i]!=null)
pstmt.setString(6,s7[i]);
}
pstmt.setString(7,s8);
pstmt.setString(8,s9);
pstmt.executeUpdate();
pw.println("<html><body bgcolor=wheat text=blue>");
pw.println("<h1>user "+s4+" has Registered successfully</h1><br/><br/>");
pw.println("<h3><a href=login.html>Login Now?</a></h3>");
pw.println("</body></html>");
}
else
{
pw.println("<html><body bgcolor=wheat text=blue>");
pw.println("<h1>**the passwords do not match go back and check</h1>");
pw.println("</body></html>");
}
Using a Trigger to create a consecutive for a record is overkill, you should call the sequence from another PreparedStatement, like this:
Then after pulling out the consecutive, do the required transformation to create your key and pass it in the insert statement.
Another option you have is directly passing the
SQ_1.nextvalin the insert statement, but this might depend on the db engine you are using.If it’s oracle as I suspect, It would be like this (please note the example below is not doing the concatenation/transforming you do in the trigger, I leave that up to you):