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

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

None.gif // 利用接口在程序底层实现数据库平台的无关性;数据层,数据库平台的无关性
None.gif
// 定义一个接口,提供读取数据库表的基本方法
None.gif
None.gif
// 接口,提供可以重写的方法
None.gif
import  java.sql. * ;
ExpandedBlockStart.gifContractedBlock.gif
interface  DBase dot.gif {
InBlock.gif                
//读取一个表里面的内容
InBlock.gif
                void readTable();
ExpandedBlockEnd.gif}

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

InBlock.gif                                    rs.close();
InBlock.gif                                    conn.close();
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
catch( Exception e)dot.gif{
InBlock.gif                                    System.out.println( e.toString() );
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif}

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

InBlock.gif                                    rs.close();
InBlock.gif                                    conn.close();
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockStart.gifContractedSubBlock.gif                            
catch( Exception e)dot.gif{
InBlock.gif                                    System.out.println( e.toString() );
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif}

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

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
public void readTable()dot.gif{
InBlock.gif                        db.readTable();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif}

None.gif
None.gif
// 业务逻辑,使用数据层
ExpandedBlockStart.gifContractedBlock.gif
class  ReadOneTable dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public static void main( String [] args )dot.gif{
InBlock.gif                            DBHandle dbh 
= new DBHandle(2);
InBlock.gif                            dbh.readTable();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/yongboy/archive/2005/11/08/271823.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值