insertuploadimahe 2 Answer(s) 3 years and 3 months ago
Posted in : JDBC
i having a 4 textbox and 3 imagetextbox so how i can enter this data in database using servlet
View Answers
February 23, 2010 at 8:42 AM
Sorry i donot know about imagetextbox so i will give sample code to how to insert image and other data to databse. I'm using netbeans ide to create this example and enterprisedb to as my database and glassfish v2 as my application server.
Sample table script below.(this script may vary if you use other data base such as oracle ,mysql,sql server ...)
CREATE TABLE sample_data ( name character varying(15) NOT NULL, age integer NOT NULL, year integer NOT NULL, image bytea NOT NULL, primary key (name) )
To retrieve values from jsp components(textboxes )you can use request.getParameter(param name ) method .
Sample jsp code is below.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
--------------------------------------------------------------------------- Sample Servlet is below. /* * To change this template, choose Tools | Templates * and open the template in the editor. */
/** * * @author pradeep */ public class processData 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(); try {
String name=request.getParameter("name"); int age=Integer.parseInt(request.getParameter("age")); int year=Integer.parseInt(request.getParameter("year")); String imgpath=""; //for image get image path in your server app get image path DBProcess bProcess=new DBProcess(); int result= bProcess.saveData(name, age, year, imgpath); //display status of the statement if(result==0){ //display success msg }else{ //display error msg }
// <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 */ @Override 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 */ @Override 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 */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold>