🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #19265

Re: [ת��] ����ݿ⡿�ܷ�ֱ�����ļ���ݿ⣿

#19265
From: ����
Date: Thu, 08 Jan 2004 04:22
218 lines
7473 bytes
import java.sql.*;

public class AddressBook {
    private final static String DB_NAME="AddressBook.mdb";
    private final static String TABLE_NAME="PersonalInfo";
    private final static int RECORDS_PER_PAGE=10;
//***************************************************************************

//���������������������CSDN����̳���ҵ���һƬ���£��������õ������ҵ����

    private final static String url="jdbc:odbc:driver={Microsoft Access Driver
 (*.mdb)};"+"DBQ="+DB_NAME;
//****************************************************************************
**
    private Connection cnn=null;
    private ResultSet rs=null;

    private boolean connectDB(){
      try{
      	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        cnn=DriverManager.getConnection(url);
        return true;
      }catch(Exception e){
          System.out.println(e.getMessage());
          return false;
       }
    }
    
    private void closeDB(){
    	try{
    		if(rs!=null){rs.close();}
    		if(cnn!=null){cnn.close();}
    		rs=null;
    		cnn=null;
    	}catch(SQLException e){
    		rs=null;
    		cnn=null;
    	}
    }
    
    private boolean getAllRecords(){    	
    	if(cnn==null){
    		if(connectDB()!=true){return false;}
    	}
    	    	
    	try{
    		if(rs!=null){
    		    rs.close();
    		     rs=null;
    	    }
    		Statement sta=cnn.createStatement();
    		rs=sta.executeQuery("select * from PersonalInfo");
    		if(rs==null){return false;}
    		return true;      		
    	}catch(SQLException e){
    		System.out.println("getAllRecords()-"+e.getMessage());
     		return false;
    	 }
    }
    
    
    public AddressRecord[] getNextRecordGroup(){
    	AddressRecord[] ars=new AddressRecord[RECORDS_PER_PAGE];
    	for(int j=0;j<RECORDS_PER_PAGE;j++){
    		ars[j]=new AddressRecord();
    	}
    	
    	if(rs==null){
    		if(getAllRecords()!=true){return null;}
    	}
    	
    	try{
    		Statement sta=cnn.createStatement();
    		    		
    		int i=0;
    		while(rs.next()){
    			ars[i].setNO(rs.getLong("NO"));
    			ars[i].setName(rs.getString("Name"));
    			ars[i].setMail(rs.getString("Mail"));
    			ars[i].setTelephone(rs.getString("Telephone"));
    			i++;
    			if(i>=RECORDS_PER_PAGE){break;}
    		}
    		
     		return ars;    		
    		
    	}catch(SQLException e){
    		System.out.println("getNextRecordGroup()-"+e.getMessage());
    		return null;
    	 }
    }
    
    public String getNextAddress(){
    	return "abcde";
    }
    public boolean AddNewAddress(AddressRecord ar){
    	try{
    		if(ar==null){return false;}
    		if(rs!=null){
    			rs.close();
    			rs=null;
    		}
    		
    		String sql="INSERT INTO "+TABLE_NAME+" "+"VALUES(";
    		sql+=ar.getNO()+",";
    		sql+="'"+ar.getName()+"'"+",";
    		sql+="'"+ar.getMail()+"'"+",";
    		sql+="'"+ar.getTelephone()+"'"+")";
    		
    		Statement sta=cnn.createStatement();
    		if(sta.executeUpdate(sql)==0){return false;}
    		
    		return true;
    	}catch(Exception e){
    		System.out.println("AddNewAddress()-"+e.getMessage());
    		return false;
    	}
    }
    
    public boolean DelAddress(String name){
    	try{
      		if(rs!=null){
    			rs.close();
    			rs=null;
    		}
    		
    		String sql="delete from "+TABLE_NAME+" "+"where Name=";
    		sql+="'"+name+"'";
    		System.out.println("SQL-"+sql);
    		
    		Statement sta=cnn.createStatement();
    		if(sta.executeUpdate(sql)==0){return false;}
    		
    		return true;
    	}catch(Exception e){
    		System.out.println("DelAddress()-"+e.getMessage());
    		return false;
    	}
    }
    
    public AddressRecord searchByName(String name){
    	try{
    		if(name==null){return null;}
    		
    		String sql="select * from "+TABLE_NAME+" "+"where Name='";
    		sql+=name+"'";
    		Statement sta=cnn.createStatement();
    		ResultSet rst=sta.executeQuery(sql);
    		if(rst==null){return null;}
    		
    		AddressRecord addr=new AddressRecord();
    		rst.next();
    		addr.setNO(rst.getLong("NO"));
    		addr.setName(rst.getString("Name"));
    		addr.setMail(rst.getString("Mail"));
    		addr.setTelephone(rst.getString("Telephone"));
    		
    		rst.close();
    		
    		return addr;
    	}catch(Exception e){
    		System.out.println("searchByName-"+e.getMessage());
    		return null;
    	}
    }

    public static void main(String[] args){
      AddressBook ab=new AddressBook();
           
      AddressRecord[] ars=ab.getNextRecordGroup();
      if(ars==null){
      	System.out.println("Failed to read table \"PersonalInfo\"");
      }
      else{
      	for(int i=0;i<ars.length;i++){System.out.println(ars[i].toString());}

      }
      
      boolean b;
      AddressRecord a=new AddressRecord(2,"liuchunhui",
                                        "a@kien.com","124464");
      b=ab.AddNewAddress(a);                                    
      if(b){System.out.println("Add: success!");}
      else{System.out.println("Add: failed!");}
      
      ars=ab.getNextRecordGroup();
      if(ars==null){System.out.println("Failed to read table \"PersonalInfo\""
);}
      else{
      	  for(int i=0;i<ars.length;i++){System.out.println(ars[i].toString());}

      }
      
      b=ab.DelAddress("liuchui");
      if(b){System.out.println("delete:success");}
      else{System.out.println("delete:failed");}
      ars=ab.getNextRecordGroup();
      if(ars==null){System.out.println("Failed to read table \"PersonalInfo\""
);}
      else{
      	for(int i=0;i<ars.length;i++){System.out.println(ars[i].toString());}

      }
      
      AddressRecord ss=ab.searchByName("liuchunhui");
      if(ss==null){System.out.println("search-failed!");}
      else{System.out.println(ss.toString());}         
    }
}
�� �� eldon. �Ĵ������ᵽ: ��
: �� ��������ת���� Java ������ ��
: �� ԭ���� eldon ��� ��
: ʹ��jdbc��odbc��ʱ���ܲ���ֱ����access����ݿ��ļ���
: ����·����������ݿ����Ҫ�����Դ�������ã�

--
�뿪�����֪������ã�����˰����֪���Ժ󲻻����Ķ�һ�Ρ�
�� ��Դ:���ɽ��ˮ bbs.neu.edu.cn��[FROM: 210.83.210.174]
Technical Details
Message-ID: <4AB8Fi$Bqx@bbs.neu.edu.cn>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!news.neu.edu.cn!NEU

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,GB
X-Google-Thread: f6cb0,ded53cd259997ef3
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2004-01-07 20:23:07 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!news.neu.edu.cn!NEU
From: stillicel.bbs@bbs.neu.edu.cn (����)
Newsgroups: cn.bbs.comp.lang.java
Subject: Re: [ת��] ����ݿ⡿�ܷ�ֱ�����ļ���ݿ⣿
Date: 08 Jan 2004 04:22:20 GMT
Organization: ��ɽ��ˮ
Message-ID: <4AB8Fi$Bqx@bbs.neu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: java/M.1073535740.A
Lines: 218