java读取数据库字段和值.doc_第1页
java读取数据库字段和值.doc_第2页
java读取数据库字段和值.doc_第3页
java读取数据库字段和值.doc_第4页
java读取数据库字段和值.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java读取数据库字段和值java读取数据库字段和值,输出到regedit,xml,text,excel,pdf的程序 主要功能,通过读取配置文件,来从数据库里提取字段和值,写入到regedit,xml,text,excel,pdf中。程序是dos界面的,由于仓促没有写windows界面,如果各位有兴趣,可以改成windows界面的。由于有配置文件的缘故,所以所有的操作不需要改写代码,只需在配置文件中改写就可以了。主程序:import java.io.*;import java.util.prefs.*;import java.sql.*;import jxl.*;import jxl.write.*;import java.util.*;import java.util.Properties;import com.lowagie.text.*;import com.lowagie.text.pdf.*;import java.awt.Color;/* *author:kenshin *effect:from database reading values ,output regedit or text or xml or excel or pdf. *create time:2004/6/8 *modify time:2004/6/9 *copyright:2004*/public class PrefsDemo static Preferences prefsdemo; static String field=new String1000; static String values=new String1000; static int account=0,maxCount=0; static String path=null,driver=null,user=null,password=null,odbcname=null; static String txtname=null,xmlname=null,excelname=null,pdfname=null,sql=null; static String author=null,createtime=null,effect=null,version=null; public static void main(String args) String rnumber=null; int number=0; BufferedReader buf; /initialize config file Initconfig(); /connection database DBconnection(); /goto enterLoop enterLoop: while(number7) System.out.println(please input an number of between 1 to 7 ); number=0; continue enterLoop; switch(number) case 1: /output regedit RegeditOutput(); break; case 2: /output xml XmlOutput(); break; case 3: /output text TextOutput(); break; case 4: /output excel ExcelOutput(); break; case 5: /output excel PdfOutput(); break; case 6: /output all RegeditOutput(); XmlOutput(); TextOutput(); ExcelOutput(); PdfOutput(); break; catch(IOException e)System.err.println(e); catch(NumberFormatException e) System.out.println(please input an number type!); number=0; continue enterLoop; /* *effect:initialize config file *input value :null *return value:null *create time:2004/6/11 *edit time:null */ static void Initconfig() Properties prop = new Properties(); String propFileName = perties; FileInputStream fis; try fis = new FileInputStream(new File(propFileName); prop.load(fis); /get config files values path = prop.getProperty(path); odbcname = prop.getProperty(odbc); driver = prop.getProperty(driver); user = prop.getProperty(user); password = prop.getProperty(password); txtname = prop.getProperty(txtname); xmlname = prop.getProperty(xmlname); excelname = prop.getProperty(excelname); pdfname = prop.getProperty(pdfname); author = prop.getProperty(author); createtime = prop.getProperty(createtime); effect = prop.getProperty(effect); version = prop.getProperty(version); sql = prop.getProperty(sql); /close inputstream fis.close(); System.out.println(*); System.out.println(*config read successfull*); System.out.println(*); catch(IOException e)System.err.println(e); /* *effect:connection database *input value :null *return value:null *create time:2004/6/9 *edit time:null */ static void DBconnection() /jdbc-odbc variable initialize Connection con=null; Statement stm=null; ResultSet rs=null; /jdbc-odbc bridge try Class.forName(driver); con=DriverManager.getConnection(odbcname,user,password); stm=con.createStatement(); rs=stm.executeQuery(sql); /get columncount values account=rs.getMetaData().getColumnCount(); System.out.println(*); System.out.println(*DB connect successfull *); System.out.println(*); /get database ColumnName for(int i=1;i=account;i+) fieldi=rs.getMetaData().getColumnName(i); while(rs.next() for(int j=1;j=account;j+) +maxCount; valuesmaxCount=rs.getString(fieldj); System.out.println(valuesmaxCount); /close database rs.close(); stm.close(); con.close(); catch(Exception e) System.err.println(e); /* *effect:output regedit *input value :null *return value:null *create time:2004/6/9 *edit time:null */ static void RegeditOutput() prefsdemo = Preferences.userRoot().node(/com/sunway/spc); /wirte regedit prefsdemo.put(author,author); prefsdemo.put(createtime,createtime); prefsdemo.put(effect,effect); prefsdemo.put(version,version); System.out.println(regedit create successfull!); /* *effect:output xml *input value :null *return value:null *create time:2004/6/9 *edit time:null */ static void XmlOutput() /write xml try File myfile = new File(path+xmlname); if(!(myfile.exists() myfile.createNewFile(); / write file FileOutputStream fos = new FileOutputStream(path+xmlname); prefsdemo.exportNode(fos); catch (Exception e) System.err.println(Cannot export nodes: + e); System.out.println(xml create successfull ); /* *effect:output text *input value :null *return value:null *create time:2004/6/9 *edit time:null */ static void TextOutput() /field length int length1=new int1000; /values length int length2=new int1000; String space= ; String str1=-; String str2=new String1000; String str3=new String1000; /check /n int q=1; /initialize str1 str2 for(int i=1;i=maxCount;i+) str2i=; str3i=; try /create file File myfile = new File(path+txtname); if(!(myfile.exists() myfile.createNewFile(); / write file FileWriter fw=new FileWriter(path+txtname); for(int i=1;i=maxCount;i+) if(i=account) length1i=25-fieldi.length(); /account length2 length for(int j=1;j=length1i;j+) str2i=str2i+ ; length2i=25-valuesi.length(); /account length3 length for(int j=1;j=length2i;j+) str3i=str3i+ ; /write field for(int i=1;i=account;i+) fw.write(fieldi); fw.write(str2i+space); /write - fw.write(n); for(int i=1;i=account;i+) fw.write(str1+space); /write values fw.write(n); for(int i=1;i=maxCount;i+) fw.write(valuesi+str3i+space); if(i=account*q) fw.write(n); q*=2; fw.close(); catch(FileNotFoundException e)System.err.println(e); catch(IOException e)e.printStackTrace(); System.out.println(text create successfull ); /* *effect:output excel *input value :null *return value:null *create time:2004/6/9 *edit time:2004/6/10 */ static void ExcelOutput() /label1:field label2:values Label label1=null,label2=null; int result=0,rows=0; try /create an new file File myfile = new File(path+excelname); if(!(myfile.exists() myfile.createNewFile(); System.out.println(an new excel create successful); /create an new excel WritableWorkbook workbook = Workbook.createWorkbook(myfile); /use first excels sheet WritableSheet sheet = workbook.createSheet(record, 0); / add table title and values for(int i=1;i=account;i+) /add table title label1 = new Label(i-1, 0, fieldi); sheet.addCell(label1); /account rows values result=maxCount/account; for(int i=1;i=result;i+) for(int m=1;m=account;m+) rows+; label2=new Label(m-1,i,valuesrows); sheet.addCell(label2); workbook.write(); workbook.close(); catch(Exception e)System.err.println(e); System.out.println(excel create successfull ); /* *effect:output pdf file *input value :null *return value:null *create time:2004/6/11 *edit time:null */ static void PdfOutput() Document document; Rectangle pageRect; try document=new Document(PageSize.A4, 50, 50, 100, 50); pageRect=document.getPageSize(); PdfWriter.getInstance(document, new FileOutputStream(path+pdfname); HeaderFooter header = new HeaderFooter(new Phrase(student information), false); header.setBorder(2); header.setAlignment(Element.ALIGN_RIGHT); document.setHeader(header); /add page HeaderFooter footer = new HeaderFooter(new Phrase(the),new Phrase(page); footer.setAlignment(Element.ALIGN_CENTER); footer.setBorder(1); document.setFooter(footer); /open document document.open(); /create table Table table = new Table(account); table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE); table.setBorder(Rectangle.NO_BORDER); int hws = new intaccount; for(int i=1;i=account;i+) hwsi-1=20; table.setWidths(hws); table.setWidth(1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论