栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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")+"rn";//在txt中换行为tn
     write.append(content);
     content=rs.getString("account")+"rn";
     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);
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/145135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号