数据没有从servlet插入到mysql表中

问题描述 投票:1回答:1

我正在尝试使用java servlets,mysql数据库创建一个注册页面。这是我的servlet代码的一部分。没有错误输出。数据库路径100%正确

 String uName = request.getParameter("username");
    String pass = request.getParameter("passwd");
    String rpass = request.getParameter("rpasswd");
    String email = request.getParameter("email");
        try {
           Class.forName("com.mysql.cj.jdbc.Driver");
            final String DB_URL = "jdbc:mysql://127.0.0.1:3306/userData";
            Connection con = DriverManager.getConnection(DB_URL, "serv", "serv");
            String sql = "INSERT INTO users (username, password, em) values (?, ?, ?)";
            PreparedStatement stat = con.prepareStatement(sql);
            stat.setString(1, uName);
            stat.setString(2, pass);
            stat.setString(3,email);
            con.close();
        }
        catch(SQLException e) {
            System.out.println(e);
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        }
java mysql servlets
1个回答
3
投票
String uName = request.getParameter("username");
String pass = request.getParameter("passwd");
String rpass = request.getParameter("rpasswd");
String email = request.getParameter("email");
Class.forName("com.mysql.cj.jdbc.Driver");
final String DB_URL = "jdbc:mysql://127.0.0.1:3306/userData";
try (Connection con = DriverManager.getConnection(DB_URL, "serv", "serv")) {
    final String sql = "INSERT INTO users (username, password, em) values (?, ?, ?)";
    final PreparedStatement stat = con.prepareStatement(sql);
    stat.setString(1, uName);
    stat.setString(2, pass);
    stat.setString(3, email);
    stat.execute(); // you miss this
} catch(Exception e) {
    System.out.println(e);
}

PreparedStatement javadoc开始,execute()在这个PreparedStatement对象中执行SQL语句,该对象可以是任何类型的SQL语句。

© www.soinside.com 2019 - 2024. All rights reserved.