gpt4 book ai didi

java - 找不到符号:方法prepareStatement(String)位置:类型连接

转载 作者:行者123 更新时间:2023-12-02 11:10:23 25 4
gpt4 key购买 nike

错误是

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com