0

(对不起,我有这么多的代码,但我真的不知道多少才够用)

我在将值插入 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.
}
}

我不断收到的一个错误是“需要不兼容的类型:com.mysql.jdbc.Statement found: java.sql.Statement”在 where “statement = conn.createStatement();”下 是。

4

2 回答 2

1

看来您有导入语句

import com.mysql.jdbc.Statement;

代替

import java.sql.Statement;

这在本网站上已多次说明,但您应该考虑使用它PreparedStatement来防止SQL 注入 攻击

于 2013-08-25T01:51:03.333 回答
1

你不应该需要投射,所以不要。还要删除 mysql Statement 类的 import 语句——你也不应该需要它。

相反,首先简单地使用对象而不引用它:

conn.createStatement().executeUpdate(query);

一旦你得到这个工作,只有在你真的需要时才考虑持有对 Statement 对象的引用。

于 2013-08-25T01:53:33.390 回答