创新互联OceanBase教程:OceanBaseConnector/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();
}

本文名称:创新互联OceanBase教程:OceanBaseConnector/J更改数据库
本文地址:http://www.csdahua.cn/qtweb/news11/457711.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网