找不到符号:method preparedStatement(String)Location:type Connection

时间:2018-06-05 15:48:28

标签: java compiler-errors

错误是

  

无法找到符号

     
    

symbol:方法preparedStatement(String)     位置:连接类型的变量conn

  

我的代码是;

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
        String url = "jdbc:mysql://localhost:3306/sampleDB";
        String un = "root";
        String pw = "";

        String firstname = request.getParameter("fname");
        String lastname = request.getParameter("lname");
        String email = request.getParameter("mail");
        String pass = request.getParameter("pwd1");
        String pass2 = request.getParameter("pwd2");

        String sql = "INSERT INTO stu(firstname,lastname,email,pass) VALUES(?,?,?,?)";
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn;
        conn = (Connection) DriverManager.getConnection(url, un, pw);
        PreparedStatement pstmt = conn.preparedStatement(sql);       
    } catch (ClassNotFoundException | SQLException e) {

    }

    //processRequest(request, response);
}

1 个答案:

答案 0 :(得分:0)

您错过了PreparedStatement?参数,您可以这样指定:

PreparedStatement pstmt = conn.preparedStatement(sql);
pstmt.setString(1, "Carlos");
pstmt.setString(2, "Moreira");
pstmt.setString(3, "Carlos@rj.com");
pstmt.setString(4, "cake$$@1");

此外,还有一个错字:

PreparedStatement pstmt = conn.preparedStatement(sql);       

应该是 prepareStatement

PreparedStatement pstmt = conn.prepareStatement(sql);