好的,所以我在这里寻求更多帮助:3在此之前,我要非常感谢。
此刻,我正在尝试创建一个简单的“注册”,其中输入了用户名,电子邮件和密码。
尽管我确实使用executeQuery(); <我在检查用户是否存在时执行此操作。如果不是,则该对象仍为null,如下面的代码所示(注册类):
package Backend;
import DataMapper.UserMapper;
import Model.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "opret", urlPatterns = {"/opret"})
public class opret extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
boolean loggedin = false;
loggedin = (boolean) session.getAttribute("loggedin");
if (loggedin == false) {
String username = request.getParameter("username");
String email = request.getParameter("email");
String password = request.getParameter("password");
User tempUser = null;
tempUser = UserMapper.receiveUser(email);
if (tempUser != null) {
String message = "En bruger med den samme email eksisterer allerede, prøv igen.";
request.setAttribute("failMessage", message);
request.getRequestDispatcher("/opret.jsp").forward(request, response);
} else {
UserMapper.createUser(username, password, email);
User currUser = null;
currUser = UserMapper.receiveUser(email);
sessionHandling.clearSession(request, response); // Clears session through iterating an enumeration.
loggedin = true;
session.setAttribute("user", currUser);
session.setAttribute("user_id", currUser.getId());
session.setAttribute("username", currUser.getUsername());
session.setAttribute("email", currUser.getEmail());
session.setAttribute("balance", currUser.getBalance());
session.setAttribute("loggedin", loggedin);
request.getRequestDispatcher("/user.jsp").forward(request, response);
}
} else {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
我知道有些代码是用“丹麦语”编写的,即字符串Message,但它仍然有意义:3
我得到的错误是:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.StatementImpl.checkForDml(StatementImpl.java:385)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1153)
at DataMapper.UserMapper.createUser(UserMapper.java:61)
at Backend.opret.doPost(opret.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
首先,我们可以看看UserMapper.java:61
方法如下:
public static void createUser(String username, String password, String email) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "";
final int permission = 0;
final Double balance = 0.0;
try {
Connection con = Connector.connection();
sql = "INSERT INTO user (`username`, `password`, `permission`, `email`, `balance`) VALUES (?, ?, ?, ?, ?);";
ps = con.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ps.setInt(3, permission);
ps.setString(4,email);
ps.setDouble(5, balance);
ps.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
我们很容易注意到我在这里不使用executeQuery(),而是使用executeUpdate()...
现在我很茫然...
我确实知道我没有在ID中“插入”任何内容,但这是主键,而不是null和自动递增..我不知道这是否是问题,但我觉得不应该是问题。
我希望你们能对这个问题有所了解,我一直在到处搜索并尝试许多事情... ...
在使用这个createUser系统时,我有时会接收用户检查它是否已经存在,该方法实际上使用了executeQuery(),因为它是一条select语句,我将其张贴在这里。
public static User receiveUser(String emails) {
User tempUser = null;
Connection con = null;
PreparedStatement ps = null;
String sql = null;
try {
con = Connector.connection();
sql = "select * from user where email = ?";
ps = con.prepareStatement(sql);
ps.setString(1, emails);
ResultSet resultSet = ps.executeQuery();
while(resultSet.next()) {
int id = resultSet.getInt("id");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
int permission = resultSet.getInt("permission");
String nyEmail = resultSet.getString("email");
Double balance = resultSet.getDouble("balance");
tempUser = new User(id, username, password, permission, nyEmail, balance);
return tempUser;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return tempUser;
}
答案 0 :(得分:0)
即使我的重新编译尝试似乎成功了,这似乎也是IntelliJ IDEA的一个问题,因为它无法自动选择要重新编译的.java文件。
我分别重新编译了所有Java文件,现在它可以正常工作..想想我对此很头疼:3对不起,浪费大家的时间。 。 ..