萬盛學電腦網

 萬盛學電腦網 >> 網頁制作 >> 腳本Html教程 >> 用Java實現HTML文件代替數據庫存儲數據

用Java實現HTML文件代替數據庫存儲數據

 Java在編寫一些圖形化的小程序時,有時也要去存儲少量的數據,如果用JDBC來連接數據庫,就會使程序速度減慢,而且及不方面,我們可以用Java中的文件來代替數據庫保存數據,這樣不但可以實現存儲的功能,而且不用考慮數據頻繁的存取,可以把文件定義成為HTML文件,並將存儲的數據以表格的方式顯示,這樣就可以直接在網頁中看到數據,下面是我寫的一個小的文件存儲數據的實例:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.io.*;
import javax.swing.*;
public class filework extends JFrame implements ActionListener {
JPanel p;
JButton b0,b1,b2;
JTable tab;
Object my[][] = new Object[50][3];
int a[]=new int[50];
int b[]=new int[50];
int j=0,rows=0;
int k=0,shu=0;
String name,age;
String title[] = {"姓名", "年齡", "身高"};
BufferedReader in;
int i = 0;
public filework() {
super("數據輪回");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
tab = new JTable(my, title);
b0 = new JButton("添加");
b1 = new JButton("打開");
b2 = new JButton("保存");
b1.addActionListener(this);
b2.addActionListener(this);
b0.addActionListener(this);
p.add(b0);
p.add(b1);
p.add(b2);
this.getContentPane().add(new JScrollPane(tab), "Center");
this.getContentPane().add(p, "South");
this.setVisible(true);
}
public void fileopen() {
try{
JFileChooser choose=new JFileChooser(".");
int sis=choose.showOpenDialog(this);
String name=choose.getSelectedFile()+"";
if(sis==JFileChooser.APPROVE_OPTION){
in=new BufferedReader(new FileReader(name));
String over=null;
String line;
while((line=in.readLine())!=null){
over=over+line;
}
for(int i=0;i<a.length;i++){
a[i]=over.indexOf("<td>")+4;
b[i]=over.indexOf("</td>");
my[rows][k]=over.substring(a[i],b[i]);
over=over.substring(b[i]+4,over.length());
k=k+1;
if(k==3){k=0;rows=rows+1;}
}
}
}catch(Exception ie){}
}
public static void main(String[] args) {
filework filework = new filework();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
fileopen();
}
if (e.getSource() == b2) {
JFileChooser save=new JFileChooser(".");
int result=save.showSaveDialog(null);
String s1=save.getSelectedFile()+"";
if(result==JFileChooser.APPROVE_OPTION){
try {
OutputStreamWriter out = new OutputStreamWriter(new
FileOutputStream(s1));
String s2="<html><head></head><body>";
s2=s2+"<table border="1" width="50%" bordercolor="green" align="center">";
s2=s2+"<tr>";
s2=s2+"<th>"+"姓名"+"</th>";
s2=s2+"<th>"+"年齡"+"</th>";
s2=s2+"<th>"+"身高"+"</th>";
s2=s2+"</tr>";
for(int shu=0;shu<rows;shu++)
{
s2=s2+"<tr align="center">";
s2=s2+"<td>"+my[shu][0].toString().trim()+"</td>";
s2=s2+"<td>"+my[shu][1].toString().trim()+"</td>";
s2=s2+"<td>"+my[shu][2].toString().trim()+"</td>";
s2=s2+"</tr>";
}
s2=s2+"</table></body></html>";
out.write(s2);
out.close();
for(int i1=0;i1<50;i1++){
my[i1][0]="";
my[i1][1]="";
my[i1][2]="";
}
rows=0;
this.repaint();
JOptionPane.showMessageDialog(null,"保存數據成功!",
"information",JOptionPane.QUESTION_MESSAGE);
} catch (Exception ex1) {
}
}
}
if(e.getSource()==b0){
JTextField message[]=new JTextField[6];
message[0]=new JTextField("請輸入姓名:");
message[0].setEditable(false);
message[1]=new JTextField();
message[2]=new JTextField("請輸入年齡:");
message[2].setEditable(false);
message[3]=new JTextField();
message[4]=new JTextField("請輸入身高:");
message[4].setEditable(false);
message[5]=new JTextField();
String str[]={"確認","取消"};
int go=JOptionPane.showOptionDialog(null, message, "添加用戶",
JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, null, str, str[0]);
if(go==0){
if(message[1].getText().equals("")&&
message[3].getText().equals("")&&
message[5].getText().equals("")){
JOptionPane.showMessageDialog(null,"數據不能為空","ERROR",JOptionPane.ERROR_MESSAGE);}
else{
my[rows][0]=message[1].getText();
my[rows][1]=message[3].getText();
my[rows][2]=message[5].getText();
this.repaint();rows=rows+1;}
}
}
}
}

copyright © 萬盛學電腦網 all rights reserved