G'day guys,

My boss has just asked me to develop a small CRM system. Now, writing a simple SQL Insert is not something im a stranger to, however for some reason i cannot seem to get this working.

I've included all necessary libraries, and tested the actual sql statement in my sql cli, but when running through java, it seems to hit a snag.

If anyone could have a look at my code below, and let me know what i've been doing wrong. Thanks.


I've just found the following error messages;
- java.sql.SQLException: Can not issue data manipulation statements with executeQuery().

- INFO: Illegal access: this web application instance has been stopped already. Could not load java.net.BindException. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException

import java.sql.*;

public class client {

    .......
    .......

    public String addClient(){
        boolean isCreated = false;
        String sql = "";
        
        try{
            Connection con = new database().getConnection();
            Statement stmt = con.createStatement();

            sql = "INSERT INTO clients ("
                + " `clientID` , `companyName` , `clientFirstName` , `clientLastName` , `contactNumber` , `orderID`"
                + ")"
                + " VALUES ("
                + "NULL, '" + this.clientBusinessName + "', '" + this.clientFirstName + "', '" + this.clientLastName + "', '" + this.clientPhoneNumber + "', '1')";
            
            stmt.executeQuery(sql);

            isCreated = true;            
        }catch(SQLException e){ e.printStackTrace(); }

        return isCreated ;
    }

}

Edited 6 Years Ago by tyson.crouch: n/a

Can not issue data manipulation statements with executeQuery()

Uhm, what do think this might mean?


Look at the API docs for statement/preparedstatement and see if there is another method that is more applicable.

To be completely honest, I have no idea why that error is being thrown back. I've used the executeQuery() statement countless times without fail.

I've gone and looked at a few other samples of methods which may be applicable, though they all use the executeQuery() statement.

Like I said earlier; I have run string outputs to check the SQL syntax once the data is added to the query, and there is absolutely no problem with my SQL syntax. By removing the stmt.executeQuery() i get a positive result so there must be an error with my query execution.

Could anyone please tell my why I might be getting an error with my query execution?

Thanks

Sorry guys, I've managed to fix the problem.

The problem was, that while I was troubleshooting an earlier problem I had a database connect class open from some previous work that I had done, and rather than editing the one relevant to my current program, i was working on the older one which wasn't included in this project.

You cannot do an insert (update or delete either) with executeQuery, you need to use either executeUpdate or execute. If the driver you are using is letting you use executeQuery for a query that actually changes the data then there is a bug in the JDBC driver and you should report that. That is what that exception meant, as it clearly states. Judging by your first response, I assume that you didn't bother to look at the API docs and just got indignant that I said you should. All I can say is to learn to read exceptions and to use the API docs, it will save you alot of time in the future. And, judging by your second post you seemingly, eventually, figured that with the method out but didn't want to admit it.

Edited 6 Years Ago by masijade: n/a

This question has already been answered. Start a new discussion instead.