我从数据库中取到值,怎么把它写到文件里面去?

解决方案 »

  1.   

    FileWriter fw = new FileWriter("xxxxoooo.txt");
    fw.write("你好");
    fw.close();
      

  2.   

    我已经把数据取出来了(用JDBC取的),现在我想把取出的数据写到文件里面去。该怎么写?
      

  3.   

    public void DBMessage(){
    String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url="jdbc:sqlserver://localhost:3333;databaseName=aidb";
    String us="sa";
    String pw="GuoXiaoXian9207";
    String sql="select * from ReportManage";

    Connection conn=null;
    Statement stat=null;
    ResultSet rs=null;

    try {
    Class.forName(dbdriver);
    conn=DriverManager.getConnection(url, us, pw);
    stat=conn.createStatement();
    rs=stat.executeQuery(sql);

    while(rs.next()){
    System.out.println("id="+rs.getInt("id"));
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    这是链接的数据 /**
     * 导出的文件
     */
    public void ExportOutTableTest() {
    JFileChooser chooser = new JFileChooser();
    chooser.showSaveDialog(this);
    File file = chooser.getSelectedFile();
    String fileName = file.getName();
    if (fileName != null && fileName.trim().length() > 0) {
    if (!(fileName.endsWith(".txt"))) {
    fileName = fileName.concat(".txt");
    }
    }
    file = new File(file.getParent().concat(File.separator).concat(fileName));
    if (file.exists()) {
    int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
    if (i != JOptionPane.YES_OPTION) {
    return;
    }
    }
    try {
    file.createNewFile();
    FileWriter fw = new FileWriter(file);
    fw.write(jtextFiledExport.getText());
    fw.flush();
    fw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",JOptionPane.WARNING_MESSAGE);
    return;
    }
    }现在我怎么把取出的数据 放到文件里面去?
      

  4.   

    package mySystem.myMail.actions.login;import java.io.FileWriter;
    import java.io.IOException;public class test1 { public static void main(String[] args) {
    FileWriter fw;
    try {
    fw = new FileWriter("xxxxoooo.txt");
    fw.write("你好");
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    你把   你好 替换就行了………………
      

  5.   

    colId, sid, id, colCode, colName, colPid, colLevel, colType, colLenght:这是数据库的列名
    75122,2,6763,BQSJ,期末数|||实际数,0,75122,0,0,2010-07-26 10:51:54.193:这是数据库的数据
    75123,10,6763,BNNC,年初数,0,75123,0,0,2010-07-26 10:51:54.193,1,1,NULL,
    75124,6,6763,BQHB,合并数,0,75124,0,0,2010-07-26 10:51:54.193,1,0,NULL,0我怎么把它导出这种形式的TXT???
      

  6.   

    恩,先导入列名colId, sid, id, colCode, colName, colPid, colLevel, colType, colLenght 加个换行
    接着到while(rs.next()){String str="";
    str+=rs.getInt("colId");//根据数据库情况而定
    Str+=",";
    String +=rs.getInt("sid");
    Str+=",";
    //此处省略n个字,楼主你懂的
    str =substr(0,str.length-1);
    str+="\r\t"//换行符
    //把String写入文件中
    }
    具体情况你修改一下,可能有部分拼写错误,木有环境
      

  7.   

    从数据库里面获取的可以string对象吧,那就创建文件流写入吧
      

  8.   

    public void DBMessage(){
    String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url="jdbc:sqlserver://localhost:3333;databaseName=aidb";
    String us="sa";
    String pw="GuoXiaoXian9207";
    String sql="select * from ReportManage";

    Connection conn=null;
    Statement stat=null;
    ResultSet rs=null;

    try {
    Class.forName(dbdriver);
    conn=DriverManager.getConnection(url, us, pw);
    stat=conn.createStatement();
    rs=stat.executeQuery(sql);

    while(rs.next()){
    String str="";
    str += rs.getInt("colId");
    str += ",";
    str += rs.getInt("sid");
    str += ",";
    str += rs.getInt("id");
    str += ",";
    str += rs.getInt("colCode");
    str += ",";
    str += rs.getString("colName");
    str += ",";
    str += rs.getInt("colPid");
    str += ",";
    str += rs.getInt("colLevel");
    str += ",";
    str += rs.getInt("colType");
    str += ",";
    str += rs.getInt("colLenght");
    str += ",";
    str += rs.getString("colDate");
    str += ",";
    str += rs.getInt("com_id");
    str += ",";
    str += rs.getInt("display");
    str += ",";
    str += rs.getInt("formula");
    str += ",";
    str += rs.getInt("isText");
    str += "\r\n";
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void ExportOutTableTest(String str) {
    JFileChooser chooser = new JFileChooser();
    chooser.showSaveDialog(this);
    File file = chooser.getSelectedFile();
    String fileName = file.getName();
    if (fileName != null && fileName.trim().length() > 0) {
    if (!(fileName.endsWith(".txt"))) {
    fileName = fileName.concat(".txt");
    }
    }
    file = new File(file.getParent().concat(File.separator).concat(fileName));
    if (file.exists()) {
    int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
    if (i != JOptionPane.YES_OPTION) {
    return;
    }
    }
    try {
    file.createNewFile();
    FileWriter fw = new FileWriter(file);
    fw.write(str);//传进来的str
    fw.flush();
    fw.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",JOptionPane.WARNING_MESSAGE);
    return;
    }
    }
    这样吗?我感觉还是有问题...
      

  9.   

    还有什么问题,我觉得你的大逻辑是没有什么问题的,
    都是小细节问题,自己调调还有你的DBMessage方法应该返回String吧,
    把你拼的那个字符串返回来,下面ExportOutTableTest的时候才能用到啊
      

  10.   

    思路
    用FileReader读出来
    String str = "";
    List<String[]> list = new ArrayList<String[]>();
    while(str=in.readline()!=null){
    //用split分开放入string[] strs 中,
    list.add(strs);
    }
    //接着你的数据都在list中了
    list.get(0)是你的字段名
    从1开始就是数据。
      

  11.   

    这样做不太灵活,要是我换一张表导的话又要改代码。有别的方法吗?用ResultSetMetaData怎么写?自动获取列的...