JSP servlet 连接失败

JSP servlet connection unsuccessful

我正在尝试使用 jsp 和 servlet 创建一个简单的登录和注册系统。这不会给我任何错误,但控制台显示连接不成功,还添加了 jar 文件。

控制台消息

    Connection unsuccessful
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.lang.NullPointerException

login.jsp

<form action="sign" method="post" >
        Enter User name : <input type="text" name="uname"><br>
        Enter Password : <input type="text" name="pass"><br> <input
            type="submit" name="submit" value="Login"> <a href = "signin.jsp">Register</a>
    </form>

signin.jsp

<form action="sign"  method="post">
        Enter User name : <input type="text" name="uname"><br>
        Enter Email : <input type="text" name="email"><br>
        Enter Password : <input type="text" name="pass"><br>
        Enter Confirm Password : <input type="text" name="pass2"><br>
         <input type="submit" name="submit" value="Signup">
    </form>

Impl.java

public User getUser(String username, String paswword) {

        User u = new User();

        try {

            con = c.connect();

            ps = con.prepareStatement("select * from signin where uname=? and pass=?");
            ps.setString(1, username);
            ps.setString(2, paswword);

            ResultSet rs = ps.executeQuery();

            while(rs.next()) {
                u.setUsername(rs.getString(1));
                u.setPassword(rs.getString(2));
            }

        } catch (Exception e) {
            System.out.println(e);
        }

        return u;
    }

config.java

public Connection connect() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp", "root", "");

            System.out.println("Connection Successful");
        } catch (Exception e) {
            System.out.println("Connection unsuccessful");
            System.out.println("" + e);
        }

        return con;
    }

我遇到的唯一错误是插入命令没有执行,我需要添加 ant 依赖项吗?

您需要在您的 lib 文件夹中添加 mysql-connector jar...

https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.49/mysql-connector-java-5.1.49.jar

将上述文件复制到您的 lib 目录并重新启动服务器...