Java 尽管正确执行,executeUpdate() 返回零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18032075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:54:50  来源:igfitidea点击:

executeUpdate() returns zero despite correct execution

javajdbc

提问by Maverick

create_PaperBean.java

create_PaperBean.java

package Beans;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class create_paperBean extends org.apache.struts.action.ActionForm {

    private String table_name;

    public String getTable_name() {
        return table_name;
    }

    public void setTable_name(String table_name) {
        this.table_name = table_name;
    }
    public String createDB(){
        String str = "creation_failed";
        try{   
            Statement st = DBConnection.DBConnection.DBConnect();
            int insert = st.executeUpdate("CREATE TABLE "+table_name+"(ques_no int(11) NOT NULL PRIMARy KEY,"
                    + "ques_name varchar(45),ans1 varchar(45),ans2 varchar(45),ans3 varchar(45),"
                    + "ans4 varchar(45),correct_ans varchar(45))");            
            System.out.println(insert);
            if(insert > 0)
                str = "created";            
        } catch (SQLException ex) {
            Logger.getLogger(create_paperBean.class.getName()).log(Level.SEVERE, null, ex);
        }  
        return str;
    }
}

I'm using struts and when this bean is executed a table is created in the database, but the value of insert is still zero! Therefore I can't proceed to the correct page for further functioning of the web app. What am I doing wrong?

我正在使用 struts,当这个 bean 被执行时,在数据库中创建了一个表,但插入的值仍然为零!因此,我无法进入正确的页面以进一步运行 Web 应用程序。我究竟做错了什么?

采纳答案by Azad

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

st.executeUpdate()要么返回 SQL 数据操作语言 (DML) 语句的行数,要么返回0不返回任何内容的SQL 语句的行数。

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

创建表既不是插入也不是更新,因此接收0是正常的,因为没有行受到影响。

回答by dcernahoschi

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

如果我们查看Statement.executeUpdatejavadoc,我们会发现它始终为 DDL 语句返回 0(在您的情况下 create table 是 DDL 语句):

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

执行给定的 SQL 语句,它可以是 INSERT、UPDATE 或 DELETE 语句,也可以是不返回任何内容的 SQL 语句,例如 SQL DDL 语句。

返回: (1) SQL 数据操作语言 (DML) 语句的行数或 (2) 0 不返回任何内容的 SQL 语句

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

如果没有得到SQLException.

回答by Areeb Gillani

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.

如果更新正确发生,则值为 1,否则为 0。此外,当您的语句不影响任何行时,它返回 0,例如在 DDL 执行中,它可能返回 0。