executeUpdate() returns zero despite correct execution

前端 未结 3 1151
北恋
北恋 2021-01-25 18:10

create_PaperBean.java

package Beans;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.loggin         


        
相关标签:
3条回答
  • 2021-01-25 18:35

    If we take a look at the Statement.executeUpdate javadoc we see that it always return 0 for DDL statements (in your case create table is a DDL statement):

    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.

    Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

    You can assume that the statement execution is successful if you don't get a SQLException.

    0 讨论(0)
  • 2021-01-25 18:45

    st.executeUpdate() either returns the row count for SQL Data Manipulation Language (DML) statements or0 for SQL statements that return nothing.

    Creating a table is neither an INSERT nor an UPDATE, so it's normal to receive 0 as no row(s) were affected.

    0 讨论(0)
  • 2021-01-25 18:49

    I am bit doubted about your query as well, you might need to look at this

       CREATE TABLE `test1` ( contact_id INT(10),name VARCHAR(40),
       birthdate DATE,PRIMARY KEY    (contact_id));
    

    If the update happens correctly then the value is 1, if not its 0. Moreover, it return 0 when your statement does not affect any row , for example in DDL execution it may return 0.

    0 讨论(0)
提交回复
热议问题