⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outputfile.java

📁 java数据库编程。JDBC+SQL+GUI。用java写的一个影碟租赁系统
💻 JAVA
字号:
//Vedio rental System Developed by Banu
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.text.DecimalFormat;

public class outputFile
{
	
	public static void TXT (ResultSet rsList, String strFile) throws Exception
	{
		
		strFile = removeExtension(strFile, ".txt");
		
		try
		{
	
			BufferedWriter createDocument = new BufferedWriter(new FileWriter(strFile + ".txt"));
			String strLine = "";
			int counter = 1;
			
			strLine = "Records from : " + strFile;
			writeLine(strLine, createDocument);
			strLine = getTimeStamp();
			writeLine(strLine, createDocument);
			createDocument.newLine();
			ResultSetMetaData rsmd = rsList.getMetaData();
			while (rsList.next())
			{
				strLine = "(" + counter + ") " + rsList.getInt(1); 
			    for (int i = 2; i <= rsmd.getColumnCount(); i++)
			    {
			    	strLine += " " + rsList.getString(i); 
			    }
			    
				writeLine(strLine, createDocument);
				counter++;
				
			}
			
			createDocument.close();
			JOptionPane.showMessageDialog(null, "Records written successfully to\n" + System.getProperty("user.dir") + "\\" + strFile + ".txt", "Save", JOptionPane.INFORMATION_MESSAGE);
			
		}
		catch (IOException ex)
		{
			throw new Exception("Error writing file " + System.getProperty("user.dir") + "\\" + strFile + ".txt\nPlease check file permissions");
		}
		
	}
	
	public static void XML (ResultSet rsList, String strFile) throws Exception
	{
		
		strFile = removeExtension(strFile, ".xml");
		ResultSetMetaData rsmd = rsList.getMetaData();
		
		try
		{
			
			BufferedWriter createDocument = new BufferedWriter(new FileWriter(strFile + ".xml"));
			String strLine = "";
			int counter = 1;
			
			strLine = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
			writeLine(strLine, createDocument);
			createDocument.newLine();
			strLine = "<!DOCTYPE conversions[";
			writeLine(strLine, createDocument);
			createDocument.newLine();
			strLine = "\t<!ELEMENT " + strFile +" (" ;
		    for (int i = 1; i <= rsmd.getColumnCount(); i++)
		    {	
		    	strLine += rsmd.getColumnName(i);
		    	if (i==rsmd.getColumnCount())
		    		{strLine += ")>";
		    		break;}
		    	else
		    		strLine += ", ";
		    }
			writeLine(strLine, createDocument);
		    for (int i = 1; i <= rsmd.getColumnCount(); i++)
		    {	
		    	strLine = "\t\t<!ELEMENT " + rsmd.getColumnName(i) +" (#PCDATA)>";
		    	writeLine(strLine, createDocument);
		    }
			createDocument.newLine();
			strLine = "]>";
			writeLine(strLine, createDocument);
			createDocument.newLine();
			
			strLine = "<!-- " + getTimeStamp() + " -->";
			writeLine(strLine, createDocument);
			createDocument.newLine();
			strLine = "<" + strFile + ">";
			writeLine(strLine, createDocument);
			createDocument.newLine();
			
			while (rsList.next())
			{
				
				strLine = "\t<index>" + counter + "</index>";
				writeLine(strLine, createDocument);
				for (int i=1; i<=rsmd.getColumnCount(); i++)
				{	
					strLine = "\t<" + rsmd.getColumnName(i) + ">" + rsList.getString(i) + "</"+ rsmd.getColumnName(i) +">";
					writeLine(strLine, createDocument);
				}
				createDocument.newLine();
				counter++;
				
			}
			
			strLine = "</" + strFile + ">";
			writeLine(strLine, createDocument);
			
			createDocument.close();
			JOptionPane.showMessageDialog(null, "Records written successfully to\n" + System.getProperty("user.dir") + "\\" + strFile + ".xml", "Save", JOptionPane.INFORMATION_MESSAGE);
			
		}
		catch (IOException ex)
		{
			throw new Exception("Error writing file " + System.getProperty("user.dir") + "\\" + strFile + ".xml\nPlease check file permissions");
		}			
		
	}
	
	private static void writeLine (String strLine, BufferedWriter createDocument) throws IOException
	{
		
		createDocument.write(strLine, 0, strLine.length());
		createDocument.newLine();
		
	}
	
	private static String removeExtension (String strFile, String strExt)
	{
		
		if (strFile.toLowerCase().endsWith(strExt.toLowerCase()))
		{
			strFile = strFile.substring(0, strFile.length() - 4);
		}
		
		return strFile;
		
	}
	
	private static String getTimeStamp ()
	{
		
		Calendar calNow = Calendar.getInstance();
		DecimalFormat decFormat = new DecimalFormat("00");
		return "Last Updated " + decFormat.format(calNow.get(Calendar.DAY_OF_MONTH)) + "\\" + decFormat.format((calNow.get(Calendar.MONTH) + 1)) + "\\" + calNow.get(Calendar.YEAR) + " at " + decFormat.format(calNow.get(Calendar.HOUR_OF_DAY)) + ":" + decFormat.format(calNow.get(Calendar.MINUTE));
			
	}
		
		

}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -