java實現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法
本文講解如何用java實現(xiàn)把數(shù)據(jù)庫的數(shù)據(jù)寫入到txt中 并實現(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)文章
RedisKey的失效監(jiān)聽器KeyExpirationEventMessageListener問題
這篇文章主要介紹了RedisKey的失效監(jiān)聽器KeyExpirationEventMessageListener問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA實現(xiàn)Maven項目創(chuàng)建并連接Tomcat方式
Maven是一款由Apache開發(fā)的項目管理工具,主要用于Java項目的構(gòu)建和依賴管理,它通過pom.xml文件自動管理項目依賴的jar包,簡化了項目構(gòu)建過程,Maven支持項目從編寫源代碼到編譯、測試、打包、部署的全過程管理,其依賴管理功能免去了手動添加jar包的麻煩2024-10-10
Spring中@RestControllerAdvice注解的使用詳解
這篇文章主要介紹了Spring中@RestControllerAdvice注解的使用詳解,@RestControllerAdvice是一個組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,需要的朋友可以參考下2024-01-01
SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題)
這篇文章主要介紹了SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教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腳本實現(xiàn)動態(tài)編程詳解
這篇文章主要為大家介紹了SpringBoot整合Groovy腳本實現(xiàn)動態(tài)編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Java中==與equals()及hashcode()三者之間的關(guān)系詳解
最近也是在讀Hollis的《深入理解Java核心技術(shù)》里面一節(jié)講到了equals()和hashcode()的關(guān)系,對于這個高頻面試點,咱們需要認真理清一下幾者之間的關(guān)系2022-10-10

