JDBC读取数据库,生成csv文件

public static void test4() throws ClassNotFoundException, SQLException {
   String fileName = "";
  ArrayList ddList = new ArrayList();
  FileOutputStream out = null;
  FileOutputStream out1 = null;
  FileOutputStream out2 = null;
  FileOutputStream out3 = null;
  FileOutputStream out4 = null;
  FileOutputStream out5 = null;
  StringBuilder sb = new StringBuilder();
  StringBuilder sb1 = new StringBuilder();
  StringBuilder sb2 = new StringBuilder();
  StringBuilder sb3 = new StringBuilder();
  StringBuilder sb4 = new StringBuilder();
  StringBuilder sb5 = new StringBuilder();
  try {
   out = new FileOutputStream(new File(fileName+ "remark.sql"));
   out1 = new FileOutputStream(new File(fileName+ "remark1.sql"));
   out2 = new FileOutputStream(new File(fileName+ "remark2.sql"));
   out3 = new FileOutputStream(new File(fileName+ "remark3.sql"));
   out4 = new FileOutputStream(new File(fileName+ "remark4.sql"));
   out5 = new FileOutputStream(new File(fileName+ "remark5.sql"));
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  
  String remark = "";
  String remark1 = "";
  String remark2 = "";
  String remark3 = "";
  String remark4 = "";
  String remark5 = "";
  String URL="数据库地址";
     String USER="用户";
     String PASSWORD="密码";
      //1.加载驱动程序
   Class.forName("com.mysql.jdbc.Driver");
   System.out.println("加载驱动=============");
    //2.获得数据库链接
         Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
         System.out.println("获取连接=============");
         //3.通过数据库的连接操作数据库,实现增删改查(使用Statement类)
         PreparedStatement  st= null;
         ResultSet rs = null;
        /* int id = 3439;
         int maxid = 104291916;
         int count = maxid;
         while(count>=id){*/
          String sql = "select id,remark,remark1,remark2,remark3,remark4,remark5 from 表 WHERE status = '1' ";
             st = conn.prepareStatement(sql);
             System.out.println("正在查询=============");
             rs =  st.executeQuery();
             System.out.println("查询成功=============");
             rs.last(); 
           int rowCount = rs.getRow();
             int w = 0;
             rs.beforeFirst();
            while(rs.next()){
             w++;
             DDebtorInfo dd = new DDebtorInfo();
             dd.setId(rs.getInt("id"));
             dd.setRemark(rs.getString("remark")==null?"":rs.getString("remark"));
             dd.setRemark1(rs.getString("remark1")==null?"":rs.getString("remark1"));
             dd.setRemark2(rs.getString("remark2")==null?"":rs.getString("remark2"));
             dd.setRemark3(rs.getString("remark3")==null?"":rs.getString("remark3"));
             dd.setRemark4(rs.getString("remark4")==null?"":rs.getString("remark4"));
             dd.setRemark5(rs.getString("remark5")==null?"":rs.getString("remark5"));
            System.out.println("装载中====共"+rowCount+"个====装载完====="+w+"个");
             ddList.add(dd);
            }
            try {
    int i = 0;
    int r = ddList.size();
     for(DDebtorInfo d : ddList){
      i++;
      remark = d.getRemark().toString()==null?"":d.getRemark().toString();
      remark1 = d.getRemark1()==null?"":d.getRemark1();
      remark2 = d.getRemark2()==null?"":d.getRemark2();
      remark3 = d.getRemark3()==null?"":d.getRemark3();
      remark4 = d.getRemark4()==null?"":d.getRemark4();
      remark5 = d.getRemark5()==null?"":d.getRemark5();
      boolean b = yanz(remark);
      boolean b1 = yanz(remark1);
      boolean b2 = yanz(remark2);
      boolean b3 = yanz(remark3);
      boolean b4 = yanz(remark4);
      boolean b5 = yanz(remark5);
      if(b){
       sb.append(d.getId()+",");
       sb.append(d.getRemark().toString()+",");
       sb.append("\r\n");
       
      }
      if(b1){
       sb1.append(d.getId()+",");
       sb1.append(d.getRemark1()+",");
       sb1.append("\r\n");
      }
      if(b2){
       sb2.append(d.getId()+",");
       sb2.append(d.getRemark2()+",");
       sb2.append("\r\n");
      }
      if(b3){
       sb3.append(d.getId()+",");
       sb3.append(d.getRemark3()+",");
       sb3.append("\r\n");
      }
      if(b4){
       sb4.append(d.getId()+",");
       sb4.append(d.getRemark4()+",");
       sb4.append("\r\n");
      }
      if(b5){
       sb5.append(d.getId()+",");
       sb5.append(d.getRemark5()+",");
       sb5.append("\r\n");
      }
      System.out.println("============债务人个数======"+ddList.size()+"======完成个数=="+i+"=====剩余"+(r-i));
     }
     String t = sb.toString();
     if(t.length()>0){
      t = t.substring(0, t.length()-3);
      System.out.println("======="+t);
      out.write(t.getBytes());
     }
              
              
              String t1 = sb1.toString();
              if(t1.length()>0){
      t1 = t1.substring(0, t1.length()-3);
      System.out.println("======="+t1);
      out1.write(t1.getBytes());
     }
              
              
              String t2 = sb2.toString();
              if(t2.length()>0){
      t2 = t2.substring(0, t2.length()-3);
      System.out.println("======="+t2);
      out2.write(t2.getBytes());
     }
             
              
              String t3 = sb3.toString();
              if(t3.length()>0){
      t3 = t3.substring(0, t3.length()-3);
      System.out.println("======="+t3);
      out3.write(t3.getBytes());
     }
              
              
              String t4 = sb4.toString();
              if(t4.length()>0){
      t4 = t4.substring(0, t4.length()-3);
      System.out.println("======="+t4);
      out4.write(t4.getBytes());
     }
              
              
              String t5 = sb5.toString();
              if(t5.length()>0){
      t5 = t5.substring(0, t5.length()-3);
      System.out.println("======="+t5);
      out5.write(t5.getBytes());
     }
              
               out.close();
     out1.close();
      out2.close();
      out3.close();
      out4.close();
      out5.close();
     
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
             try {
               rs.close();
                  st.close();
                  conn.close();
               out.close();
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
            //count-=1000;
       /* }
         try {
    out.close();
    out1.close();
     out2.close();
     out3.close();
     out4.close();
     out5.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
    
         conn.close();*/
          
           
 }
 public static boolean yanz(String jj){
  boolean b = false;
  /*String regex = ".*((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}.*";//验证手机号
  String gh =".*[0][1-9]{2,3}-[0-9]{5,10}.*";// 验证带区号的
  String gh2 =".*[1-9]{1}[0-9]{5,8}.*";// 验证不带区号的
*/  
  String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";//验证纯手机号
  String regex2 = "[^0-9]{1,}((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}";//手机号前非数字
  String regex3 = "((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}[^0-9]{1,}";//手机号后非数字
  
  String gh ="^[0][0-9]{2,3}-[0-9]{5,10}$";// 验证带区号的
  String gh2 ="[^0-9]{1,}[0][0-9]{2,3}-[0-9]{5,10}";// 验证带区号的
  String gh3 ="[0][0-9]{2,3}-[0-9]{5,10}[^0-9]{1,}";// 验证带区号的
  
  String bgh ="^[0-9]{2,3}[0-9]{5,8}$";// 验证不带区号的
  String bgh2 ="[^0-9]{1,}[0-9]{2,3}[0-9]{5,8}";// 验证不带区号的
  String bgh3 ="[0-9]{2,3}[0-9]{5,8}[^0-9]{1,}";// 验证不带区号的
  
  
   Pattern p = Pattern.compile(regex);
   Matcher m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(regex2);
   m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(regex3);
   m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   
   p = Pattern.compile(gh);
    m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(gh2);
   m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(gh3);
   m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(bgh);
    m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(bgh2);
   m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   p = Pattern.compile(bgh3);
   m = p.matcher(jj);
   b =  m.matches();
   if(b){
    return b; 
   }
   return b;
 }

你可能感兴趣的:(JDBC,文件导出,JDCB读取数据库,验证是否为手机号,导出csv文件)