dljd_007_jdbc编程中的statement执行DML/DDL

一、使用statement的executeUpdate执行DML(增删改)语句示例  

package edu.aeon.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * [说明]:使用statement的executeUpdate执行DML(增删改)
 *     statement的executeQuery用于执行DQL(select ...)语句。
 * @author aeon
 */
public class TestStatment {

    public static void main(String[] args) {
        Connection connection=null;
        Statement statement=null;
        try {
            //2.1加载/注册驱动
            //driver是jdbc声明的标准接口、com.mysql.jdbc.Driver是mysql数据库厂商根据这个标准做的实现、注意这两个Driver是有区别的。
            Driver driver=new com.mysql.jdbc.Driver(); //接口声明引用指向实现类的对象
            DriverManager.registerDriver(driver);
            //2.2获取和数据库服务器的连接(java程序是客户端    mysql是个服务器)
            String username="root"; //用户名
            String password="root";    //密码
            //url中的jdbc:mysql说明:jdbc是由sun公司制定的一套网络协议  jdbc:mysql是指jdbc协议下的mysql子协议。
            String url="jdbc:mysql://localhost:3306/db_test";
            //2.3连接服务器     Connection是jdbc规范中声明的接口
            connection=DriverManager.getConnection(url, username, password);
            //2.4通过连接对象获取执行sql语句的对象
            statement=connection.createStatement();
            String dml_sql="insert into user(userid,username,userpw) values(10006,'aeon','aeon')";
            //2.6输出结果
            int rows=statement.executeUpdate(dml_sql);
            System.out.println(rows+"行数据插入成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(null!=statement){
                try {
                    statement.close();
                } catch (SQLException e) {
                    System.out.println("关闭流失败!--->statement");
                    e.printStackTrace();
                }
            }
            if(null!=connection){
                try {
                    connection.close();
                } catch (SQLException e) {
                    System.out.println("关闭流失败!--->connection");
                    e.printStackTrace();
                }
            }
        }
    }

}

结果截图:

  

  我们再来看看数据库插入前和插入后截图:

    

  注意:使用statement执行DML返回的结果是int类型的、这个int类型的具体值代表你执行后影响数据库的行数。

二、使用statement的executeUpdate执行DDL(创建)语句

  

package edu.aeon.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * [说明]:使用statement的executeUpdate执行DDL(创建)
 *     statement的executeQuery用于执行DQL(select ...)语句。
 * @author aeon
 */
public class TestStatment {

    public static void main(String[] args) {
        Connection connection=null;
        Statement statement=null;
        try {
            //2.1加载/注册驱动
            //driver是jdbc声明的标准接口、com.mysql.jdbc.Driver是mysql数据库厂商根据这个标准做的实现、注意这两个Driver是有区别的。
            Driver driver=new com.mysql.jdbc.Driver(); //接口声明引用指向实现类的对象
            DriverManager.registerDriver(driver);
            //2.2获取和数据库服务器的连接(java程序是客户端    mysql是个服务器)
            String username="root"; //用户名
            String password="root";    //密码
            //url中的jdbc:mysql说明:jdbc是由sun公司制定的一套网络协议  jdbc:mysql是指jdbc协议下的mysql子协议。
            String url="jdbc:mysql://localhost:3306/db_test";
            //2.3连接服务器     Connection是jdbc规范中声明的接口
            connection=DriverManager.getConnection(url, username, password);
            //2.4通过连接对象获取执行sql语句的对象
            statement=connection.createStatement();
            String ddl_sql="create table test(id int primary key,name varchar(10))";//简单的DDL测试语句
            //2.6输出结果
            int result=statement.executeUpdate(ddl_sql);
            System.out.println("返回的结果是:"+result);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(null!=statement){
                try {
                    statement.close();
                } catch (SQLException e) {
                    System.out.println("关闭流失败!--->statement");
                    e.printStackTrace();
                }
            }
            if(null!=connection){
                try {
                    connection.close();
                } catch (SQLException e) {
                    System.out.println("关闭流失败!--->connection");
                    e.printStackTrace();
                }
            }
        }
    }

}

执行结果截图:

  

我们来看一下数据库执行前后的数据:

  

  

猜你喜欢

转载自www.cnblogs.com/aeon/p/10068869.html
今日推荐