利用接口实现数据库平台的扩展性

        在C#中实现了同样的功能,现在学习JAVA也想着实现同样的功能.程序的学习,看来最重要的是业务逻辑.
结构是接口提供抽象方法,根据数据库平台的不同提供实现该数据库的操作,然后一个作为过渡的类把数据操作的具体类和接口直接整合在一起,为业务层提供常规数据操作. 

//利用接口在程序底层实现数据库平台的无关性;数据层,数据库平台的无关性
//定义一个接口,提供读取数据库表的基本方法

//接口,提供可以重写的方法
import java.sql.*;
interface DBase{
                
//读取一个表里面的内容
                void readTable();
}


//具体实现类
class Access implements DBase{
                
public void readTable(){
                            
try{
                                    Class.forName( 
"sun.jdbc.odbc.JdbcOdbcDriver" );
                                    
//连接ACCESS数据库,需要到控制面板--管理工具--数据源 (ODBC)--选择新数据源的驱动程序: Driver Microsoft Access(*.mdb),击完成钮
                                    
//,在此输入输入新数据源名为Access2003
                                    String str = "jdbc:odbc:Access2003";
                                    Connection conn 
= DriverManager.getConnection( str );
                                    Statement state 
= conn.createStatement();
                                    String sql 
= "Select * From ps";
                                    ResultSet rs 
= state.executeQuery( sql );
                                    
while( rs.next() ){
                                                System.out.println( rs.getString( 
"title" ) );
                                    }

                                    rs.close();
                                    conn.close();
                            }

                            
catch( Exception e){
                                    System.out.println( e.toString() );
                            }

                }

}


//实现SQL数据库的访问
class SQL implements DBase{
                
public void readTable(){
                            
try{
                                    Class.forName( 
"sun.jdbc.odbc.JdbcOdbcDriver" );
                                    
//"管理工具"的"数据源(ODBC)"打开"ODBC数据源管理器"对话框,单击"系统DSN"选项卡,
                                    
//然后单击"添加"按钮,得到"创建数据源"对话框,选择"SQL Server"并单击"完成"按钮,
                                    
//在出现的"建立新的数据源到SQL Server"对话框中的"数据源名称"项填写"sql2000"并选
                                    
//取"服务器名",然后单击"下一步"按钮,选择用户名和密码登陆,输入连接数据库的用户名
                                    
//和密码就可以
                                    String str = "jdbc:odbc:sql2000";
                                    String userName 
= "sa";
                                    String userPass 
= "123456";
                                    Connection conn 
= DriverManager.getConnection( str,userName,userPass );
                                    Statement state 
= conn.createStatement();
                                    String sql 
= "Select * From ps";
                                    ResultSet rs 
= state.executeQuery( sql );
                                    
while( rs.next() ){
                                                System.out.println( rs.getString( 
"title" ) );
                                    }

                                    rs.close();
                                    conn.close();
                            }

                            
catch( Exception e){
                                    System.out.println( e.toString() );
                            }

                }

}


//整合类,屏蔽了数据库平台,面向业务逻辑层
class DBHandle{
            DBase db;
            
public DBHandle( int dbChoice ){
                        
switch( dbChoice ){
                                    
case 1: db = new Access(); break;
                                    
case 2: db = new SQL();break;
                        }

            }

            
public void readTable(){
                        db.readTable();
            }

}


//业务逻辑,使用数据层
class ReadOneTable{
            
public static void main( String [] args ){
                            DBHandle dbh 
= new DBHandle(2);
                            dbh.readTable();
            }

}

posted on 2005-11-08 21:49  yongboy  阅读(624)  评论(0编辑  收藏  举报

导航