(对不起,我有这么多的代码,但我真的不知道多少才够用)
我在将值插入 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();”下 是。