日期:2014-05-17  浏览次数:20924 次

读取数据库写入数组
package list;



import java.io.File;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.*;

import java.util.ArrayList;
import java.util.List;


public class ConnOracle {



  private static Statement connc;
  private static Connection connOrac;

  //就差把它搞到数组里了
   
  public static void main(String args[]) throws IOException{
 
  try {
  Class.forName("com.mysql.jdbc.Driver").newInstance(); 
  connOrac = DriverManager.getConnection("jdbc:mysql://localhost/ip?user=root&password=1314521&useUnicode=true&characterEncoding=gb2312");
  connc=connOrac.createStatement();
  System.out.println("ok1");
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   
   
  } catch (Exception e) {
  e.printStackTrace();
  }
   
   
  List<String> userName = new ArrayList<String>();
  String sql="SELECT SUBSTRING(startip,1,9),startip,endip FROM ip WHERE country LIKE '广东%' GROUP BY SUBSTRING(startip,1,9) HAVING COUNT(*)>3 " ;


   

  try {
  ResultSet rs = connc.executeQuery(sql);
  // int i = 0;
  // int rowCount = rs.getRow();//首先将指针移动到最后一条记录,获得总记录数
  String[] tastr = new String[rs.getRow()];
  int i=0;

  for (rs.next() ){
  tastr[i] = rs.getString(1);
  tastr[i+1] = rs.getString(2);
  tastr[i+2] = rs.getString(3);
  tastr[i+3] = rs.getString(4);
  i++;

 
  File f = new File("D:"+File.separator+"testfile.txt");
  //用FileOutputSteam包装文件,并设置文件可追加
  OutputStream out;
try {
out = new FileOutputStream(f,true);

out.write(tastr[i+2].getBytes()); //向文件中写入数据
out.write('\r'); // \r\n表示换行
out.write('\n'); 
 
out.close(); //关闭输出流
System.out.println("写入成功!");



} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

   
 

   
  } 
   
   
   
   
   
   
  
   
   
 
   
   
  } catch (SQLException ex) {
  System.out.println(ex);
  } 
  // TODO Auto-generated catch block