错误是
cannot find symbol
symbol: method preparedStatement(String) Location: variable conn of type Connection
我的代码是;@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); }
最佳答案
您是否缺少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);
关于java - 找不到符号:方法prepareStatement(String)位置:类型连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50704206/