java實(shí)現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法
更新時(shí)間:2017年07月25日 08:45:33 作者:碼農(nóng)之路
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文講解如何用java實(shí)現(xiàn)把數(shù)據(jù)庫的數(shù)據(jù)寫入到txt中 并實(shí)現(xiàn)類似下載軟件的樣子在網(wǎng)頁中彈出下載.
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ù)庫
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ù)庫
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ù)庫中讀取的數(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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-Plus中的selectByMap使用實(shí)例
Mybatis-Plus來對數(shù)據(jù)庫進(jìn)行增刪改查時(shí),將里面的函數(shù)試了個(gè)遍,接下來我就將使用selectByMap函數(shù)的簡單測試實(shí)例寫出來,方便沒有使用過的朋友們快速上手,感興趣的可以了解一下2021-11-11
SpringBoot如何實(shí)現(xiàn)starter原理詳解
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)starter原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
關(guān)于spring.factories的常用配置項(xiàng)說明
這篇文章主要介紹了關(guān)于spring.factories的常用配置項(xiàng)說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring MVC接受表單自動(dòng)封裝特性實(shí)例解析
這篇文章主要介紹了Spring MVC接受表單自動(dòng)封裝特性實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

