OceanBase Connector/J 更改数据库

OceanBase Connector/J 支持 DML 操作和 DDL 操作来更改数据库。

DML 操作

要执行数据操作语言(DML)的 INSERT 或 UPDATE 操作,可以创建一个 Statement 对象或 PreparedStatement 对象。可以使用 PreparedStatement 对象运行带有输入参数集的语句。Connection 对象的 prepareStatement 方法可以定义一条语句并采用变量绑定参数,返回带有语句定义的 PreparedStatement 对象。

PreparedStatement 对象使用 setXXX 方法将数据绑定到准备发送到数据库的语句。

示例:使用 PreparedStatement 执行 INSERT 操作将两行数据添加到 customers 表中。

PreparedStatement ps = null;
try{
    ps = conn.prepareStatement ("insert into customers (customerID, name) values (?, ?)");

    ps.setInt (1, 150);              // 第一个? 对应 customerID
    ps.setString (2, "Adam");   // 第二个? 对应 name

    ps.execute ();

    ps.setInt (1, 207);           
    ps.setString (2, "Alice");   
    ps.execute ();
}

finally{
     if(ps!=null)
     ps.close();
}

DDL 操作

要执行数据定义语言(DDL)操作,可以创建一个 Statement 对象或 PreparedStatement 对象。

示例:使用 Statement 对象在数据库中创建表。

//创建表 customers 以及 customerID 和 name 列
String query;
Statement st=null;

try{
    query="create table customers " +
          "(customerID int, " +
          "name varchar(50))";
    st = conn.createStatement();
    st.executeUpdate(query);
    }
finally{
     //关闭 Statement
     st.close();
    }

如果涉及重新执行 DDL 操作,那么在重新执行该语句之前,必须重新进行准备。

示例:在重新执行之前准备 DDL 语句。

PreparedStatement ps = null;
PreparedStatement ts = null;
try{
    ps = conn.prepareStatement ("insert into customers(customerID, name) values (?, ?)");
 
    // 添加新顾客 Adam,编号150
    ps.setInt (1, 150);          // 第一个? 对应 customerID
    ps.setString (2, "Adam");   // 第二个? 对应 name
    // 插入数据
    ps.execute ();
    
    ts = conn.prepareStatement("truncate table customers"); 
    ts.executeUpdate();
 
 
    // 添加新顾客 Alice,编号 207
    ps.setInt (1, 207);           // 第一个? 对应 customerID
    ps.setString (2, "Alice");   // 第二个? 对应 name
    // 插入数据
    ps.execute ();
 
    ts.close();
    ts = conn.prepareStatement("truncate table customers"); 
    ts.executeUpdate();
    }
finally{
     if(ps!=null)
     // 关闭 Statement
     ps.close();
}
andy

前端小白,在Web176教程网这个平台跟大家一起学习,加油!

Share
Published by
andy
Tags: OceanBase

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

7 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago