Cannot insert into table using JDBC

The following query was successful when I used in mysql

INSERT INTO user(`dev_id`,`email`) VALUES('123','456@gmail.com');

      

But in java jdbc I got this exception:

Can not issue data manipulation statements with executeQuery().
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.Statement.checkForDml(Statement.java:417)
    at com.mysql.jdbc.Statement.executeQuery(Statement.java:1140)

      

My table has 5 columns and 3 columns has default = null;

+3


source to share


4 answers


executeQuery

is only for making inquiries. You should use executeUpdate

for insertion, which is for operators (eg INSERT

) that modify data. Ideally, you should also usePreparedStatement



+5


source


executeQuery()

Try using execute()

or insteadexecuteUpdate()



+3


source


Call executeUpdate()

if u produces statements INSERT/UPDATE/DELETE

, not those executeQuery()

that are intended to execute SELECT statements

+1


source


You should use executeUpdate () instead of executeQuery () . JavaDoc:

Executes an SQL job, which can be an INSERT, UPDATE, or DELETE, or an SQL statement that returns nothing, such as SQL DDL.

Note. This method cannot be called on PreparedStatement or CallableStatement.

Parameters: sql - SQL Data Manipulation Language (DML) statement such as INSERT, UPDATE, or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Also, whenever a problem occurs - check the javadoc first and try googling. Stackoverflow is a bit delicate for your case.

+1


source







All Articles