java實(shí)現(xiàn)數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt的方法
本文講解如何用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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
RedisKey的失效監(jiān)聽(tīng)器KeyExpirationEventMessageListener問(wèn)題
這篇文章主要介紹了RedisKey的失效監(jiān)聽(tīng)器KeyExpirationEventMessageListener問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA實(shí)現(xiàn)Maven項(xiàng)目創(chuàng)建并連接Tomcat方式
Maven是一款由Apache開(kāi)發(fā)的項(xiàng)目管理工具,主要用于Java項(xiàng)目的構(gòu)建和依賴管理,它通過(guò)pom.xml文件自動(dòng)管理項(xiàng)目依賴的jar包,簡(jiǎn)化了項(xiàng)目構(gòu)建過(guò)程,Maven支持項(xiàng)目從編寫源代碼到編譯、測(cè)試、打包、部署的全過(guò)程管理,其依賴管理功能免去了手動(dòng)添加jar包的麻煩2024-10-10
Spring中@RestControllerAdvice注解的使用詳解
這篇文章主要介紹了Spring中@RestControllerAdvice注解的使用詳解,@RestControllerAdvice是一個(gè)組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,需要的朋友可以參考下2024-01-01
Java中Lombok工具庫(kù)使用的技術(shù)指南
Lombok 是 Java 開(kāi)發(fā)中常用的工具庫(kù),通過(guò)注解的方式大大簡(jiǎn)化了代碼開(kāi)發(fā),本篇文章將從基礎(chǔ)入門到高級(jí)用法,深入講解 Lombok 的使用技巧和注意事項(xiàng),快跟隨小編一起來(lái)學(xué)習(xí)一下吧2025-04-04
SpringCloud啟動(dòng)eureka server后,沒(méi)報(bào)錯(cuò)卻不能訪問(wèn)管理頁(yè)面(404問(wèn)題)
這篇文章主要介紹了SpringCloud啟動(dòng)eureka server后,沒(méi)報(bào)錯(cuò)卻不能訪問(wèn)管理頁(yè)面(404問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
java 出現(xiàn)Zipexception 異常的解決辦法
這篇文章主要介紹了java 出現(xiàn)Zipexception 異常的解決辦法的相關(guān)資料,出現(xiàn) java.util.zip.ZipException: error in opening zip file 異常的原因及解決方法,需要的朋友可以參考下2017-08-08
SpringBoot整合Groovy腳本實(shí)現(xiàn)動(dòng)態(tài)編程詳解
這篇文章主要為大家介紹了SpringBoot整合Groovy腳本實(shí)現(xiàn)動(dòng)態(tài)編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Java中==與equals()及hashcode()三者之間的關(guān)系詳解
最近也是在讀Hollis的《深入理解Java核心技術(shù)》里面一節(jié)講到了equals()和hashcode()的關(guān)系,對(duì)于這個(gè)高頻面試點(diǎn),咱們需要認(rèn)真理清一下幾者之間的關(guān)系2022-10-10

