java實(shí)現(xiàn)數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt的方法-創(chuàng)新互聯(lián)

本文講解如何用java實(shí)現(xiàn)把數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt中 并實(shí)現(xiàn)類似下載軟件的樣子在網(wǎng)頁(yè)中彈出下載.

創(chuàng)新互聯(lián)擁有網(wǎng)站維護(hù)技術(shù)和項(xiàng)目管理團(tuán)隊(duì),建立的售前、實(shí)施和售后服務(wù)體系,為客戶提供定制化的網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、成都西云數(shù)據(jù)中心解決方案。為客戶網(wǎng)站安全和日常運(yùn)維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、商城建設(shè)、政府網(wǎng)站等各類型客戶群體,為全球上千企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。
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 {
  //設(shè)置編碼
  response.setCharacterEncoding("UTF-8");
  //連接數(shù)據(jù)庫(kù)
  ConnDB conn = new ConnDB();
  ServletOutputStream outputstream = null;
  BufferedOutputStream buffoutputstream = null; 
  String txt_name = "導(dǎo)出的txt文件名.txt";//導(dǎo)出的txt文件名
  try {
   response.reset();// 清空輸出流
   response.setContentType("text/plain;charset=utf-8");
   //設(shè)置txt文件名稱編碼,防止中文亂碼
   response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(txt_name, "UTF-8"));
  StringBuffer write = new StringBuffer();
   outputstream=response.getOutputStream();
   buffoutputstream = new BufferedOutputStream(outputstream);
  //根據(jù)id查詢數(shù)據(jù)庫(kù)
   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())
    {
     //把數(shù)據(jù)庫(kù)中讀取的數(shù)據(jù)寫入
     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);
   //設(shè)置編碼 防止中文亂碼
   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);
 }

}

新聞標(biāo)題:java實(shí)現(xiàn)數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt的方法-創(chuàng)新互聯(lián)
文章起源:http://www.muchs.cn/article34/dpgipe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、App開發(fā)面包屑導(dǎo)航、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化