jdbc 直接调用DDL

本文展示了使用Java JDBC直接执行DDL语句,如创建、修改和删除分区的示例。测试类包括添加分区、创建和删除表格的测试方法,值得注意的是,DDL操作在MySQL中会自动提交,不支持回滚。
摘要由CSDN通过智能技术生成
package client.frw;

import java.sql.Connection;
import java.sql.Statement;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.Test;

public class MysqlLockTest {

@Test
public void testAddPartition() throws Exception {
Connection conn = MysqlConnectionTest.createConnection();
conn.setAutoCommit(false);

Statement st = conn.createStatement();


String insert = "alter table kaps_zjyils add PARTITION (PARTITION p" + RandomStringUtils.randomNumeric(8) + " values less than('20150424'));";
System.out.println(insert);
st.execute(insert);
conn.rollback();//rollback不起作用
}

@Test
public void testCreateTest2() throws Exception {
Connection conn = MysqlConnectionTest.createConnection();
conn.setAutoCommit(false);

Statement st = conn.createStatement();

String insert = "insert into tb_test1 values('" + RandomStringUtils.randomNumeric(8) + "')";
System.out.println(insert);
st.execute(insert);

//ddl 自动提交
String ddl = "create table t" + RandomStringUtils.randomNumeric(8) + " (name1 varchar (30) null)";
st.execute(ddl);
System.out.println(ddl);

insert = "insert into tb_test1 values('" + RandomStringUtils.randomNumeric(8) + "')";
System.out.println(insert);
st.execute(insert);
conn.rollback();
}

@Test
public void testDropTest2() throws Exception {
Connection conn = MysqlConnectionTest.createConnection();
conn.setAutoCommit(false);
String ddl = "drop table tb_test1;";
System.out.println(ddl);
System.out.println(conn.createStatement().execute(ddl));
// conn.commit();
conn.rollback();
}

@Test
public void testCreateTest1() throws Exception {
Connection conn = MysqlConnectionTest.createConnection();
conn.setAutoCommit(false);
String ddl = "create table tb_test1 (name1 varchar (30) null);";
System.out.println(ddl);
System.out.println(conn.createStatement().execute(ddl));
// conn.commit();
conn.rollback();
}

}

查询分区的方法
SELECT
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
FROM
INFORMATION_SCHEMA.partitions
WHERE
TABLE_SCHEMA = schema()
AND TABLE_NAME='table';


对ddl操作,不支持事物,自动commit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值