简体   繁体   中英

Having issues with a SQLException, Using executeUpdate getting executeQuery error

Alright so I'm here asking for more help :3 Beforehand I'd like to say thanks a lot.

I'm trying to create a simple 'registration' at the moment, where I take input username, email and password.

Although I do use executeQuery(); < I do it when I check if the user exists. If it doesn't then the object is still null, as can be seen in this code below which is registration class:

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 {

    }
}

I know some of the code is written in 'danish' ie the string Message, but it should still make sense :3

The error I get is this:

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)

First we can look at the UserMapper.java:61

The method looks like this:

 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();
        }
    }

We easily notice how I'm not using executeQuery() here, and I'm definetely using executeUpdate() ...

Now I'm just at a loss ...

I do know I'm not 'inserting' anything in the ID, but it is primary key and not null and auto increment .. I don't know if that's the problem but I feel like it shouldn't be the issue..

I hope you guys can shed some light on the problem, I've been searching everywhere and trying lots of things ... ...

When using this createUser system I do at some point receive a user to check if it exists already, this method actually uses executeQuery() as it's a select statement, I've posted it below here.

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;
    }

It seems like even though my attempts to recompile was seen as succesful, it's apparantly an issue with IntelliJ IDEA where it can't automatically select .java files to recompile.

I recompiled all the java files separately and now it works .. To think I had such a headache with this :3 Sorry for wasting everyone's time. . .. . .

  • Sincerely GodLess / Matt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM