DDL创建删除表 PreparedStatement

package sqExe;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.java.myLib.CommDb;



public class ExeDDLP {
private static PreparedStatement pstmt = null;
private String sql = null;
private CommDb commDb = null;
private Connection con = null;

/**
* 构造方法:
*/
public ExeDDLP(String sql,CommDb commDb) {
this.sql = sql;
this.commDb = commDb;
//创建连接
this.con = commDb.getConnection();
try {
//创建statement对象
this.pstmt = con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("sqExe.ExeDDLP.ExeDDLP() "+e);
}
}

/**
* 功能:创建表功能
*
*/
public void createTable()
{


try {
//执行sql语句成功返回0
int returnvalue = pstmt.executeUpdate();
if(returnvalue == 0)
System.out.println("创建表成功");
else
System.out.println("创建表失败");
} catch (SQLException e) {
System.out.println("sqExe.ExeDDLP.createTable() "+e);
}
finally{
commDb.closeStatement(pstmt);
commDb.closeConnection(con);
}
}

/**
* 功能:删除表
*/
public void dropTable()
{
try {
//执行sql语句成功返回0
int returnvalue = pstmt.executeUpdate();
if(returnvalue == 0)
System.out.println("删除表成功");
else
System.out.println("删除表失败");
} catch (SQLException e) {
System.out.println("sqExe.ExeDDLP.dropTable() "+e);
}
finally{
commDb.closePreparedStatement(pstmt);
commDb.closeConnection(con);
}
}
public static void main(String[] args) {
//获取连接驱动
CommDb commDb =
new CommDb("jdbc:oracle:thin:@127.0.0.1:1521:java0218",
"bihai", "bihai");
/*
* new exeDDL("create table test01 (id number(4),name varchar2(20))"," +
* "commDb).createTable();
*/ new ExeDDLP("drop table test01",commDb).dropTable();

}

}

你可能感兴趣的:(java,oracle,sql,jdbc)