Hello Everyone,
May someone help me with servlet for inserting records in mysql table. It is giving me some error saying that some column data has been tracanted.
second how can I change String variable into int variable. The code is below
Code:import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; public class Addendangeredspecie extends HttpServlet { // process "get" requests from clients protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); String tname=request.getParameter("Name"); String tlocation=request.getParameter("Location"); String tlocationdescrip=request.getParameter("Location_Description"); String ttype=request.getParameter("Specie_Type"); String tnumber=request.getParameter("Number_of_species"); String tapproved=request.getParameter("approved"); String tdateofsub=request.getParameter("Date_of_Submitting"); String tdateofsighting=request.getParameter("Date_of_sighting"); String tweight=request.getParameter("Weight"); int nuke; nuke=2; //variables to do with endangered animals String tnumberlegs=request.getParameter("Name_of_legs"); String ttypeanimals=request.getParameter("Type_of_Animal"); //variables to do with endangered plants String tplanttype=request.getParameter("Type_of_plant"); String tseasontype=request.getParameter("Season"); // send XHTML page to client try{ DAO con=new DAO(); //Getting a connection Connection myconn=con.getConnection(); //create a statement Statement stmt =myconn.createStatement(); String insertQuery="Insert into endangeredspecies (Name,Location,LocationDescription,Type,Number,Approved,DateofSubmitting,Dateofsighting) values ('"+tname+"','"+tlocation+"','"+tlocationdescrip+"','"+ttype+"','"+nuke+"','"+tapproved+"','"+tdateofsub+"','"+tdateofsighting+"')"; String selectQuery="Select * from endangeredspecies"; //String insertQuery2="Insert into animal (Idspices ,NumberLegs,TypeAnimals) values ('"+nuke+"','"+tnumberlegs+"','"+ttypeanimals+"')"; //String insertQuery3="Insert into plant (Idspices ,PlantType,TypeSeasonal) values ('"+nuke+"','"+tplanttype+"','"+tseasontype+"')"; int insertStatus=stmt.executeUpdate(insertQuery); ResultSet myresults=stmt.executeQuery(selectQuery); // start XHTML document out.println( "<?xml version = \"1.0\"?>" ); out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); out.println( "<html xmlns = \"http://www.w3.org/1999/xhtml\">" ); // head section of document out.println( "<head>" ); out.println( "<title> Endangered Species Database </title>" ); out.println( "</head>" ); // body section of document out.println( "<body align=\"center\">" ); out.println( "<h2> You have successfully added the record</h2>" ); out.println( "<p></p>" ); out.println( "Details submitted for "+tname+" "+ttype); if(insertStatus>0) out.println("<h3>Information has been add</h3>"); else out.println("Your info has been update"); out.println("<table>"); while(myresults.next()){ out.println("<tr><td>"+myresults.getString(1)+"</td></tr>"); } out.println("</table>"); out.println( "</body>" ); // end XHTML document out.println( "</html>" ); out.close(); myconn.close(); } catch(Exception ex) { System.out.println("Error"+ex.getMessage());// close stream to complete the page } } }




Bookmarks