Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not insert into a table using JDBC

Tags:

java

mysql

jdbc

The following query was successful when I used in mysql

INSERT INTO user(`dev_id`,`email`) VALUES('123','[email protected]');

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 have 5 columns and 3 columns have default value =null;

like image 405
user1235872 Avatar asked May 09 '26 08:05

user1235872


2 Answers

executeQuery is only for issuing query statements. You need to be using executeUpdate for an insert, which is for statements (like INSERT) that modify data. Ideally, you should also be using a PreparedStatement

like image 137
Jon Bright Avatar answered May 11 '26 21:05

Jon Bright


Instead of executeQuery() try to use execute() or executeUpdate()

like image 25
Michał Niklas Avatar answered May 11 '26 20:05

Michał Niklas