DAY 发表于 2019-8-31 12:43:44

[JDBC]通过Statement执行更新操作

本帖最后由 DAY 于 2019-8-31 13:51 编辑


http://pic1.win4000.com/pic/f/96/da2aad0f03.jpg

/*
通过JDBC向指定的数据表中插入一条记录

1.Statement:用于执行sql语句的对象
1.1通过Connection的createStatement()方法获取
1.2通过executeUIpdate(sql)可以执行sql语句。
1.3传入的sql可以是INSERT,UPDATE或DELETE但不能是SELECT

2.Connection、Satement都是应用程序数据库服务器的连接资源,使用后一定要关闭。
需要finallly中关闭Connection和Statement对象

3.关闭的顺序是:先关闭后获取的,即关闭Statement后关闭Connection
**/
public void testStatement(){
    //获取数据库连接
    Connection conn = DriverManager.getConnection(jdbcUrl,user,password);
    //准备插入的sql语句
    String sql = "insert into customers (name,email,birth)"+
            "values('xyz','zyx@123.com','1990-1-2')";
    //执行插入
    Statement statement = conn.createStatement();
    statement.executeUpdate(sql);
    statement.close();
    //关闭连接
    conn.close();
}
页: [1]
查看完整版本: [JDBC]通过Statement执行更新操作