# executeUpdate(): Example

__*executeUpdate*__

```
import java.sql.Connection;
import java.sql.DriverManager;
import ~!@java.sql.Statement@!~;
public class DemoJdbcExecuteUpdate{
    public static void main(String[] args){
        Connection connection = null;
        try{
            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:4000/test", "root", ""
            );
            Statement statement = ~!@connection.createStatement()@!~;
            statement.~!@executeUpdate@!~("DROP TABLE IF EXISTS t1");
            statement.~!@executeUpdate@!~("CREATE TABLE t1 (id int PRIMARY KEY, name char(4))");
            int rowCount = 0;
            rowCount = statement.~!@executeUpdate@!~("INSERT INTO t1 VALUES(1, 'ABCD')");
            rowCount = statement.~!@executeUpdate@!~("INSERT INTO t1 VALUES(2, 'EFGH')");
            rowCount = statement.~!@executeUpdate@!~("INSERT INTO t1 VALUES(3, 'IJKL')");
            statement.close();
        }
```
----------------------------------------------------------------------------------------
幻灯片中的代码示例创建并填充名为 t1 的表。

你可以看出程序关闭了 Statement 对象。最好在使用 Statement 和 ResultSet 对象后将其关闭。虽然关闭 Connection 对象和垃圾回收通常会清除未使用的对象,但是当您使用连接池并执行 connection.close () 时,Connection 对象会返回到池中,但子对象可能会保持打开状态,并可能导致内存泄漏。


演示脚本:

对于 macOS 和 Linux:https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/09-demo-jdbc-execute-update-01-show.sh

对于 Windows:https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/09-demo-jdbc-execute-update-01-show.bat

```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DemoJdbcExecuteUpdate{
    public static void main(String[] args){
        Connection connection = null;
        try{
            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:4000/test", "root", ""
            );
            Statement statement = connection.createStatement();
            statement.executeUpdate("DROP TABLE IF EXISTS t1");
            statement.executeUpdate("CREATE TABLE t1 (id int PRIMARY KEY, name char(4))");
            int rowCount = 0;
            rowCount = statement.executeUpdate("INSERT INTO t1 VALUES(1, 'ABCD')");
            rowCount = statement.executeUpdate("INSERT INTO t1 VALUES(2, 'EFGH')");
            rowCount = statement.executeUpdate("INSERT INTO t1 VALUES(3, 'IJKL')");
            statement.close();
        }
```
