简体   繁体   English

从oracle导出表数据

[英]export table data from oracle

I need to create a flat file in java. 我需要在Java中创建一个平面文件。 In this flat file i write the insert statement where data comes from oracle table. 在这个平面文件中,我编写了insert语句,其中数据来自oracle表。 Following is my java code 以下是我的java代码

stmtSQL="SELECT * FROM Table1";
resultSQL=null;
resultSQL=myCreateStmt.executeQuery(stmtSQL);
strSQLIns="";
while(resultSQL.next())
{
strSQLIns=strSQLIns+"INSERT INTO Table1 VALUES ('";
for(int iLoopCtr=0;iLoopCtr<27;iLoopCtr++)
{
    if(iLoopCtr==26)
        strSQLIns=strSQLIns+resultSQL.getString(iLoopCtr+1)+"'";
    else
        strSQLIns=strSQLIns+resultSQL.getString(iLoopCtr+1)+"','";
}
strSQLIns=strSQLIns+");\n";
}

when I run this code system generates the insert statement like this 当我运行此代码系统时,会生成如下插入语句

INSERT INTO Table1 VALUES ('80896948       ','gMWcjDMPXox4LVyrXGw0Ok+4SQI=');
INSERT INTO Table1 VALUES ('39454249       ','z+5fkcA+7wrZrF2455kngiTdYU4=');
INSERT INTO Table1 VALUES ('52167960       ','+N55nru+ewwhKPJygW99T8yEQXg=');

Now I do not want to use to while loop so code generate the above insert statement. 现在,我不想使用while循环,因此代码可以生成上面的insert语句。 I want some quick way so i generate such formatted sql query. 我想要一些快速的方法,以便生成此类格式化的sql查询。

INSERT INTO Table1 (c1,c2) (SELECT '1', 'first row' FROM dual  UNION SELECT '2', 'second    row' FROM dual);

If you are in a production environment: the quickest way to do what you need is to use the import/export tools Oracle gives you 如果您在生产环境中:所需的最快方法是使用Oracle为您提供的导入/导出工具

http://docs.oracle.com/cd/B10501_01/server.920/a96652/ch01.htm http://docs.oracle.com/cd/B10501_01/server.920/a96652/ch01.htm

If you have an academic problem or are doing this for the sake of it: 如果您有学术上的问题或正在为此而做:

Your program is a nice point to start with. 您的程序是一个不错的起点。 I would read the ResultSetMetadata , so you can drop that magic '26' from your code. 我将阅读ResultSetMetadata ,因此您可以从代码中删除该魔术“ 26”。 Just store it all in a List<String[]> and then build your INSERT-SELECT command by iterating on that list. 只需将所有内容存储在List<String[]> ,然后通过对该列表进行迭代来构建INSERT-SELECT命令。 Beware that the example is over-simplified, and you will run with some problems: 注意该示例过于简化,您将遇到一些问题:

  • With some data types as dates and numbers, you will have to be extra-careful to format them as correct strings 对于某些数据类型(例如日期和数字),您必须格外小心,以将其格式化为正确的字符串
  • With some data types as BLOB, your idea is not likely to work 对于某些数据类型(如BLOB),您的想法可能行不通

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM