gpt4 book ai didi

java实现数据库的数据写入到txt的方法

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java实现数据库的数据写入到txt的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文讲解如何用java实现把数据库的数据写入到txt中 并实现类似下载软件的样子在网页中弹出下载. 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package datatest;
 
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import bean.ConnDB;
 
 
public class export extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   //设置编码
   response.setCharacterEncoding( "UTF-8" );
   //连接数据库
   ConnDB conn = new ConnDB();
   ServletOutputStream outputstream = null ;
   BufferedOutputStream buffoutputstream = null ;
   String txt_name = "导出的txt文件名.txt" ; //导出的txt文件名
   try {
    response.reset(); // 清空输出流
    response.setContentType( "text/plain;charset=utf-8" );
    //设置txt文件名称编码,防止中文乱码
    response.setHeader( "Content-disposition" , "attachment; filename=" +URLEncoder.encode(txt_name, "UTF-8" ));
   StringBuffer write = new StringBuffer();
    outputstream=response.getOutputStream();
    buffoutputstream = new BufferedOutputStream(outputstream);
   //根据id查询数据库
    int id=Integer.parseInt(request.getParameter( "id" ));
    String sql = "select a.id,name,account,password " ;
    sql+= "from test_rank a " ;
    sql+= "left join test_join b on b.id=a.id where a.id=" +id;
    ResultSet rs = conn.doQuery(sql);
    String content= "" ;
    try {
     while (rs.next())
     {
      //把数据库中读取的数据写入
      content=rs.getString( "name" )+ "\r\n" ; //在txt中换行为\t\n
      write.append(content);
      content=rs.getString( "account" )+ "\r\n" ;
      write.append(content);
      break ;
     }
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    //write.append(content);
    //设置编码 防止中文乱码
    String str = new String(write.toString().getBytes(), "gbk" );
    buffoutputstream.write(str.toString().getBytes( "gbk" ));
    buffoutputstream.flush();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally {
    if (outputstream != null )
     try {
      outputstream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    if (buffoutputstream != null )
     try {
      buffoutputstream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
   }
 
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   this .doGet(request, response);
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

最后此篇关于java实现数据库的数据写入到txt的方法的文章就讲到这里了,如果你想了解更多关于java实现数据库的数据写入到txt的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com