Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JDBC and Relational Databases
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Tim Cooke
Devaka Cooray
Sheriffs:
Liutauras Vilda
paul wheaton
Rob Spoor
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Piet Souris
Mikalai Zaikin
Bartenders:
Carey Brown
Roland Mueller
Forum:
JDBC and Relational Databases
i have a problem insert data into database but i didnt get any error
Irfan Zubair
Greenhorn
Posts: 17
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
//addCourseValidate.java package Controller; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import java.util.LinkedList; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.sql.DataSource; /** * * @author Zubair */ public class addCourseValidate extends HttpServlet { /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ PreparedStatement pst; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); List errorMsg=new LinkedList(); boolean flag=false; HttpSession session=request.getSession(true); try { /* * TODO output your page here. You may use following sample code. */ String coursename=request.getParameter("coursename").trim(); String fee=request.getParameter("fee").trim(); String prerequisites=request.getParameter("prerequisites").trim(); String status=null; String username; int count=0; DataSource ds=getAddCourse(); Connection con=ds.getConnection(); Statement st=con.createStatement(); username=(String) session.getAttribute("username"); if(coursename.isEmpty()&&fee.isEmpty()&&prerequisites.isEmpty()){ errorMsg.add("Enter all fields."); } else if(coursename.isEmpty()&&fee.isEmpty()){ errorMsg.add("Enter a valid course name and fee.<br/>"); } else if(coursename.isEmpty()&&prerequisites.isEmpty()){ errorMsg.add("Enter a valid course name and prerequisites.<br/>"); } else if(fee.isEmpty()&&prerequisites.isEmpty()){ errorMsg.add("Enter a valid fee and prerequisites.<br/>"); } else if(coursename.isEmpty()){ errorMsg.add("Enter a valid course name.<br/>"); } else if(fee.isEmpty()){ errorMsg.add("Enter a valid fee.<br/>"); } else if(prerequisites.isEmpty()){ errorMsg.add("Enter a valid prerequisites.<br/>"); } [color=red] String sql="insert into fastlearn.coursefl values(?,?,?,?)"; pst=con.prepareStatement(sql); ResultSet rs = st.executeQuery("select count(*) from fastlearn.coursefl"); while(rs.next()) { count=rs.getInt(1); } pst.setInt(1, count); pst.setString(2, coursename); pst.setString(3, fee); pst.setString(4,prerequisites ); [/color] errorMsg.add(" "); status="Added"; out.println("successfully added"); request.setAttribute("status", status); request.setAttribute("errorMsg", errorMsg); RequestDispatcher view=request.getRequestDispatcher("addCourse.view"); view.forward(request, response); return; } catch (SQLException ex) { ex.printStackTrace(); } catch (NamingException 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 { processRequest(request, response); } /** * 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 { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ public String getServletInfo() { return "Short description"; }// </editor-fold> private DataSource getAddCourse() throws NamingException { Context c = new InitialContext(); return (DataSource) c.lookup("java:comp/env/AddCourse"); } } //addCourseForm.java package View; import Model.Course; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Zubair */ public class addCourseForm extends HttpServlet { /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @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 processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); List errorMsg=new LinkedList(); errorMsg.clear(); errorMsg=(List)request.getAttribute("errorMsg"); String coursename=request.getParameter("coursename"); String fee=request.getParameter("fee"); String prerequisites=request.getParameter("prerequisites"); String status=null; status=(String)request.getAttribute("status"); if(coursename==null){ coursename=""; } if(fee==null){ fee=""; } if(prerequisites==null){ prerequisites=""; } if(status!=null){ if(status.equals("Added")){ status=coursename; coursename=""; fee=""; prerequisites=""; } } try { /* * TODO output your page here. You may use following sample code. */ out.println("<html>"); out.println("<head>"); out.println("<title>Add Course</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1> Add Course </h1>"); out.println("<form method=\"POST\" action=\"addCourse.do\">"); out.println("<center>"); out.println("<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\">"); out.println(" <tr>"); out.println("<td>Course Name</td>"); out.println(" <td><input type=\"text\" name=\"coursename\" value=\""+coursename+"\" size=\"30\"></td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>Fee</td>"); out.println("<td><input type=\"text\" name=\"fee\" value=\""+fee+"\" size=\"30\"></td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>Prerequisites</td>"); out.println("<td><input type=\"text\" name=\"prerequisites\" value=\""+prerequisites+"\" size=\"30\"></td>"); out.println("</tr>"); if(errorMsg!=null) { Iterator items=errorMsg.iterator(); while(items.hasNext()){ String next=(String)items.next(); if(next.equals("Added")){ out.println("<tr><td colspan=\"2\">"); out.println("<font color=\"green\">"); out.println("<p>"); out.println("<i> "+status+" </i>successfully added."); out.println("</p>"); out.println("</font>"); out.println("</td></tr>"); } else{ out.println("<tr><td colspan=\"2\">"); out.println("<font color=\"red\">"); out.println("<p>"); out.println(next); out.println("</p>"); out.println("</font>"); out.println("</td></tr>"); } } } out.println("<tr>"); out.println("<td colspan=\"2\"><input type=\"submit\" value=\"Add Course\"></td>"); out.println("</tr>"); out.println("</table>"); out.println("</center>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); } 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 { processRequest(request, response); } /** * 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 { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ public String getServletInfo() { return "Short description"; }// </editor-fold> }
Paul Clapham
Marshal
Posts: 28296
95
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Irfan Zubair wrote:
i have a problem inserting data into database
And what exactly is that problem?
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
not able to insert values in ms access database
javax.servlet.ServletException: PWC1381: Error allocating a servlet instance
Problem in Login using Servlets
user authentication and dao
Change password for user
More...