Java reads database fields and values, outputs to regedit, XML, Text, Excel, PDF program

zhaozj2021-02-16  50

The main function, by reading the configuration file, extracting fields and values ​​from the database, writes to regedit, XML, Text, Excel, PDF. The program is the DOS interface. Because the rush does not write a Windows interface, if you are interested, you can change it to the Windows interface. Due to the configuration file, all operations do not need to be rewritten, just overwriting in the configuration file.

Main program:

Import java.io. *; import java.util.prefs. *; import java.sql. *; import jxl. *; import jxl.write. *; import java.util. *; import java.util.properties; import java.util.properties; import java.util.properties; import java.util.properties; import java.util.properties; import java.util.properties; import java.util.properties; import java.util.properties; import java.util.properties 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 String [] field = new string [1000]; static string [] value; static intect = 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 (number <= 6) {try {// choice output formatting System.out.println ( "please choice input formatting! !! "); System.out.print (" 1: Input Regedit 2: Input XML 3: Input Text "); System.out.Println (" 4: Excel 5: PDF 6: ALL 7: EXIT "); System .out.print ("INPUT A NUMBER, NOT A Character:"); // Keyword Input Buf = New BufferedReader (NEW InputStreamRead); rnumber = buf.readline (); // change "Number" TYPE Number = INTEGER.PARSEINT (RNUMBER); if (Number> 7) {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 (); breaf; case 3: / / Output text textoutput (); break; case 4: // output Excel Exceloutput (); break; case 5: // output excel pdfoutput (); break; case 6: // output all regeditoutput (); xmloutput (); XMLOUTPUT (); TextOutput (); ExcelOutput (); pdfoutput (); break;}} catch (ooException e) {system.err.println (e);} catch (NumberFormatexcect 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 * ************************************************************************************ Voidinfig () {Properties Prop = New Properties (); String PropFileName = "config.properties"; fileInputstream Fis;

Try {FIS = New FileInputStream (New File (PROPFILENAME)); Prop.Load (fis); // Get Config File's 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; cretetime = 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 successful *"); 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 value confount = 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 ) {Field [i] = rs.getMetadata (). getColumnName (i);} while (rs.next ()) {for (int J = 1; j <= Account; J ) { Maxcount; VALUES [MAXCOUNT] = rs.getstring (field [j]); system.out.println (value);}} // close database}} // close database}} // close database}} // close database}} // close database}} // close database}} // close database}} // close database}} // close Database}} // Close Database}} // Close Database Rs.Close (); stm.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 ("c Reatetime ", createtime); prefSDemo.put (" effect ", effect); prefsdemo.put (" Version ", Version);

System.out.println ("Regedit Create Success Equipment!");} / ***************************************** ************** * 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 Successful ");} / *** ***************************************************** * EFFECT: OUTPUT Time: NULL * RETURUE: NULL * CREATE TIME: 2004/6/9 * Edit Time: null ******************************** ********************************** / static void textoutput () {// Field length Int length1 [] = new int [1000]; // VALUES LENGTH INT length2 [] = new int [1000]; string space = " "; String str1 =" ------------------------- "; string str2 [] = new string [1000]; string str3 [] = new String [1000]; // Check "/ n" int = 1; // Initialize str1 [] str2 [] for (int i = 1; i <= maxcount; i ) {str2 [i] = ""; str3 [i] = "";

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) {length1 [i] = 25-field [i] .length (); // account length2 length for (int J = 1 J <= length1 [i]; j ) {str2 [i] = STR2 [i] "";}} length2 [i] = 25-value [i] .length (); // Account length3 Length for ( INT j = 1; j <= length2 [i]; j ) {str3 [i] = STR3 [i] "";}} // Write Field for (INT i = 1; i <= account; i ) { Fw.write (Field [I]); fw.write (STR2 [I] 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 (Values ​​[i ] STR3 [i] 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 successful");} / *********** ********************************************* * 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 excel's 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, Field [I]); 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, values ​​[rows]); Sheet.Addcell (label2);}}

Workbook.write (); workbook.close ();} catch (exception e) {system.err.println (e);} system.out.println ("Excel Create Successful");} / ****** ****************************************** * 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 int [account]; for (int i = 1; i <= account; i ) {hws [i-1] = 20;

Table.SetWidths (HWS); Table.SetWidth (100); // Add Field for (INT i = 1; i <= Account; i ) {Table.Addcell (New Phrase);} // Add Values ​​for (int i = 1; i <= maxcount; i ) {Table.Addcell (New Phrase);}

// add table to pdf file document.add (table); // close pdf file document.close ();} catch (exception e) {system.out.println (e);} system.out.println ("PDF Create success ");}}

转载请注明原文地址:https://www.9cbs.com/read-23549.html

New Post(0)