【问题标题】:PreparedStatement does not work with MySqlPreparedStatement 不适用于 MySql
【发布时间】:2017-06-29 14:53:11
【问题描述】:

我是 java web 新手。我想编写简单的登录页面,将数据插入 MySql。当我运行代码时,一切运行正常,但是数据 int 插入到数据库中。我现在搜索了大约 2 天但无法解决。谢谢你的帮助 第一个是我的数据库类名是 MyDb,第二个是 server.Also 我的 id 是自动递增的

#1
Connection conn;
public Connection getCon(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        DriverManager.getConnection("jdbc:mysql://localhost:3306/sign_up", "root", "12345");
        System.out.println("connection is done");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(MyDb.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(MyDb.class.getName()).log(Level.SEVERE, null, ex);
    }

    return conn;
}
#2
public class registr extends HttpServlet {
    protected void processRequest(HttpServletRequest request,HttpServletResponse   response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        String name = request.getParameter("name");
        String username = request.getParameter("username")
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        MyDb db = new MyDb();
        Connection conn = db.getCon();
        conn.setAutoCommit(true);
        String query = "INSERT INTO sign(name, surname, email, password)" + "VALUES(?, ?, ?, ?)";
        PreparedStatement stmt = conn.prepareStatement(query);
        stmt.setString(1, name);
        stmt.setString(2, username);
        stmt.setString(3, password);
        stmt.setString(4, email);
        stmt.executeUpdate(query);
    } catch (SQLException ex) {
        Logger.getLogger(registr.class.getName()).log(Level.SEVERE,null,ex);
    }
}

【问题讨论】:

  • 添加堆栈跟踪
  • 没有错误码

标签: java mysql jsp


【解决方案1】:

更改stmt.executeUpdate(query);给stmt.executeUpdate();

【讨论】:

    【解决方案2】:

    如果您已经发布了所有 Servlet 类代码,那么您就缺少了一些东西。官方的 Servlet 接口不会自动调用processRequest 方法。这是 HttpServlet 的 JavaDoc 的摘录:

    公共抽象类 HttpServlet 扩展 GenericServlet 实现 java.io.Serializable

    提供一个抽象类,以创建适合网站的 HTTP servlet 进行子类化。 HttpServlet 的子类必须覆盖至少一个方法,通常是其中之一:

    doGet, if the servlet supports HTTP GET requests
    doPost, for HTTP POST requests
    doPut, for HTTP PUT requests
    doDelete, for HTTP DELETE requests
    init and destroy, to manage resources that are held for the life of the servlet
    getServletInfo, which the servlet uses to provide information about itself 
    

    几乎没有理由重写服务方法。服务通过将标准 HTTP 请求分派给每种 HTTP 请求类型的处理程序方法(上面列出的 doXXX 方法)来处理它们。

    所以你要做的是,改变你的Servlet,例如,如下

    public class registr extends HttpServlet {
         public void doPost(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
             processRequest(req, res);
         }
         
         public void doGet(HttpServletRequest req, HttpServletResponse res) throws  ServletException, IOException {
             processRequest(req, res);
         }
         
         protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException     {
             // your code
         } 
    
    }
    

    提示:Java 编码惯例规定类的名称应以大写字母开头,即应为 registr 而不是 Registr

    【讨论】:

    • 非常感谢,现在我明白了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多