使用java连接数据库按需生成oracle卸数装数的control、selectSQL、建表ddl语句等文件
写这个主要是当时有需要从另外一个oracle数据库环境卸一部分的表的数据到另外一个oracle数据库进行装数,因为懒得一个个的写control文件,没办法
简单的具体思路
一、首先创建数据库连接工具类
package cn.com.wjq.connect;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnectionUtil {
private static String driver;
private static String username;
private static String pwd;
private static String url;
static {
try {
ClassLoader classLoader = DBConnectionUtil.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("config/props/db.properties");
Properties properties=new Properties();
properties.load(resourceAsStream);
url = properties.getProperty("url");
username = properties.getProperty("user");
pwd = properties.getProperty("password");
driver = properties.getProperty("driver");
Class.forName(driver);
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection conn = (Connection) DriverManager.getConnection(url, username, pwd);
if (conn == null) {
System.out.println("Failed to connect database...");
} else {
System.out.println("database connected successful...");
}
return conn;
}
public static void release(ResultSet rs, PreparedStatement sta, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("Resource release successful...");
}
public static void release(PreparedStatement sta, Connection conn) {
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("Resource release successful...");
}
}
二、创建对象bean类
类一
package cn.com.wjq.bean;
public class DBA_TAB_COLS {
//OWNER,TABLE_NAME,COLUMN_NAME,DATA_TYPE,DATA_LENGTH,COLUMN_ID
public String OWNER ;
public String TABLE_NAME ;
public String COLUMN_NAME ;
public String DATA_TYPE ;
public String DATA_LENGTH ;
public String COLUMN_ID ;
public String Comment ;
public String DATA_PRECISION ;
public String DATA_SCALE ;
public String getDATA_PRECISION() {
return DATA_PRECISION;
}
public void setDATA_PRECISION(String dATA_PRECISION) {
DATA_PRECISION = dATA_PRECISION;
}
public String getDATA_SCALE() {
return DATA_SCALE;
}
public void setDATA_SCALE(String dATA_SCALE) {
DATA_SCALE = dATA_SCALE;
}
public String getComment() {
return Comment;
}
public void setComment(String comment) {
Comment = comment;
}
public String getOWNER() {
return OWNER;
}
public void setOWNER(String oWNER) {
OWNER = oWNER;
}
public String getTABLE_NAME() {
return TABLE_NAME;
}
public void setTABLE_NAME(String tABLE_NAME) {
TABLE_NAME = tABLE_NAME;
}
public String getCOLUMN_NAME() {
return COLUMN_NAME;
}
public void setCOLUMN_NAME(String cOLUMN_NAME) {
COLUMN_NAME = cOLUMN_NAME;
}
public String getDATA_TYPE() {
return DATA_TYPE;
}
public void setDATA_TYPE(String dATA_TYPE) {
DATA_TYPE = dATA_TYPE;
}
public String getDATA_LENGTH() {
return DATA_LENGTH;
}
public void setDATA_LENGTH(String dATA_LENGTH) {
DATA_LENGTH = dATA_LENGTH;
}
public String getCOLUMN_ID() {
return COLUMN_ID;
}
public void setCOLUMN_ID(String cOLUMN_ID) {
COLUMN_ID = cOLUMN_ID;
}
}
类二
package cn.com.wjq.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class TableClass {
private String table_name;
private String comment;
private String owner;
private String charset;
private Set<String> primaryKey;
private Set<String> uniqueKey;
private List<DBA_TAB_COLS> columns = new ArrayList<>();
public String getTable_name() {
return table_name;
}
public void setTable_name(String table_name) {
this.table_name = table_name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getowner() {
return owner;
}
public void setowner(String owner)