gpt4 book ai didi

java - JSP servlet 连接失败

转载 作者:行者123 更新时间:2023-12-01 16:50:46 25 4
gpt4 key购买 nike

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

控制台消息

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

登录.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>

登录.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;
}

配置.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 目录中并重新启动服务器...

关于java - JSP servlet 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61694620/

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