当前位置: 代码迷 >> Java Web开发 >> 读取数据库写入数组,该如何处理
  详细解决方案

读取数据库写入数组,该如何处理

热度:6155   发布时间:2013-02-25 21:21:54.0
读取数据库写入数组
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
 
 
  }
   

   
}
   
   
   
   

 

------解决方案--------------------------------------------------------
个人觉得应该这样吧:
Java code
        OutputStream out = null;        try {            ResultSet rs = connc.executeQuery(sql);            String[][] datas = new String[rs.getRow()][4];                        int i = 0;            while (rs.next()) {                datas[i][0] = rs.getString(1);                datas[i][1] = rs.getString(1);                datas[i][2] = rs.getString(1);                datas[i][3] = rs.getString(1);                i++;            }            for(String[] data : datas){                for(String d : data){                    File f = new File("D:" + File.separator + "testfile.txt");                    // 用FileOutputSteam包装文件,并设置文件可追加                    out = new FileOutputStream(f, true);                    out.write(d.getBytes()); // 向文件中写入数据                }                out.write('\r'); // \r\n表示换行                out.write('\n');                System.out.println("写入成功!");            }        } catch (SQLException ex) {            System.out.println(ex);        } finally{            if(out != null) out.close();        }
  相关解决方案