如何使用Java servlet将值插入到mysql DataBase中?

时间:2013-08-25 01:45:34

标签: java mysql jsp servlets

(抱歉我的代码很多,但我真的不知道多少就足够了)

我在将值插入mysql数据库时遇到问题。我正在使用Jsp文件而不是html文件。我一直收到这个错误,我不确定是什么导致它。

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.
}
}

我一直遇到的一个错误是“不兼容的typesrequired:com.mysql.jdbc.Statement found:java.sql.Statement”,其中“statement = conn.createStatement();”是。

2 个答案:

答案 0 :(得分:1)

看来你有导入声明

import com.mysql.jdbc.Statement;

而不是

import java.sql.Statement;

此网站已多次说明,但您应考虑使用PreparedStatement代替SQL Injection attacks

答案 1 :(得分:1)

你不应该施展,所以不要。同时删除mysql Statement类的import语句 - 你也不需要。

相反,首先只使用该对象而不引用它:

conn.createStatement().executeUpdate(query);

一旦你开始工作,只有在你真的需要的时候才考虑持有对Statement的引用。