站内搜索
本类推荐文章
用Java实现HTML文件代替数据库存储数据
作者:    来源:互连网    点击:    日期:2008-7-18 10:08:21   

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 {

首页 上一页 [1] [2] [3]  下一页 尾页 
用Java实现HTML文件代替数据库存储数据 评论