溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java實(shí)現(xiàn)數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt的方法

發(fā)布時(shí)間:2020-09-14 13:20:26 來(lái)源:腳本之家 閱讀:194 作者:碼農(nóng)之路 欄目:編程語(yǔ)言

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

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);
 }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI