Example #1
0
  public boolean loadBaiduData(String file) {

    try {

      br =
          new BufferedReader(
              new InputStreamReader(
                  new FileInputStream(new File("D:" + File.separator + "newsbaidu.txt"))));
      String line = "";
      String sql = "insert into yqjk_report (reportId,title,url,resource,roleId)values(?,?,?,?,?)";
      try {
        con.setAutoCommit(false);
        ps = (PreparedStatement) con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

        int n = 0, index = 1, road = 1;
        try {
          while ((line = br.readLine()) != null) {
            int reportId = index++;
            String title = line.substring(1, line.indexOf("~") + 1).replace("~", "");
            String url =
                line.substring(line.indexOf("~") + 1, line.indexOf("#") + 1)
                    .replace("#", "")
                    .replace("\\", "");
            ;
            String resource =
                line.substring(line.indexOf("#") + 1, line.indexOf("$") + 1).replace("$", "");
            int roleId = road++;
            ps.setInt(1, reportId);
            ps.setString(2, title);
            ps.setString(3, url);
            ps.setString(4, resource);
            ps.setInt(5, roleId);
            ps.addBatch();
            n++;
            if (n > 500) {
              ps.executeBatch();
              n = 0;
            }
            ps.executeBatch();
            con.commit();
          }

        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } finally {
          if (null != rs) {
            rs.close();
          }
          if (null != ps) {
            ps.close();
          }
          if (null != ps2) {
            ps2.close();
          }
          if (null != ps1) {
            ps1.close();
          }
        }
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return true;
  }