Moke|墨客

 找回密码
 立即注册
搜索
查看: 7529|回复: 0

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

[复制链接]

310

主题

10

回帖

1688

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1688

优秀版主荣誉管理论坛元老

发表于 2016-5-9 14:10:36 | 显示全部楼层 |阅读模式



                 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=over.indexOf(&quot;<td>&quot;)+4;
b=over.indexOf(&quot;</td>&quot;);
my[rows][k]=over.substring(a,b);
over=over.substring(b+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(&quot;.&quot;);
int result=save.showSaveDialog(null);
String s1=save.getSelectedFile()+&quot;&quot;;
if(result==JFileChooser.APPROVE_OPTION){
try  {
OutputStreamWriter  out = new OutputStreamWriter(new
FileOutputStream(s1));
String  s2=&quot;<html><head></head><body>&quot;;
s2=s2+&quot;<table  border=&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(int  shu=0;shu<rows;shu++)
{
s2=s2+&quot;<tr  align=&quot;center&quot;>&quot;;
s2=s2+&quot;<td>&quot;+my[shu][0].toString().trim()+&quot;</td>&quot;;
s2=s2+&quot;<td>&quot;+my[shu][1].toString().trim()+&quot;</td>&quot;;
s2=s2+&quot;<td>&quot;+my[shu][2].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[i1][0]=&quot;&quot;;
my[i1][1]=&quot;&quot;;
my[i1][2]=&quot;&quot;;
}
rows=0;
this.repaint();
JOptionPane.showMessageDialog(null,&quot;保存数据成功!&quot;,
&quot;information&quot;,JOptionPane.QUESTION_MESSAGE);
}  catch (Exception ex1) {
}
}
}
if(e.getSource()==b0){
JTextField  message[]=new JTextField[6];
message[0]=new  JTextField(&quot;请输入姓名:&quot;);
message[0].setEditable(false);
message[1]=new  JTextField();
message[2]=new  JTextField(&quot;请输入年龄:&quot;);
message[2].setEditable(false);
message[3]=new  JTextField();
message[4]=new  JTextField(&quot;请输入身高:&quot;);
message[4].setEditable(false);
message[5]=new  JTextField();
String  str[]={&quot;确认&quot;,&quot;取消&quot;};
int  go=JOptionPane.showOptionDialog(null, message, &quot;添加用户&quot;,
JOptionPane.YES_OPTION,  JOptionPane.INFORMATION_MESSAGE, null, str, str[0]);
if(go==0){
if(message[1].getText().equals(&quot;&quot;)&&
message[3].getText().equals(&quot;&quot;)&&
message[5].getText().equals(&quot;&quot;)){
JOptionPane.showMessageDialog(null,&quot;数据不能为空&quot;,&quot;ERROR&quot;,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;}
}
}
}
}

            


来源链接: http://www.3lian.com/edu/2014/01-10/123301.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 

 

快速回复 返回顶部 返回列表