java实现数据库的数据写入到txt的方法
作者:码农之路 时间:2024-01-28 06:22:16
本文讲解如何用java实现把数据库的数据写入到txt中 并实现类似下载软件的样子在网页中弹出下载.
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 {
//设置编码
response.setCharacterEncoding("UTF-8");
//连接数据库
ConnDB conn = new ConnDB();
ServletOutputStream outputstream = null;
BufferedOutputStream buffoutputstream = null;
String txt_name = "导出的txt文件名.txt";//导出的txt文件名
try {
response.reset();// 清空输出流
response.setContentType("text/plain;charset=utf-8");
//设置txt文件名称编码,防止中文乱码
response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(txt_name, "UTF-8"));
StringBuffer write = new StringBuffer();
outputstream=response.getOutputStream();
buffoutputstream = new BufferedOutputStream(outputstream);
//根据id查询数据库
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())
{
//把数据库中读取的数据写入
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);
//设置编码 防止中文乱码
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);
}
}
标签:java,数据库,数据写入
0
投稿
猜你喜欢
Python编程中的异常处理教程
2022-10-16 04:26:23
JavaScript 判断浏览器类型及版本
2024-05-13 10:36:39
Django通用类视图实现忘记密码重置密码功能示例
2022-12-04 13:06:06
javascript 的 in 操作符实例详解
2007-10-07 12:00:00
Pytorch之view及view_as使用详解
2023-10-09 23:10:34
python中os和sys模块的区别与常用方法总结
2022-05-26 18:04:24
Sanic框架请求与响应实例分析
2023-05-26 00:07:00
Python中asyncore异步模块的用法及实现httpclient的实例
2021-02-19 01:05:18
Python的索引与切片原来该这样理解
2023-11-21 16:17:51
vue循环中点击选中再点击取消(单选)的实现
2024-05-13 09:43:50
如何得到数据库中所有表名 表字段及字段中文描述
2024-01-24 23:58:40
透明度设置
2009-12-12 18:51:00
django中forms组件的使用与注意
2021-03-11 00:14:04
Mysql中SQL语句不使用索引的情况
2024-01-28 04:19:57
asp防止盗链HTTP_REFERER判断代码
2010-03-12 10:41:00
如何解决mysql重装失败方法介绍
2024-01-19 10:52:05
Go语言利用time.After实现超时控制的方法详解
2024-04-26 17:21:50
Go语言实现请求超时处理的方法总结
2024-04-23 09:37:50
让验证码友好一点
2007-10-20 13:45:00
Python机器学习logistic回归代码解析
2021-02-20 23:59:16