gpt4 book ai didi

java - 服务器使用我在代码中甚至没有提到的表

转载 作者:行者123 更新时间:2023-11-29 03:01:33 26 4
gpt4 key购买 nike

我正在使用 NetBeans 8.0。我在数据库“face”中创建了一个表“full”。早些时候,我在数据库“rishi”中创建了一个表“student”。现在,从数据库“rishi”中删除表并将数据库更改为“full”后,我收到以下错误:-

java.sql.SQLException: Base table or view not found message from server: "Table 'face.student' doesn't exist".

这是我的 HTML 代码:-

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login !</h1>
<hr>
<form action = "Check" method = "post">
<pre>
Enter email : <input type = "text" name = "email">
Enter password : <input type = "password" name = "pass">
<input type = "submit" value = "Login">
</pre>
</form>
<hr>
<h3>New ? Sign in today !</h3>
<form action = "New" method = "post">
<pre>
Enter name : <input type = "text" name = "user">
Enter email : <input type = "text" name = "newEmail">
Enter password : <input type="password" name = "newPass">
Re-enter password : <input type="password" name = "repass">
<input type = "submit" value = "Sign Up">
</pre>
</form>
</center>
</body>
</html>

这是我的 Servlet :-

public class New extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name = request.getParameter("user");
String email = request.getParameter("newEmail");
String pass = request.getParameter("newPass");
String repass = request.getParameter("repass");

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/face" , "root" , "root");
Statement st = con.createStatement();
ResultSet r = st.executeQuery("select * from full");
int flag = 0;

while(r.next()) {
String s = r.getString(2);
if(pass.equals(repass) || email.equals(s)) {
flag = 1;
}
else {
flag = 2;
}

if(flag == 2) {
response.sendRedirect("fail2.html");
}
else {
String qr = String.format("insert into student values('%s','%s','%s')" , name , email , pass);
st.executeUpdate(qr);
response.sendRedirect("home.html");
}
}

catch (ClassNotFoundException ex) {
out.println("Cannot load driver !");
} catch (SQLException ex) {
out.println(ex);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}

可能出了什么问题?

提前致谢!

最佳答案

对不起,我的错 - 重新阅读问题..事实上你的数据库连接很好,因为你还有数据库 face

问题出在您的 Insert 语句上 - 您正试图将数据插入到数据库中不存在的表中。

String qr = String.format("insert into student values('%s','%s','%s')" , name , email , pass);

这就是问题所在 - 所以要么你必须在那个"new"数据库中创建一个表 student,要么将其更改为 db 中现有的内容。

String qr = String.format("insert into " insert valid table name here "values('%s','%s','%s')" , name , email , pass);

但我想解决不同的问题 - 我不明白你为什么有这个?

if(pass.equals(repass) || email.equals(s)) {
flag = 1;
}
else {
flag = 2;
}
if(flag == 2) {
response.sendRedirect("fail2.html");
}
else {
String qr = String.format("insert into student values('%s','%s','%s')" , name , email , pass);
st.executeUpdate(qr);
response.sendRedirect("home.html");
}

这个 block (对我来说)似乎是冗余的,因为你检查凭据是否匹配,如果他们将标志设置为 1 - 到目前为止一切顺利,但如果他们不这样做,你将标志设置为 2。那也很好 - 但是为什么你有另一个 if 语句来检查标志是否为 2 ?如果您重定向到 fail.html。否则你做一些查询......它有点冗余(除非你期望超过 2 个标志)如果你不那么下面的这个 block 是更好的解决方案

if(pass.equals(repass) || email.equals(s)) {
String qr = String.format("insert into student values('%s','%s','%s')" , name , email , pass);
st.executeUpdate(qr);
response.sendRedirect("home.html");
}
else {
response.sendRedirect("fail2.html");
}

关于java - 服务器使用我在代码中甚至没有提到的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22895495/

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