Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

(Sorry for the amoount Of code I have, but I really don't know how much is enough)

I'm having trouble inserting values into the mysql database. I'm using Jsp files instead of html files. I keep getting this error and I'm not sure what's causing it.

public class AddTo extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException {
    response.setContentType("text/html;charset=UTF-8");

    {

        String name = request.getParameter("name");

        String dbURL = "jdbc:mysql://localhost:3306/movieDB";
        String username = "root";
        String password = "sund ";

        try {
            Connection conn = (Connection) DriverManager.getConnection(
            dbURL, username, password);

            } 

        catch (SQLException ex) {
            Logger.getLogger(AddTo.class.getName()).log(Level.SEVERE, null, ex);
            }

        String query = "INSERT INTO table1 " + "VALUES ('" + name + "')";
        Statement statement = null;

        statement = (Statement) conn.createStatement();


        statement.executeUpdate(query);


        Movie aMovie = new Movie(request.getParameter("name"));
        request.setAttribute("user", aMovie);
        String cartRadio = request.getParameter("cartRadio");

        if ( cartRadio.equalsIgnoreCase("cartSelect") ) {

            String url = "/jsp2.jsp";
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request,response);   
        }

        if ( cartRadio.equalsIgnoreCase("listSelect")) {

            String url = "/jsp3.jsp";
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request,response);
        }

        if ( cartRadio.equalsIgnoreCase("none")) {

            String url = "/jsp4.jsp";
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request,response);
        }
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (SQLException ex) {
        Logger.getLogger(AddTo.class.getName()).log(Level.SEVERE, null, ex);
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (SQLException ex) {
        Logger.getLogger(AddTo.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private Object getServletContext() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

One error I keep getting is "incompatible typesrequired: com.mysql.jdbc.Statement found: java.sql.Statement" under where "statement = conn.createStatement();" is.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

It appears you have the import statement

import com.mysql.jdbc.Statement;

instead of

import java.sql.Statement;

This has been stated many times on this site but you should consider using PreparedStatement instead to protect against SQL Injection attacks.

share|improve this answer
    
I usually try to find similar questions before asking. Thank you for the extra links. –  Ebadly.Decipher Aug 25 '13 at 1:56
add comment

You should not need to cast, so don't. Also remove the import statement for the mysql Statement class - you shouldn't need that either.

Instead, first simply use the object without referring to it:

conn.createStatement().executeUpdate(query);

Once you get that working, only then consider holding a reference to the Statement object if you really need one.

share|improve this answer
add comment
String query = "INSERT INTO table1 " + "VALUES ('" + name + "');";

You have a missing semi colon.

share|improve this answer
2  
The error in the question does not point to this being the problem, and, since the accepted answer doesn't point to this problem either (nor any of the other answers or comments), I strongly doubt that is the problem, or even a problem at all. –  Dukeling Mar 19 at 23:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.