4 Replies - 15089 Views - Last Post: 27 November 2011 - 03:04 PM

#1 kingofleinster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 09-December 08

Insert Data into mysql Database using netbeans Enterprise Project

Posted 16 November 2011 - 10:04 AM

Hi Folks

Hope you can help

I'm having a problem Inserting data into a Database using Netbeans Enterprise project , I can view the data in the table no problem but writing does nothing

Here is the code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import EJB.CategoryFacade;
import EJB.Category;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.sql.*;
/**
 *
 * @author paul
 */
@WebServlet(name = "addCategory", urlPatterns = {"/addCategory"})
public class addCategory extends HttpServlet {
    
    @EJB
    
    private CategoryFacade cat;

    /** 
     * 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, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet addCategory</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet addCategory at " + request.getContextPath () + "</h1>");
            out.println("<form>");
            out.println("New Category:<input type='text' name='catName'><br/>");
            out.println("<input type='submit'><br/>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
           
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookbean", "root","glenhill15");
            Statement stat = (Statement) con.createStatement();
            //just a test to see if it will insert
            String dname = "Sports";
            
            String insert = "INSERT INTO category VALUES ('" + dname +"')";
            
            stat.executeUpdate(insert);
            
        } 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
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(addCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
         
        
    }

    /** 
     * 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 {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(addCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
        
       
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}







Any help will be much appreciated , As i'm stuck

This post has been edited by kingofleinster: 16 November 2011 - 10:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Insert Data into mysql Database using netbeans Enterprise Project

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,294
  • Joined: 27-December 08

Re: Insert Data into mysql Database using netbeans Enterprise Project

Posted 16 November 2011 - 11:06 AM

Moved to Java EE.

You should at least catch your Exceptions and Log the Stack Trace for debugging purposes.
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Insert Data into mysql Database using netbeans Enterprise Project

Posted 16 November 2011 - 11:11 AM

You should not be writing HTML into a servlet. Is it not painful to type? Try using MVC.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Insert Data into mysql Database using netbeans Enterprise Project

Posted 16 November 2011 - 03:14 PM

You may need to load the SQL driver before using it. Put

Class.forName("com.mysql.jdbc.Driver").newInstance();


right before

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookbean", "root","glenhill15");


Everything else in the code looks correct. See this. Also, make sure the jar file is in your class path.

This post has been edited by blackcompe: 16 November 2011 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 Sodefeattaigo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-November 11

Re: Insert Data into mysql Database using netbeans Enterprise Project

Posted 27 November 2011 - 03:04 PM

that I interfere, but it is necessary for me little bit more information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1