在jsp页面中有个操作,实现的功能是:把数据库里的一个表的所有数据备份成一个文件,也就是打包;还要可以把数据再导入加载到数据库表里.
  各位说说有什么好的方法.我的想法是把所有的记录查出来,然后一条条再写入一个文件.加载就再读取文件里的字段,插入数据库.但是我觉得这个方法效率不高,而且具体代码也不好写.
  各位高手能不能给一些好的方法和代码.

解决方案 »

  1.   

    如果是
    oracle数据库很容易 ,数据导入文件调用spool,文本文件导入用external table
    别的数据库不熟悉,但我想本身也会有一些相应方便功能
      

  2.   

    我做过这方面的东西。我这里用的是把数据全部选出来,写成文件。然后在导入的时候就读这个文件生成sql语句,在插入数据库,当然这个文件格式可以有你定。 public synchronized static void createsysmoreFile(String tablename,
    String filepath,
    String encrypt,
    Connection conn) throws Exception
    {
    log.debug("creatFile(String, Connection) - start");

    PreparedStatement pstamt = null;
    ResultSet rs = null;
    ResultSetMetaData rsmd = null;
    StringBuffer databuffer = new StringBuffer();
    PrintWriter out = null;

    String sql = "select * form ";

    int FILELINE = 1000 //如果记录大于1000就分文件存储;
    File sysflie = new File(filepath + tablename+"_0" + ".txt"); try
    {
    out = new PrintWriter(new FileWriter(sysflie)); pstamt = conn.prepareStatement(sql + tablename);
    log.debug("creatFile(String, Connection) - String sql=" + sql
    + parsername); rs = pstamt.executeQuery(); rsmd = rs.getMetaData(); int totalColumn = rsmd.getColumnCount(); log.debug("creatFile(String, Connection) - int totalColumn=" + totalColumn); // System.out.println("totalColumn=" + totalColumn);
    int j = 0;

    while (rs.next())
    {
    // System.out.println("rs.=\t" + rs.getString(1));
    j = rs.getRow();
    if(j%FILELINE==0)
    {
    int k = j/FILELINE;
    sysflie = new  File(filepath + tablename+"_"+k+ ".txt");
    out = new PrintWriter(new FileWriter(sysflie));
    }
    databuffer.setLength(0);
    for (int i = 1; i <= totalColumn - 1; i++)
    {
    if (rs.getString(i) != null)
    {
    databuffer.append(rs.getString(i).trim());
    databuffer.append("\t");
    }
    else
    {
    databuffer.append("");
    databuffer.append("\t");
    } }
    if (rs.getString(totalColumn) != null)
    {
    databuffer.append(rs.getString(totalColumn).trim());
    }
    else
    {
    databuffer.append("");
    }
    out.println(databuffer.toString());

    }

    out.close(); log.debug("creatFile(String, Connection) - int totalColumn=" + databuffer.toString());
    }
    catch (SQLException e)
    {
    log.error("creatFile(String, Connection)", e);

    throw new SQLException("查询数据出错!");
    }
    catch (Exception e)
    {
    log.error("creatFile(String, Connection)", e);
    throw new Exception ("生成系统文件出错!");
    }
    finally
    { }

    }
      

  3.   

    是oracle数据库,具体怎么调用,还望写一写.
    要求是在通过jsp页面完成这样的操作.
      

  4.   

    不好意思。刚才多删了一句。在“int k = j/FILELINE;”前,if(j%FILELINE==0)之后加一句
    out.close();
      

  5.   

    athlon0807(书尚友)
    -----------------
    这位兄弟再给导入的代码好吗.比如文件里存储的形式是这样的 :
     2   发电   弗里德
     23    分   扶绥大一行是数据库里的一条记录.那怎么读取'2'作为一个字段,然后再读取'发电'作为第二个字段,
    还有怎么判断一行的结束.
    谢谢了.
      

  6.   

    public void readFile(File datafile,String tablename,Connection conn) throws Exception
    {
    InputStreamReader read = null;
    BufferedReader reader = null;

    // PreparedStatement pstamt= null;

    Statement stamt = null;

    ResultSet rs = null;

    // PreparedStatement pstamtfind = null;
    // String sql = null;

    TableTye tty = null;

    String findsql = null;

    String updatasql = null;

    String insertsql = null;

    int total = 0; try
    {
    /**
     * 判断条件存在不;
     */
    findsql = "select count(*) from "+tablename+" where ";
    log.debug("readFile(File, String, Connection) - String findsql=" + findsql);

    /**
     * 更新存在的数据;
     */
    updatasql ="update "+tablename+" set ";

    insertsql = "insert into "+tablename+" values(";

    //tty = new TableTye();
    read = new InputStreamReader(new FileInputStream(datafile),
    "GB2312");
    reader = new BufferedReader(read); stamt = conn.createStatement(); String line; StringBuffer wheresql = new StringBuffer();

    StringBuffer datasql = new StringBuffer();

    StringBuffer insersql = new StringBuffer();

    int count = 0; while ((line = reader.readLine()) != null)
    {

    String[] values = line.split("\t", 3);//有几个字段就用是几,这样在处理是不会出现null错误.

    //这里就是你要构造的sql语句.
        //tty.getFindSql(line,wheresql,datasql,insersql);
             rs = stamt.executeQuery(findsql+wheresql.toString());
        if(rs.next())
        {
         total = rs.getInt(1);
        }
        
        if(total==0)
        {
    //      tty.getXmlFile(line,pstamt);
         log.debug("insert sql:="+insertsql+insersql.toString()+")");
         stamt.executeUpdate(insertsql+insersql.toString()+")");
    //      pstamt.executeUpdate();
        }
        else
        {
    //      log.debug("aaaa:==="+updatasql+datasql.toString()+ "where "+wheresql.toString());
         stamt.executeUpdate(updatasql+datasql.toString()+ "where "+wheresql.toString());
        }
        count++; }
    message.append("文件名"+datafile.getName()+"文件数据导入成功!,成功导入:"+count+"条记录");
    message.append("\n");
    conn.commit();
    } catch (Exception e)
    {
    try
    {
    conn.rollback();
    }
    catch (SQLException e1)
    {
    // TODO 自动生成 catch 块
    e1.printStackTrace();
    }
    e.printStackTrace();
    message.append("文件名"+datafile.getName()+"文件数据导入失败!");
    message.append("\n");
    throw new Exception ("导入文件失败");
    }
    finally
    {
     read = null;
     reader = null;

    // PreparedStatement pstamt= null;

     stamt = null;

     rs = null;

    // PreparedStatement pstamtfind = null;
    //  sql = null;
     tty = null;

     findsql = null;

     updatasql = null;

     insertsql = null;
    }
    }
      

  7.   

    Oracle不是有导入导出的命令么?可以用Runtime.getRuntime().exec(command)
      

  8.   

    其实那个分割符是由你自己定的.你也可以定成其他特殊的字符,不过不要弄和某些字段里的值出现的符号一样就是,哪样分割数据会出错的.还有就是你的字段的类型你要清楚,比如是char的有些数据库需要加"'"的而int的是不用加的"'";wheresql = 主键的值比如(ID=value[0] and ClassID=value[2]);等
    insersql  = (value[0],value[1],value[2]);
    datasql  = name='value[1]';等这只是个例子.