guhetu 发表于 2016-5-9 14:10:36

用Java实现HTML文件代替数据库存储数据




                 Java在编写一些图形化的小程序时,有时也要去存储少量的数据,如果用JDBC来连接数据库,就会使程序速度减慢,而且及不方面,我们可以用Java中的文件来代替数据库保存数据,这样不但可以实现存储的功能,而且不用考虑数据频繁的存取,可以把文件定义成为HTML文件,并将存储的数据以表格的方式显示,这样就可以直接在网页中看到数据,下面是我写的一个小的文件存储数据的实例:
importjava.awt.event.ActionEvent;
import java.awt.event.ActionListener;
importjava.sql.*;
import java.io.*;
import javax.swing.*;
public classfilework extends JFrame implements ActionListener {
JPanel p;
JButtonb0,b1,b2;
JTable tab;
Object my[][] = new Object;
inta[]=new int;
int b[]=new int;
int j=0,rows=0;
intk=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 = newJPanel();
tab = new JTable(my, title);
b0 = newJButton("添加");
b1 = new JButton("打开");
b2 = newJButton("保存");
b1.addActionListener(this);
b2.addActionListener(this);
b0.addActionListener(this);
p.add(b0);
p.add(b1);
p.add(b2);
this.getContentPane().add(newJScrollPane(tab), "Center");
this.getContentPane().add(p,"South");
this.setVisible(true);
}
public void fileopen(){
try{
JFileChooser choose=new JFileChooser(".");
intsis=choose.showOpenDialog(this);
Stringname=choose.getSelectedFile()+"";
if(sis==JFileChooser.APPROVE_OPTION){
in=newBufferedReader(new FileReader(name));
String over=null;
Stringline;
while((line=in.readLine())!=null){
over=over+line;
}
for(inti=0;i<a.length;i++){
a=over.indexOf(&quot;<td>&quot;)+4;
b=over.indexOf(&quot;</td>&quot;);
my=over.substring(a,b);
over=over.substring(b+4,over.length());
k=k+1;
if(k==3){k=0;rows=rows+1;}
}
}
}catch(Exceptionie){}
}
public static void main(String[] args) {
fileworkfilework = new filework();
}
publicvoid actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
fileopen();
}
if(e.getSource() == b2) {
JFileChooser save=new JFileChooser(&quot;.&quot;);
int result=save.showSaveDialog(null);
String s1=save.getSelectedFile()+&quot;&quot;;
if(result==JFileChooser.APPROVE_OPTION){
try{
OutputStreamWriterout = new OutputStreamWriter(new
FileOutputStream(s1));
Strings2=&quot;<html><head></head><body>&quot;;
s2=s2+&quot;<tableborder=&quot;1&quot; width=&quot;50%&quot; bordercolor=&quot;green&quot; align=&quot;center&quot;>&quot;;
s2=s2+&quot;<tr>&quot;;
s2=s2+&quot;<th>&quot;+&quot;姓名&quot;+&quot;</th>&quot;;
s2=s2+&quot;<th>&quot;+&quot;年龄&quot;+&quot;</th>&quot;;
s2=s2+&quot;<th>&quot;+&quot;身高&quot;+&quot;</th>&quot;;
s2=s2+&quot;</tr>&quot;;
for(intshu=0;shu<rows;shu++)
{
s2=s2+&quot;<tralign=&quot;center&quot;>&quot;;
s2=s2+&quot;<td>&quot;+my.toString().trim()+&quot;</td>&quot;;
s2=s2+&quot;<td>&quot;+my.toString().trim()+&quot;</td>&quot;;
s2=s2+&quot;<td>&quot;+my.toString().trim()+&quot;</td>&quot;;
s2=s2+&quot;</tr>&quot;;
}
s2=s2+&quot;</table></body></html>&quot;;
out.write(s2);
out.close();
for(int i1=0;i1<50;i1++){
my=&quot;&quot;;
my=&quot;&quot;;
my=&quot;&quot;;
}
rows=0;
this.repaint();
JOptionPane.showMessageDialog(null,&quot;保存数据成功!&quot;,
&quot;information&quot;,JOptionPane.QUESTION_MESSAGE);
}catch (Exception ex1) {
}
}
}
if(e.getSource()==b0){
JTextFieldmessage[]=new JTextField;
message=newJTextField(&quot;请输入姓名:&quot;);
message.setEditable(false);
message=newJTextField();
message=newJTextField(&quot;请输入年龄:&quot;);
message.setEditable(false);
message=newJTextField();
message=newJTextField(&quot;请输入身高:&quot;);
message.setEditable(false);
message=newJTextField();
Stringstr[]={&quot;确认&quot;,&quot;取消&quot;};
intgo=JOptionPane.showOptionDialog(null, message, &quot;添加用户&quot;,
JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE, null, str, str);
if(go==0){
if(message.getText().equals(&quot;&quot;)&&
message.getText().equals(&quot;&quot;)&&
message.getText().equals(&quot;&quot;)){
JOptionPane.showMessageDialog(null,&quot;数据不能为空&quot;,&quot;ERROR&quot;,JOptionPane.ERROR_MESSAGE);}
else{
my=message.getText();
my=message.getText();
my=message.getText();
this.repaint();rows=rows+1;}
}
}
}
}

            


来源链接: http://www.3lian.com/edu/2014/01-10/123301.html
页: [1]
查看完整版本: 用Java实现HTML文件代替数据库存储数据