小试从数据库读取数据到文件
来源:互联网 发布:centos jdk 环境变量 编辑:程序博客网 时间:2024/06/05 06:12
读数据库写入文本文件:
public static void readDataToTxt() throws Exception
{
Long d=new Date().getTime();
PrintWriter pw= new PrintWriter(new BufferedWriter(new FileWriter("c://a.txt",true)));
Idao dao=Dao.getInstance();
List list=dao.search("from User n");
Iterator it=list.iterator();
String result;
String newline = "/r/n";
try{
while(it.hasNext())
{
User user=(User)it.next();
result=user.getId()+","+user.getUserName()+","+user.getUserPwd()+","+user.getUserEmail();
pw.write(result);
pw.write(newline);
}
Long d2=new Date().getTime();
pw.write(String.valueOf(d2-d));
pw.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
读数据库写入xls文件:
public static void readDataToExcel() throws Exception
{
Long d=new Date().getTime();
WritableWorkbook book = Workbook.createWorkbook( new File( "WebRoot/user/aa.xls" ));
WritableSheet sheet = book.createSheet( "test" , 0 );
Idao dao=Dao.getInstance();
List list=dao.search("from User n");
int num=dao.searchCount("select count(*) from User n");
Iterator it=list.iterator();
int t=0;
Label label=null;
Label label2=null;
Label label3=null;
Label label4=null;
try{
while(it.hasNext())
{
User user=(User)it.next();
if(t<num)
{
label = new Label( 0 ,t ,String.valueOf(user.getId()));
label2 = new Label( 1 ,t ,user.getUserName());
label3 = new Label( 2 ,t ,user.getUserPwd());
label4 = new Label( 3 ,t ,user.getUserEmail());
sheet.addCell(label);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
}
t=t+1;
}
Long d2=new Date().getTime();
System.out.println(d2-d);
Label label5=new Label( 0 ,t+1 ,String.valueOf(d2-d));;
book.write();
book.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
用jdbc访问读取数据到excel文件:
public static void readDataToExcel3() throws Exception
{
Long d=new Date().getTime();
WritableWorkbook book = Workbook.createWorkbook( new File( "c://3.xls" ));
//生成名为“test”的工作表
WritableSheet sheet = book.createSheet( "test" , 0 );
int t=0;
Label label=null;
Label label2=null;
Label label3=null;
Label label4=null;
String sql="select * from user";
int rn=0;
Statement stms=null;
ResultSet rs=null;
try{
stms=Db.getStm();
rs=stms.executeQuery(sql);
while(rs.next())
{
label = new Label( 0 ,rn ,String.valueOf(rs.getInt(1)));
label2 = new Label( 1 ,rn ,rs.getString(2));
label3 = new Label( 2 ,rn ,rs.getString(3));
label4 = new Label( 3 ,rn ,rs.getString(4));
sheet.addCell(label);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
rn=rn+1;
}
Long d2=new Date().getTime();
Label label5=new Label( 0 ,rn+1 ,String.valueOf(d2-d));
sheet.addCell(label5);
// 写入数据并关闭文件
book.write();
book.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
程序代码
public static void readDataToTxt() throws Exception
{
Long d=new Date().getTime();
PrintWriter pw= new PrintWriter(new BufferedWriter(new FileWriter("c://a.txt",true)));
Idao dao=Dao.getInstance();
List list=dao.search("from User n");
Iterator it=list.iterator();
String result;
String newline = "/r/n";
try{
while(it.hasNext())
{
User user=(User)it.next();
result=user.getId()+","+user.getUserName()+","+user.getUserPwd()+","+user.getUserEmail();
pw.write(result);
pw.write(newline);
}
Long d2=new Date().getTime();
pw.write(String.valueOf(d2-d));
pw.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
读数据库写入xls文件:
程序代码
public static void readDataToExcel() throws Exception
{
Long d=new Date().getTime();
WritableWorkbook book = Workbook.createWorkbook( new File( "WebRoot/user/aa.xls" ));
WritableSheet sheet = book.createSheet( "test" , 0 );
Idao dao=Dao.getInstance();
List list=dao.search("from User n");
int num=dao.searchCount("select count(*) from User n");
Iterator it=list.iterator();
int t=0;
Label label=null;
Label label2=null;
Label label3=null;
Label label4=null;
try{
while(it.hasNext())
{
User user=(User)it.next();
if(t<num)
{
label = new Label( 0 ,t ,String.valueOf(user.getId()));
label2 = new Label( 1 ,t ,user.getUserName());
label3 = new Label( 2 ,t ,user.getUserPwd());
label4 = new Label( 3 ,t ,user.getUserEmail());
sheet.addCell(label);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
}
t=t+1;
}
Long d2=new Date().getTime();
System.out.println(d2-d);
Label label5=new Label( 0 ,t+1 ,String.valueOf(d2-d));;
book.write();
book.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
用jdbc访问读取数据到excel文件:
程序代码
public static void readDataToExcel3() throws Exception
{
Long d=new Date().getTime();
WritableWorkbook book = Workbook.createWorkbook( new File( "c://3.xls" ));
//生成名为“test”的工作表
WritableSheet sheet = book.createSheet( "test" , 0 );
int t=0;
Label label=null;
Label label2=null;
Label label3=null;
Label label4=null;
String sql="select * from user";
int rn=0;
Statement stms=null;
ResultSet rs=null;
try{
stms=Db.getStm();
rs=stms.executeQuery(sql);
while(rs.next())
{
label = new Label( 0 ,rn ,String.valueOf(rs.getInt(1)));
label2 = new Label( 1 ,rn ,rs.getString(2));
label3 = new Label( 2 ,rn ,rs.getString(3));
label4 = new Label( 3 ,rn ,rs.getString(4));
sheet.addCell(label);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
rn=rn+1;
}
Long d2=new Date().getTime();
Label label5=new Label( 0 ,rn+1 ,String.valueOf(d2-d));
sheet.addCell(label5);
// 写入数据并关闭文件
book.write();
book.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
- 小试从数据库读取数据到文件
- java从Excel文件读取数据到数据库和从读取数据库数据写入Excel
- 从Excel文件读取数据批量导入到Mysql数据库
- 从Mysql数据库读取数据批量导入到Excel文件
- python-从文件读取json数据写到数据库
- 关于读取Excel文件数据到数据库,以及从数据库导出数据到Excel的例子
- 从数据库中读取数据到Excel
- 从文本文件读取数据存储到数据库
- 从文件读取数据到SAP
- 从资源读取数据到文件中
- c++从文件读取数据到字符串
- ThinkPHP读取数据库数据到模板文件
- 使用Apache POI创建Excel,并从数据库中读取数据写入到Excel文件中
- java从文件中读取数据然后插入到数据库表中
- 从数据库中读取表中数据然后写到txt文件中
- 从mongoDB数据库中读取tvsplst数据写到CSV文件里面
- XNA:保存数据到文件和从文件读取数据
- 数据存储到文件和从文件中读取数据
- C#中的Event,多余乎?不多余
- 端午节快乐
- java类型转换参考大全
- with和有名function那些不得不说的事....
- Lucene深入研究(2)
- 小试从数据库读取数据到文件
- java操作Excel、PDF文件
- prototype 用法
- 专门为PB,VB程序获取本机IP而制作的动态库
- 关于文件操作的若干方法(File类)
- [翻译+推荐]你需要知道的:WCF、WF、ADO.NET SyncServices和ClickOnce
- 毕业3年,月薪从5k到20k
- 利用uploadbean实现上传
- log4j日志使用详解