Oracle全库快速导出工具 一键搞定!
Oracle是企业级关系型数据库系统的代表,广泛应用于各种领域。但有时在备份和迁移数据库时,需要将整个数据库导出成一个SQL文件。然而,Oracle提供的exp/expdp工具需要耗费大量的时间和资源,尤其是在大型数据库中。因此,我们需要一个更高效的工具来完成这项任务。
现在,我们推出一款全新的Oracle全库快速导出工具,只需简单的一键操作,即可快速导出整个数据库,并生成一个完整的SQL文件。该工具基于Java开发,可以跨平台运行,支持windows和Linux操作系统,而且不需要安装Oracle客户端或其他任何软件。
如何使用该工具?
在运行该工具之前,需要先在Oracle数据库中创建一个普通用户,该用户需要有DBA权限。
接着,下载该工具,并将其解压到任意目录下。
打开配置文件config.properties,根据实际情况修改其中的参数,例如:
“`sql
# 数据库连接配置
# 数据库所在主机IP
db.host=192.168.0.1
# 数据库端口号
db.port=1521
# 数据库实例名
db.instance=DB01
# 数据库用户名
db.user=testuser
# 数据库密码
db.password=testuser
# 导出选项配置
# 导出文件路径
export.filepath=D:\oracle_backup
# 导出文件名
export.filename=oracle_backup.sql
# 是否导出存储过程和函数
export.procedure=true
# 是否导出触发器
export.trigger=true
# 是否导出索引
export.index=true
其中,db.host是数据库所在主机IP,db.port是数据库的端口号,db.instance是数据库实例名,db.user是普通用户的用户名,db.password是普通用户的密码。
export.filepath是导出文件的路径,export.filename是导出文件的名称,export.procedure表示是否导出存储过程和函数,export.trigger表示是否导出触发器,export.index表示是否导出索引。可以根据需求修改这些参数。
然后,在命令行中进入工具所在的目录,输入以下命令:
```shell
java -jar oracle_export_tool.jar
即可开始导出。导出进度会显示在命令行窗口中,导出成功后会生成一个完整的SQL文件,包含整个数据库的结构和数据。
如果需要导出多个数据库,可以将上述过程重复多次,只需要修改config.properties中的数据库连接信息和导出文件的名称即可。
该工具还支持导出压缩文件,只需要在配置文件中设置export.compress=true即可。
下面是该工具的部分源代码:
“`java
public class OracleExportTool {
static String dbHost = “”;
static int dbPort = 0;
static String dbInstance = “”;
static String dbUser = “”;
static String dbPassword = “”;
static String filePath = “”;
static String fileName = “”;
static boolean exportProcedure = false;
static boolean exportTrigger = false;
static boolean exportIndex = false;
static boolean compress = false;
private static void readConfig() throws Exception {
Properties props = new Properties();
FileInputStream fis = new FileInputStream(“config.properties”);
props.load(fis);
dbHost = props.getProperty(“db.host”);
dbPort = Integer.parseInt(props.getProperty(“db.port”));
dbInstance = props.getProperty(“db.instance”);
dbUser = props.getProperty(“db.user”);
dbPassword = props.getProperty(“db.password”);
filePath = props.getProperty(“export.filepath”);
fileName = props.getProperty(“export.filename”);
exportProcedure = Boolean.parseBoolean(props.getProperty(“export.procedure”));
exportTrigger = Boolean.parseBoolean(props.getProperty(“export.trigger”));
exportIndex = Boolean.parseBoolean(props.getProperty(“export.index”));
compress = Boolean.parseBoolean(props.getProperty(“export.compress”));
fis.close();
}
public static void mn(String[] args) {
Connection conn = null;
PrintWriter writer = null;
ZipOutputStream zos = null;
try {
readConfig();
Class.forName(“oracle.jdbc.driver.OracleDriver”);
String url = String.format(“jdbc:oracle:thin:@%s:%d:%s”, dbHost, dbPort, dbInstance);
conn = DriverManager.getConnection(url, dbUser, dbPassword);
DatabaseMetaData meta = conn.getMetaData();
String[] types = { “TABLE” };
ResultSet tables = meta.getTables(null, null, null, types);
writer = new PrintWriter(new FileWriter(filePath + “/” + fileName), true);
zos = new ZipOutputStream(new FileOutputStream(filePath + “/” + fileName + “.zip”));
while (tables.next()) {
String tableName = tables.getString(“TABLE_NAME”);
String tableComment = tables.getString(“REMARKS”);
writer.println(“– CREATE TABLE ” + tableName + ” (” + tableComment + “)”);
writer.println(“CREATE TABLE \”” + tableName + “\” (“);
ResultSet columns = meta.getColumns(null, null, tableName, null);
while (columns.next()) {
String columnName = columns.getString(“COLUMN_NAME”);
String typeName = columns.getString(“TYPE_NAME”);
int columnSize = columns.getInt(“COLUMN_SIZE”);
int decimalDigits = columns.getInt(“DECIMAL_DIGITS”);
int nullAble = columns.getInt(“NULLABLE”);
String columnComment = columns.getString(“REMARKS”);
String nullable = nullAble == 1 ? “NULL” : “NOT NULL”;
String columnDef = columns.getString(“COLUMN_DEF”);
if (columnDef != null) {
writer.println(“\t” + columnName + ” ” + typeName + “(” + columnSize + “,” + decimalDigits
+ “)” + ” DEFAULT ” + columnDef + ” ” + nullable + “,”);
} else {
writer.println(“\t” + columnName + ” ” + typeName + “(” + columnSize + “,” + decimalDigits
+ “)” + ” ” + nullable + “,”);
}
}
columns.close();
ResultSet pk = meta.getPrimaryKeys(null, null, tableName);
StringBuilder sbPK = new StringBuilder();
while (pk.next()) {
sbPK.append(pk.getString(“COLUMN_NAME”)).append(“,”);
}
pk.close();
if (sbPK.length() > 0) {
sbPK.delete(sbPK.length() – 1, sbPK.length());
writer.println(“\t” + “CONSTRNT ” + tableName + “_PK PRIMARY KEY (” + sbPK.toString() + “)”);
}
writer.println(“);”);
writer.println();
if (exportIndex) {
ResultSet indexRs = meta.getIndexInfo(null, null, tableName, false, true);
while (indexRs.next()) {
String indexName = indexRs.getString(“INDEX_NAME”);
String columnName = indexRs.getString(“COLUMN_NAME”);
boolean unique = !indexRs.getBoolean(“NON_UNIQUE”);
if (unique) {
writer.println(“CREATE UNIQUE INDEX \”” + indexName + “\” ON \”” + tableName + “\” (\””
+ columnName + “\”);”);
} else {
writer.println(“CREATE INDEX \”” + indexName + “\” ON \”” + tableName + “\” (\””
+ columnName + “\”);”);
}
}
indexRs.close();
}
if (exportTrigger) {
ResultSet triggerRs = meta.getTriggers(null, null, tableName);
while (triggerRs.next()) {
String triggerName = triggerRs.getString(“TRIGGER_NAME”);
String triggerEvent = triggerRs.getString(“TRIGGERING_EVENT”);
String triggerBody = triggerRs.getString(“TRIGGER_BODY”);
writer.println(“– CREATE ” + triggerEvent + ” TRIGGER ” + triggerName);
writer.println(triggerBody);
writer.println(“/”);
}
triggerRs.close();
}
if (exportProcedure) {
ResultSet procRs = meta.getProcedures(null, null, tableName);
while (procRs.next()) {
String procName = procRs.getString(“PROCEDURE_NAME”);
String procBody = procRs.getString(“PROCEDURE_BODY”);
writer.println(“– CREATE PROCEDURE ” + procName);
writer.println(procBody);
writer.println(“/”);
}
procRs.close();
}
ResultSet dataRs = conn.createStatement().executeQuery(“SELECT * FROM ” + tableName);
ResultSetMetaData rsmd = dataRs.getMetaData();
while (dataRs.next()) {
writer.print(“INSERT INTO \”” + tableName + “\” VALUES (“);
for (int i = 1; i
int columnType = rsmd.getColumnType(i);
String data = “”;
switch (columnType) {
case Types.INTEGER:
case Types.TINYINT:
case Types.SMALLINT:
case Types.BIGINT:
data = String.valueOf(dataRs.getLong(i));
break;
case Types.FLOAT:
case Types.REAL:
case Types.DOUBLE:
data = String.valueOf(dataRs.getDouble(i));
break;
case Types.NUMERIC:
case Types.DECIMAL:
data = String.valueOf(dataRs.get