简体   繁体   English

无法使用JDBC插入表中

[英]Can not insert into a table using JDBC

The following query was successful when I used in mysql 当我在mysql中使用时,以下查询成功

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

But in java jdbc I got this exception: 但是在java jdbc中,我得到了这个异常:

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; 我的表有5列,而3列的默认值为= null;

executeQuery is only for issuing query statements. executeQuery仅用于发布查询语句。 You need to be using executeUpdate for an insert, which is for statements (like INSERT ) that modify data. 您需要将executeUpdate用于插入,该插入用于修改数据的语句(如INSERT )。 Ideally, you should also be using a PreparedStatement 理想情况下,您还应该使用PreparedStatement

而不是executeQuery()尝试使用execute()executeUpdate()

如果u正在发出INSERT/UPDATE/DELETE语句,而不是用于执行SELECT语句的executeQuery() ,则调用executeUpdate()

You should use executeUpdate() instead of executeQuery() . 您应该使用executeUpdate()而不是executeQuery() JavaDoc: JavaDoc:

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. 执行给定的SQL语句,该语句可以是INSERT,UPDATE或DELETE语句,也可以是不返回任何内容的SQL语句,例如SQL DDL语句。

Note:This method cannot be called on a PreparedStatement or CallableStatement. 注意:不能在PreparedStatement或CallableStatement上调用此方法。

Parameters: sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; 参数:sql-SQL数据操作语言(DML)语句,例如INSERT,UPDATE或DELETE; or an SQL statement that returns nothing, such as a DDL statement. 或不返回任何内容的SQL语句,例如DDL语句。

Also whenever problem arise - check javadoc and try googling first. 同样,只要出现问题,请检查javadoc并首先尝试使用Google谷歌搜索。 Stackoverflow is a little bit delicate for your case. 对于您的情况,Stackoverflow有点微妙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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