Why is the return value of the table created by execute in Mysql false?

Why is the return value false after execute creates the table in the SQL statement?

First look at the problems encountered:
execute the sql statement, create the table, and run without error, indicating that our code is no problem, and in the database, the student table has also been created successfully, which further shows that our code is no problem. But the console output is false, not true. I
Insert picture description here
don't know the reason, so we need to look at the source code of execute.
Press and hold Ctrl, and then click execute to
Insert picture description here
see here, we found that this is an excuse, and we can’t see anything.
Therefore, we should think about whether there is any indication
in his implementation class that we are pressing Ctrl+H and find His implementation class
Insert picture description here
As a result, we found that there is still no explanation here, but here, we have no thoughts, and we have to go to his explanation document.
Insert picture description here
We can find this sentence, which probably means:

 execute 方法返回一个 boolean 值,并没有返回结果,所以返回的是false,如果返回有结果则输出true。再联系我们创建表,我们只是在数据库中完成了表的创建,没有返回结果,所以为false。
 到这里我们也终于搞懂了,着实有点不容易呀!

Here is my code, if you want to try it, you can study it!

private Connection con;
    @Before //每次单元测试之前都会执行
    public void init() throws Exception {
    
    
        //2,注册驱动  Driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        //3,DriverManager 获取连接数据库对象
        String url="jdbc:mysql://localhost:3306/long ?useSSL=false&serverTimezone=UTC";
        con = DriverManager.getConnection(url, "root", "123456");
    }
    @Test   // dml--新增
    public void test01() throws SQLException {
    
    
        //1,定义sql
        String sql="insert into account values(NULL,'关羽',1000)";
        //2,获取执行sql的对象,Statement
        Statement statement = con.createStatement();
        //3,执行sql,并返回表格影响的行数
        int i = statement.executeUpdate(sql);
        System.out.println(i);
    }

    @Test   // ddl--创建表
    public void test02() throws SQLException {
    
    
        //1,定义sql
        String sql="CREATE TABLE student(id INT,NAME VARCHAR(20));";
        //2,获取执行sql的对象,Statement
        Statement statement = con.createStatement();
        //3,执行sql,并返回表格影响的行数
        boolean i = statement.execute(sql);
        System.out.println(i);
    }

The creation code of the account table:

-- 创建账户表
CREATE TABLE account(
	id INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20),
    balance DOUBLE 
);
INSERT INTO account VALUES (NULL,'曹操',1000), (NULL,'刘备',1000);

Guess you like

Origin blog.csdn.net/weixin_44889894/article/details/112321603