先简述一下功能,b/s实现短信群发,(jsp+javabean)要用txt格式批量导入用户的手机号码(在txt中仅号码一列)。要保证插入的手机号是唯一的。(因为不能给一个用户发两条同样的短信吧^_^,人家会告你骚扰的),插入用到了序列,数据库是Oracle9i一般的做法是:方法一1、先上传txt到服务器中
2、按行读取文件,校验这个手机号码的合法性——包括长度,是否为数字,是否是以133等开头的等等
3、如果上面读取的这一行合法的话,插入数据库。每插入一条数据,就要用"select * from user where usernum = '13232232222'"检验这个数据是否在数据库中有重复。感觉上面的做法太慢了,插入1万条手机号要用50秒左右的时间。于是又想用其他的想法。方法二1、不上传txt到服务器中,直接通过文件路径读取文件。
2、按行读取文件,校验,生成Arraylist3、传入list,设置数据库状态不提交,先查询数据库中,是否存在重复的手机号,如果没有,则addBatch,每1000条提交一次数据。这样的做法更慢了……还有个问题,当下面的方法仍然无法保证手机号的唯一性,因为传入的list不能保证是否有重复的号码就是这样的,不知道我说明白了没,由于是新手,想法有些稚嫩,所以请多批评,大家有什么好的思路跟我分享一下

解决方案 »

  1.   

    1、select count(*) from user where usernum = '13232232222'"
    2、索引
      

  2.   

    恩,count(*)比较快,但我更想知道关于java插入数据库的代码怎么优化
      

  3.   

    首先 文件应该不上传服务器的。。
       不管上不上传都得打开 读取文件。。//这一步很慢
          读取后直接生成Set集合(不是List,直接做到客户端手机号输入时不重复)
       每1000条清空缓存。。
          
      

  4.   

    没有办法确保txt文本中的号码是合法的,也没法保证不重复,所以必须验证查询一下数据库。
      

  5.   

      手机号码的合法性可以验证的吧。。
        Set集合保证不重复的。。
      

  6.   

    public static Collection readFile(String fileName, String tx_name,
    String usernum) throws Exception {
    Collection ret = new ArrayList();
    String s = null;
    File f = new File(fileName);
    BufferedReader br = null;
    try {
    // 读取文件对应的输出流buffer
    br = new BufferedReader(new InputStreamReader(
    new FileInputStream(f)));
    // 一行行读
    while ((s = br.readLine()) != null) {
    if (s.length() == 11 && isNumeric(s)) {
    if (Insert_user(s, tx_name, usernum)) {
    } else {
    // System.out.println("重复号码!"+s);
    ret.add(s);
    }
    }
    }
    // Iterator it = list.iterator();
    // while (it.hasNext()) {
    // System.out.println(it.next());
    // }
    System.out.println("read file is done");
    } catch (Exception e) {
    throw e;
    } finally {
    try {
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return ret;
    }public static boolean Insert_user(String s, String tx_name, String usernum) {
    Connection con = null;
    Statement stm = null;
    Statement stm1 = null;
    ResultSet rs = null;
    String sql = "";
    String sql2 = "";
    try {
    con = com.login.ConnPool.getConnection();
    stm = con.createStatement();
    stm1 = con.createStatement();
    sql = "select * from jifen_red_sms_user t where t.c_usernum = '"
    + s + "' and t.c_type = '" + tx_name + "'";
    // System.out.println(sql);
    rs = stm1.executeQuery(sql);
    if (rs.next()) {
    return false;
    } else {
    sql2 = "insert into jifen_red_sms_user t (t.n_id,t.c_usernum,t.c_type,t.d_date,t.c_op_usernum) values(jifen_red_sms_user_seq.nextval,'"+ s + "','" + tx_name + "',sysdate,'" + usernum + "')";
    stm.executeUpdate(sql2);
    } } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (rs != null)
    rs.close();
    if (stm != null)
    stm.close();
    if (stm1 != null)
    stm1.close();
    if (con != null)
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    return true;
    }
    jsp页面SendMsg.readFile(fileName,ct_name,usernum);方法
    现在是这么写的,感觉效率不是很好
      

  7.   

    SQL1是查询号码是否存在,SQL2是将号码插入数据库,这两个都语句语句都有啊,在定义下面